Git: How to rebase to a specific commit?

后端 未结 9 568

I\'d like to rebase to a specific commit, not to a HEAD of the other branch:

A --- B --- C          master
 \\
  \\-- D                topic
<
相关标签:
9条回答
  • 2020-12-07 08:26

    You can avoid using the --onto parameter by making a temp branch on the commit you like and then use rebase in its simple form:

    git branch temp master^
    git checkout topic
    git rebase temp
    git branch -d temp
    
    0 讨论(0)
  • 2020-12-07 08:28

    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.

    0 讨论(0)
  • 2020-12-07 08:29

    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".
    
    0 讨论(0)
  • 2020-12-07 08:30

    You can even take a direct approach:

    git checkout topic
    git rebase <commitB>
    
    0 讨论(0)
  • 2020-12-07 08:31

    Use the "onto" option:

    git rebase --onto master^ D^ D
    
    0 讨论(0)
  • 2020-12-07 08:32

    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:

    1. Find a previous branching point of the branch to be rebased (moved) - call it old parent. In the example above that's A
    2. Find commit on top of which you want to move the branch to - call it new parent. In the exampe that's B
    3. You need to be on your branch (the one you move):
    4. 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
    
    0 讨论(0)
提交回复
热议问题