git 将整个分支重置/恢复到另一个分支状态?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28774194/
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
Reset/revert a whole branch to another branches state?
提问by daniel451
I have a branch Aand a branch B(and some other branches).
我有一个分支A和一个分支B(以及其他一些分支)。
Lets say A's commit history looks like:
假设A的提交历史看起来像:
- commit 5
- commit 4
- commit 3
- ...
- 提交 5
- 提交 4
- 提交 3
- ...
And B's commit history:
和B的提交历史:
- some other commit
- commit 4
- merge of other stuff from branch C(into branch B)
- commit 3
- ...
- 其他一些提交
- 提交 4
- 合并来自分支C的其他内容(进入分支B)
- 提交 3
- ...
Basically what I want is to "delete" all changes made by the commits some other commitand merge of other stuff from branch Cto branch B.
基本上我想要的是“删除”提交所做的所有更改,其他提交和其他内容从分支 C到分支B 的合并。
I want the working tree of branch Bto be exactly the same like branch A's working tree.
我想分支的工作树乙是完全一样的像支一的工作树。
How do I achieve this?
我如何实现这一目标?
回答by Rüdiger Herrmann
One way to achieve this is through git reset
. While on branch B
execute
实现这一目标的一种方法是通过git reset
. 在分支B
执行时
git reset --hard A
Thereafter, branch B
points to the head-commit of A
. The --hard
option resets the index and working tree so that all tracked files are reset to the version in branch A
. The old HEAD commit-ID of A
is stored in .git/ORIG_HEAD
in order to allow undoing the change.
此后, branchB
指向 的 head-commit A
。该--hard
选项会重置索引和工作树,以便将所有跟踪的文件重置为 branch 中的版本A
。旧的 HEAD 提交 IDA
存储在其中.git/ORIG_HEAD
以允许撤消更改。
Alternatively - while not on branch B
- you can delete branch B
and re-created it like this:
或者 - 虽然不在分支上B
- 您可以删除分支B
并像这样重新创建它:
git branch -d B # delete branch B
git branch B A # re-create branch B and let it point to the commit of branch A
Other than the first suggestion, this will leave the index and working tree untouched.
除了第一个建议之外,这将使索引和工作树保持不变。
回答by pratZ
If you want your branch B
to look exactly like branch A
. You could just do a reset --hard
如果您希望您的分支B
看起来完全像 branch A
。你可以做一个reset --hard
git checkout branch-B
git reset --hard branch-A
Be careful you will lose commits in this case. Your branch-B will look exactly like branch-A, whatever commits were made to branch-B, that were not present in branch-A, will be lost. Also if branch-B is shared with other people, its not recommended to perform this operation.
小心在这种情况下你会丢失提交。您的分支 B 将看起来与分支 A 完全一样,对分支 B 所做的任何提交,在分支 A 中不存在的,都将丢失。另外如果分支 B 与其他人共享,则不建议执行此操作。
In that case you could try reverting the commits you don't want in branch-B
在这种情况下,您可以尝试在分支 B 中恢复您不想要的提交
git revert <sha-of-"some other commit">
git revert <sha-of-"merge of other stuff from branch C (into branch B)">
The second commit looks like a merge commit so you might have to pass the parent as well.
第二次提交看起来像合并提交,因此您可能也必须通过父提交。
git revert <sha-of-"merge of other stuff from branch C (into branch B)"> -m1
回答by RomainValeri
For completion, let's add this very simple way to achieve it :
为了完成,让我们添加这个非常简单的方法来实现它:
git branch -f branchB branchA
It takes advantage of the fact that branches in git are mere pointers. This command just replaces the ref to the tip commit of one branch with another. No need to go into complex structure changes to build something you already have.
它利用了这样一个事实,即 git 中的分支仅仅是指针。此命令只是将一个分支的提示提交的引用替换为另一个。无需进行复杂的结构更改来构建您已经拥有的东西。