Continuing a rebase with merge conflicts

When you rebase a commit or a branch on top of a different HEAD, you will eventually see a conflict.

If there is a conflict, you will be asked to solve the merge conflict and continue with the rebase using git rebase --continue.

How to do it

We will be creating a commit that adds the same fishtank.txt file on top of the origin/stable-3.1 branch; then, we will try to rebase this on top of the rebaseExample branch we created in the Rebasing commits to another branch section:

  1. Check out a branch named rebaseExample2 that tracks origin/stable-3.1:
    $ git checkout -b rebaseExample2 --track origin/stable-3.1
    Checking out files: 100% (212/212), done.
    Branch rebaseExample2 set up to track remote branch stable-3.1 from origin.
    Switched to a new branch 'rebaseExample2'
    
  2. Make a commit on the branch.
    $ echo "My Fishtank
    
    Pirateship, Oister shell
    Coconut shell
    ">fishtank.txt
    $ git add fishtank.txt
    $ git commit -m "My brand new fishtank"
    [rebaseExample2 39811d6] My brand new fishtank
     1 file changed, 5 insertions(+)
     create mode 100644 fishtank.txt
    
  3. Try to rebase the branch on top of the rebaseExample branch.
    $ git rebase rebaseExample
    First, rewinding head to replay your work on top of it...
    Applying: My brand new fishtank
    Using index info to reconstruct a base tree...
    <stdin>:12: new blank line at EOF.
    +
    warning: 1 line adds whitespace errors.
    Falling back to patching base and 3-way merge...
    Auto-merging fishtank.txt
    CONFLICT (add/add): Merge conflict in fishtank.txt
    Failed to merge in the changes.
    Patch failed at 0001 My brand new fishtank
    The copy of the patch that failed is found in:
       c:/Users/Rasmus/repos/chapter4/.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".
    
  4. As predicted, we have a conflict. Solve the conflict in your preferred editor. Then, add the file to the index using git add and continue with the rebase.
    $ git add fishtank.txt
    $ git rebase --continue
    Applying: My brand new fishtank
    
  5. We can now check with gitk to see whether our change is rebased on top of the rebaseExample branch, as shown in the following screenshot:
    How to do it

How it works

As we learned from the first example, Git will apply the commits that are not available on the branch you are rebasing to. In our example, it is only our commit, as we made it, that is available on the rebaseExample2 branch.

There's more…

You might have noticed in the output of the failing rebase that you have two extra options for the commit.

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.

The first extra option we have is to totally ignore this patch by skipping it; you can do this using git rebase --skip. In our example, this will cause our branch to be fast-forwarded to the rebaseExample branch. So, both our branches will point to the same commit hash.

The second option is to abort the rebasing. If we choose to do this, then we will go back to the branch as it was prior to starting the rebase. This can be done using git rebase --abort.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset