git:移动分支头
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9759423/
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
git: moving branch head
提问by Andrew Tomazos
I have two git branches, "A" and "B", and commits numbered 1 thru 8. My history looks like this
我有两个 git 分支,“A”和“B”,提交编号为 1 到 8。我的历史看起来像这样
1 -> 2 -> 3 -> 4[A] -> 5 -> 6 -> 7 -> 8[B]
I want to change it so my history looks like this:
我想改变它,让我的历史看起来像这样:
1 -> 2 -> 3 -> 4 -> 5 -> 6[A] -> 7 -> 8[B]
That is, I want to move the head of branch A from commit 4 to commit 6.
也就是说,我想将分支 A 的头部从提交 4 移动到提交 6。
What commands do I use to do this?
我使用什么命令来做到这一点?
回答by Matthew Flaschen
You can run:
你可以运行:
git branch -f A 6
回答by ralphtheninja
git checkout A
git reset --hard 6
回答by Kaz
This is a special case of rebase, just that the branch is empty:
这是 rebase 的一个特例,只是分支是空的:
git checkout A
git rebase B
rebase
is more general; it handles this case also:
rebase
更一般;它也处理这种情况:
Before:
前:
A1 -> A2 -> [A]
/
1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8[B]
After:
后:
A1' -> A2' -> [A]
/
1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8[B]
A1' and A2' are merged to account for the delta between 4 and 8 on the parent branch.
A1' 和 A2' 合并以解释父分支上 4 和 8 之间的增量。
Git rebase handles this trivial case without a hassle. I created a repo with two commits on master
and a branch br
pointing to the first commit.
Git rebase 可以毫不费力地处理这个微不足道的情况。我创建了一个 repo,其中包含两个提交master
和一个br
指向第一个提交的分支。
$ git checkout br
Switched to branch 'br'
$ git rebase master
First, rewinding head to replay your work on top of it...
Fast-forwarded br to master.
Poof, done. The log now shows the branch pointing to the second commit.
噗,搞定。日志现在显示指向第二次提交的分支。
We can also achieve this "After": [thanks to M. Flaschen for pointing out this was missing]:
我们也可以实现这个“之后”:[感谢 M. Flaschen 指出这是缺失的]:
A1'' -> A2'' -> [A]
/
1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8[B]
Instead of rebasing to the branch B, we would name the specific commit 6, e.g.
我们将特定的提交命名为 6,而不是重新定位到分支 B,例如
git checkout A
git rebase 6 # rather than rebase B
When there are no A1 and A2 commits, this reduces to the original question: moving the [A] pointer from 4 to 6.
当没有 A1 和 A2 提交时,这简化为原始问题:将 [A] 指针从 4 移动到 6。