Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git rebase: conflicts keep blocking progress

Tags:

git

rebase

People also ask

How do I fix conflicts in rebase?

If the change that you submitted has a merge conflict, you need to manually resolve it using git rebase. Rebasing is used to integrate changes from one branch into another to resolve conflicts when multiple commits happen on the same file. Never do a rebase on public (master) branches. You submit a change.

Can git rebase cause conflicts?

When you perform a git rebase operation, you're typically moving commits around. Because of this, you might get into a situation where a merge conflict is introduced. That means that two of your commits modified the same line in the same file, and Git doesn't know which change to apply.


I encountered a similar problem with a rebase. My problem was caused because one of my commit only changed a file, and when resolving, I discarded the change introduced in this commit. I was able to solve my problem by skipping the corresponding commit (git rebase --skip).

You can reproduce this problem in a test repository. First create the repository.

$ mkdir failing-merge
$ cd failing-merge
$ git init
Initialized empty Git repository in $HOME/failing-merge/.git/

Then commit the original content of version.txt in master.

$ echo v1.4-alpha-02 > version.txt
$ git add version.txt
$ git commit -m initial
[master (root-commit) 2eef0a5] initial
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 version.txt

Create the v4 branch and change the content of version.txt.

$ git checkout -b v4
Switched to a new branch 'v4'
$ echo v1.4-alpha-03 > version.txt
$ git add version.txt
$ git commit -m v4
[v4 1ef8c9b] v4
 1 files changed, 1 insertions(+), 1 deletions(-)

Go back to master and change the content of version.txt so that there will be a conflit during the rebase.

$ git checkout master
Switched to branch 'master'
$ echo v1.4-alpha-04 > version.txt
$ git add version.txt
$ git commit -m master
[master 7313eb3] master
 1 files changed, 1 insertions(+), 1 deletions(-)

Switch back to v4 branch and try to rebase. It fails with a conflit in version.txt as planned.

$ git checkout v4
Switched to branch 'v4'
$ git rebase master
First, rewinding head to replay your work on top of it...
Applying: v4
Using index info to reconstruct a base tree...
Falling back to patching base and 3-way merge...
Auto-merging version.txt
CONFLICT (content): Merge conflict in version.txt
Recorded preimage for 'version.txt'
Failed to merge in the changes.
Patch failed at 0001 v4

When you have resolved this problem run "git rebase --continue".
If you would prefer to skip this patch, instead run "git rebase --skip".
To restore the original branch and stop rebasing run "git rebase --abort".
$ cat version.txt
<<<<<<< HEAD
v1.4-alpha-04
=======
v1.4-alpha-03
>>>>>>> v4

We resolve the conflict by selecting the master content of version.txt. We add the file and try to continue our rebase.

$ echo v1.4-alpha-04 > version.txt
$ git add version.txt
$ git rebase --continue 
Applying: v4
No changes - did you forget to use 'git add'?
If there is nothing left to stage, chances are that something else
already introduced the same changes; you might want to skip this patch.

When you have resolved this problem run "git rebase --continue".
If you would prefer to skip this patch, instead run "git rebase --skip".
To restore the original branch and stop rebasing run "git rebase --abort".

It fails ! Let's see what changes git think there is in our repository.

$ git status
# Not currently on any branch.
nothing to commit (working directory clean)

Ah ah, there is no change. If you read in detail the previous error message, git informed us of this and recommended to use git rebase --skip. He told us "If there is nothing left to stage, chances are that something else already introduced the same changes; you might want to skip this patch." So we just skip the commit and the rebase succeed.

$ git rebase --skip
HEAD is now at 7313eb3 master

Word of caution: Please note that git rebase --skip will completely drop the commit that git tried to rebase. In our case, this should be okay since git is complaining this is an empty commit. If you think you've lost changes once the rebase is complete, you can use git reflog to get the commit id of your repository before the rebase, and use git reset --hard to get your depot back in that state (this is another destructive operation).


Quoting from here: http://wholemeal.co.nz/node/9

Huh?!? No, I didn't forget to use git add, I did it ... like ... 2 seconds ago!

Turns out that because there is no change from the patch git suspects something has gone wrong. Git expects a patch to have been applied, but the file has remained unchanged.

The error message is not very intuitive, but it does contain the answer. We just need to tell rebase to skip this patch. It's also not necessary to fix the conflict markers in the file. You will end up with the file version from the branch you are rebasing on.

$ git rebase --skip

That error message is a result of your git commit -a -m "merged". If you just fix up the file, then run git add <file>, and git rebase --continue, it should work fine. git rebase --continue is trying to do a commit, but finding that there are no pending changes to commit (because you committed them already).


Change app/views/common/version.txt to

v1.4-alpha-01

At this point in the rebase, remember that you are resolving merge conflicts to show the progression of the non-master branch.

So, in rebasing from

      A---B---C topic
     /
D---E---F---G master

to

              A*--B*--C* topic
             /
D---E---F---G master

the conflict you are resolving is in how to create A* on the topic branch.

So after doing git rebase --abort, the commands should be

git checkout topic
git rebase master
< make edits to resolve conflicts >
git add .
git rebase --continue

The behavior you're seeing is not what I would expect from a typical rebase with just this conflict. Consider using a separate branch to do this rebase (especially if you've already pushed the commits remotely that you're fast-forwarding). Also, git mergetool can be helpful for resolving conflicts and remembering to issue a git add.

In this minimal example, the rebase works as expected. Can you provide an example that shows the behavior you're seeing?

#!/bin/bash

cd /tmp
mkdir rebasetest
cd rebasetest
git init
echo 'v1.0' > version.txt
git add version.txt
git commit -m 'initial commit'
git checkout -b v4
echo 'v1.4-alpha-01' > version.txt
git add version.txt
git commit -m 'created v4'
git checkout master
git merge v4
echo 'v1.4-alpha-01-rc1' > version.txt
git add version.txt
git commit -m 'upped version on master to v1.4-alpha-01-rc1'
git checkout v4
echo 'v1.4-alpha-02' > version.txt
git add version.txt
git commit -m 'starting work on alpha-02'

git rebase master
echo 'v1.4-alpha-02' > version.txt
git add version.txt
git rebase --continue

Here are some ideas:

  • Before starting the rebase, make sure you aren't in the middle of a rebase or am. Do: rm -rf .git/rebase-apply
    • if you use Bash, enhance your bash prompt so it always presents this information automatically
  • One bit you mentioned I didn't understand: "and then try to carry on: at first i try a commit:"... why commit? In the middle of a rebase I think you should only "git add" after you tidy up or "git rm" to toss changes or confirm a file deletion. Maybe that messed something up?
  • try a merge instead of a rebase
  • try some of Ethan Rowe's ideas