可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I'd like to rebase to a specific commit, not to a HEAD of the other branch:
A --- B --- C master
\
\-- D topic
to
A --- B --- C master
\
\-- D topic
instead of
A --- B --- C master
\
\-- D topic
How can I achieve that?
回答1:
You can avoid using the --onto parameter by making a temp branch on the commit you like and then use rebase in it's simple form:
git branch temp master^
git checkout topic
git rebase temp
git branch -d temp
回答2:
You can even take a direct approach:
git checkout topic
git rebase <commitB>
回答3:
Use the "onto" option:
git rebase --onto master^ D^ D
回答4:
The comment by jsz above saved me tons of pain, so here's a step-by-step recipie based on it that I've been using to rebase/move any commit on top of any other commit:
- Find a previous branching point of the branch to be rebased (moved) - call it old parent. In the example above that's A
- Find commit on top of which you want to move the branch to - call it new parent. In the exampe that's B
- You need to be on your branch (the one you move):
- Apply your rebase:
git rebase --onto <new parent> <old parent>
In the example above that's as simple as:
git checkout topic
git rebase --onto B A
回答5:
I've used a mixture of solutions described above:
$ git branch temp <specific sha1>
$ git rebase --onto temp master topic
$ git branch -d temp
I found it much easier to read and understand. The accepted solution lead me to a merge conflict (too lazy to fix by hand):
$ git rebase temp
First, rewinding head to replay your work on top of it...
Applying: <git comment>
Using index info to reconstruct a base tree...
M pom.xml
.git/rebase-apply/patch:10: trailing whitespace.
<some code>
.git/rebase-apply/patch:17: trailing whitespace.
<some other code>
warning: 2 lines add whitespace errors.
Falling back to patching base and 3-way merge...
Auto-merging pom.xml
CONFLICT (content): Merge conflict in pom.xml
error: Failed to merge in the changes.
Patch failed at 0001 <git comment>
The copy of the patch that failed is found in: .git/rebase-apply/patch
When you have resolved this problem, run "git rebase --continue".
If you prefer to skip this patch, run "git rebase --skip" instead.
To check out the original branch and stop rebasing, run "git rebase --abort".
回答6:
There is another way of doing it or if you wish to move back to more than just one commit.
Here is a an example to move back to n
number of commits:
git branch topic master~n
For the sake of this question, this can also be done:
git branch topic master~1
The command works perfectly on git version 2.7.4
. Haven't tested it on any other version.
回答7:
Since rebasing is so fundamental, here's an expansion of Nestor Milyaev's answer. Combining jsz's and Simon South's comments from Adam Dymitruk's answer yields this command which works on the topic
branch regardless of whether it branches from the master
branch's commit A
or C
:
git checkout topic
git rebase --onto <commit-B> <pre-rebase-A-or-post-rebase-C-or-base-branch-name>
Note that the last argument is required (otherwise it rewinds your branch to commit B
).
Examples:
# if topic branches from master commit A:
git checkout topic
git rebase --onto <commit-B> <commit-A>
# if topic branches from master commit C:
git checkout topic
git rebase --onto <commit-B> <commit-C>
# regardless of whether topic branches from master commit A or C:
git checkout topic
git rebase --onto <commit-B> master
So the last command is the one that I typically use.
回答8:
A simpler solution is git rebase <SHA1 of B> topic
. This works irrespective of where your HEAD
is.
We can confirm this behaviour from git rebase doc
<upstream>
Upstream branch to compare against. May be any valid
commit, not just an existing branch name. Defaults to the configured
upstream for the current branch.
You might be thinking what will happen if I mention SHA1 of
topic
too in the above command ?
git rebase <SHA1 of B> <SHA1 of topic>
My little experiment shows that the command will execute but instead it will rebase a copy of topic
branch there instead of rebasing topic
branch. In short it does not work as we were expecting it to work. Hence don't use like this.