问题描述
如何将我的本地分支重置为与远程存储库上的分支一样?
How do I reset my local branch to be just like the branch on the remote repository?
我做到了:
git reset --hard HEAD
但是当我运行 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.
推荐答案
设置你的分支与远程分支完全匹配可以分两步完成:
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
现在您的工作已保存在分支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).
请注意,第一个示例假定远程存储库的名称是origin";并且名为master"的分支远程仓库中的与本地仓库中当前签出的分支匹配.
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.
顺便说一句,您所处的这种情况看起来很像一种常见情况,即已将推送推送到非裸存储库的当前已检出分支.你最近有没有推送到你的本地仓库?如果没有,那么不用担心——一定是其他原因导致这些文件意外地被修改了.否则,您应该知道,不建议推送到非裸仓库(尤其不要推送到当前签出的分支).
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).
这篇关于将本地存储库分支重置为就像远程存储库 HEAD的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!