Git:重置/恢复整个分支到另一个分支状态? [英] Git: reset/revert a whole branch to another branches state?
问题描述
我有一个分支 A 和一个分支 B (以及其他一些分支)。
可以说 的提交历史记录如下所示:
- 提交5
- 提交4
- 提交3
- ...
- 一些其他提交
- commit 4
- 从分支 C (分支到 )的其他内容合并
- commit 3
- ...
<
$ b
基本上我想要的是删除所有由提交其他提交 和从分支C 合并到分支 B 的其他内容。
我希望分支 B 的工作树与分支 A 的工作树完全相同。
我该如何实现这一目标?
实现此目的的一种方法是通过 git reset
。在分支上 B
执行
git reset --hard A
之后,分支 B
指向头部提交 A
。 - hard
选项重置索引和工作树,以便所有跟踪的文件都重置为分支中的版本 A
。为了允许撤销更改, A
的旧头提交被复制到 .git / ORIG_HEAD
p>
或者 - 不在分支上 B
- 您可以删除分支 B
然后重新创建它:
git branch -d B#删除分支B
git branch BA #重新创建分支B并让它指向分支的提交A
除了第一个建议,这会使索引和工作树不变。
I have a branch A and a branch B (and some other branches).
Lets say A's commit history looks like:
- commit 5
- commit 4
- commit 3
- ...
And B's commit history:
- some other commit
- commit 4
- merge of other stuff from branch C (into branch B)
- commit 3
- ...
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.
I want the working tree of branch B to be exactly the same like branch A's working tree.
How do I achieve this?
One way to achieve this is through git reset
. While on branch B
execute
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 of A
is copied to .git/ORIG_HEAD
in order to allow undoing the change.
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.
这篇关于Git:重置/恢复整个分支到另一个分支状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!