重置/还原整个分支到另一个分支状态? [英] Reset/revert a whole branch to another branches state?
问题描述
我有一个分支 A 和一个分支 B (以及其他一些分支).
I have a branch A and a branch B (and some other branches).
让我们说 A 的提交历史如下:
Lets say A's commit history looks like:
- 提交5
- 提交4
- 提交3
- ...
和 B 的提交历史记录:
- 其他一些提交
- 提交4
- 合并分支 C 中的其他内容(进入分支 B )
- 提交3
- ...
- some other commit
- commit 4
- merge of other stuff from branch C (into branch B)
- commit 3
- ...
基本上我想要的是删除"所有由提交其他提交所做的更改,以及将其他东西从分支C合并到分支 B .
Basically what I want is to "delete" all changes made by the commits some other commit and merge of other stuff from branch C to branch B.
我希望分支 B 的工作树与分支 A 的工作树完全相同.
I want the working tree of branch B to be exactly the same like branch A's working tree.
我该如何实现?
推荐答案
一种方法是通过git reset
.在分支B
上执行
One way to achieve this is through git reset
. While on branch B
execute
git reset --hard A
此后,分支B
指向A
的head-commit. --hard
选项将重置索引和工作树,以便将所有跟踪的文件重置为分支A
中的版本. A
的旧HEAD提交ID存储在.git/ORIG_HEAD
中,以便撤消更改.
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.
或者-不在分支B
上时-您可以删除分支B
并重新创建它,如下所示:
Alternatively - while not on branch B
- you can delete branch B
and re-created it like this:
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.
这篇关于重置/还原整个分支到另一个分支状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!