Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
575 views
in Technique[技术] by (71.8m points)

git - 将本地存储库分支重置为类似于远程存储库HEAD(Reset local repository branch to be just like remote repository HEAD)

How do I reset my local branch to be just like the branch on the remote repository?

(如何将本地分支重置为与远程存储库中的分支相同?)

I did:

(我做了:)

git reset --hard HEAD

But when I run a git status ,

(但是当我运行git status ,)

On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)
      modified:   java/com/mycompany/TestContacts.java
      modified:   java/com/mycompany/TestParser.java

Can you please tell me why I have these 'modified'?

(您能告诉我为什么我要进行这些“修改”吗?)

I haven't touched these files?

(我还没有碰过这些文件?)

If I did, I want to remove those.

(如果我这样做了,我想删除那些。)

  ask by hap497 translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Setting your branch to exactly match the remote branch can be done in two steps:

(可以通过两个步骤将分支设置为与远程分支完全匹配:)

git fetch origin
git reset --hard origin/master

If you want to save your current branch's state before doing this (just in case), you can do:

(如果要在执行此操作之前保存当前分支的状态(以防万一),可以执行以下操作:)

git commit -a -m "Saving my work, just in case"
git branch my-saved-work

Now your work is saved on the branch "my-saved-work" in case you decide you want it back (or want to look at it later or diff it against your updated branch).

(现在,您的工作将保存在分支“ my-saved-work”上,以防万一您决定退回该文件(或稍后查看或将其与更新后的分支进行比较)。)

Note that the first example assumes that the remote repo's name is "origin" and that the branch named "master" in the remote repo matches the currently checked-out branch in your local repo.

(请注意,第一个示例假定远程仓库的名称为“ origin”,并且远程仓库中名为“ master”的分支与本地仓库中当前已签出的分支匹配。)

BTW, this situation that you're in looks an awful lot like a common case where a push has been done into the currently checked out branch of a non-bare repository.

(顺便说一句,您遇到的这种情况看起来很糟糕,就像一个普通情况,即已推送到非裸存储库的当前签出分支中。)

Did you recently push into your local repo?

(您最近是否进入了本地仓库?)

If not, then no worries -- something else must have caused these files to unexpectedly end up modified.

(如果没有,那么就不用担心-某些其他原因一定会导致这些文件意外地被修改。)

Otherwise, you should be aware that it's not recommended to push into a non-bare repository (and not into the currently checked-out branch, in particular).

(否则,您应该注意,不建议将其推送到非裸仓库(尤其是不要放入当前已签出的分支)。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...