Git:丢弃分支本地分支上的所有更改

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2358643/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 07:57:30  来源:igfitidea点击:

Git: Discard all changes on a diverged local branch

git

提问by Electrons_Ahoy

I have a local topic branch that's tracking a remote branch. For the sake of argument, say the commit histories look like this:

我有一个跟踪远程分支的本地主题分支。为了便于论证,假设提交历史如下所示:

A--B--C--O1--O2--O3 (origin/phobos)
       \
         L1--L2--L3 (phobos)

Having looked at the relative commit histories, I now want to discard all the changes to the local phobosbranch and get it back to being a direct copy of origin/phobos, so that the local history looks like this:

查看了相关的提交历史记录后,我现在想放弃对本地phobos分支的所有更改并将其恢复为 的直接副本origin/phobos,以便本地历史记录如下所示:

A--B--C--O1--O2--O3 (phobos origin/phobos)

I really don't want the local changes to the phobosbranch, and I really don't want any merges to show up in the origin repository afterwards. (So, just merging isn't what I have in mind.)

我真的不希望对phobos分支进行本地更改,而且我真的不希望之后在原始存储库中显示任何合并。(所以,只是合并不是我的想法。)

This seems like it should be really easy, but my google-fu has failed me. How do I do this?

这看起来应该很容易,但是我的 google-fu 失败了。我该怎么做呢?

采纳答案by mipadi

Delete the branch, then re-create it:

删除分支,然后重新创建它:

$ git branch -D phobos
$ git checkout --track -b phobos origin/phobos

回答by Dan Moulding

git checkout phobos
git reset --hard origin/phobos

This tells Git to reset the head of phobosto the same commit as origin/phobos, and to update the working tree to match.

这告诉 Git 将 head 重置phobos为与 相同的提交origin/phobos,并更新工作树以匹配。