git rebase merge conflict

217,431

Solution 1

Rebasing can be a real headache. You have to resolve the merge conflicts and continue rebasing. For example you can use the merge tool (which differs depending on your settings)

git mergetool

Then add your changes and go on

git rebase --continue

Good luck

Solution 2

If you have a lot of commits to rebase, and some part of them are giving conflicts, that really hurts. But I can suggest a less-known approach how to "squash all the conflicts".

First, checkout temp branch and start standard merge

git checkout -b temp
git merge origin/master

You will have to resolve conflicts, but only once and only real ones. Then stage all files and finish merge.

git commit -m "Merge branch 'origin/master' into 'temp'"

Then return to your branch (let it be alpha) and start rebase, but with automatical resolving any conflicts.

git checkout alpha
git rebase origin/master -X theirs

Branch has been rebased, but project is probably in invalid state. That's OK, we have one final step. We just need to restore project state, so it will be exact as on branch 'temp'. Technically we just need to copy its tree (folder state) via low-level command git commit-tree. Plus merging into current branch just created commit.

git merge --ff $(git commit-tree temp^{tree} -m "Fix after rebase" -p HEAD)

And delete temporary branch

git branch -D temp

That's all. We did a rebase via hidden merge.

Also I wrote a script, so that can be done in a dialog manner, you can find it here.

Solution 3

Note: with Git 2.14.x/2.15 (Q3 2017), the git rebase message in case of conflicts will be clearer.

See commit 5fdacc1 (16 Jul 2017) by William Duclot (williamdclt).
(Merged by Junio C Hamano -- gitster -- in commit 076eeec, 11 Aug 2017)

rebase: make resolve message clearer for inexperienced users

Before:

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"

After:

Resolve all conflicts manually, 
mark them as resolved with git add/rm <conflicted_files>
then run "git rebase --continue".

You can instead skip this commit: run "git rebase --skip".
To abort and get back to the state before "git rebase", run "git rebase --abort".')

The git UI can be improved by addressing the error messages to those they help: inexperienced and casual git users.
To this intent, it is helpful to make sure the terms used in those messages can be understood by this segment of users, and that they guide them to resolve the problem.

In particular, failure to apply a patch during a git rebase is a common problem that can be very destabilizing for the inexperienced user.
It is important to lead them toward the resolution of the conflict (which is a 3-steps process, thus complex) and reassure them that they can escape a situation they can't handle with "--abort".
This commit answer those two points by detailing the resolution process and by avoiding cryptic git linguo.

Share:
217,431

Related videos on Youtube

pahnin
Author by

pahnin

Programmer (c,cpp,javascrip,php,python) recently fell in love with ruby. I hope I'm gonna end up being rubyist.

Updated on May 05, 2022

Comments

  • pahnin
    pahnin about 2 years

    I forked a github repo and worked on my github repo.
    I have made pull-requests and it was completed.

    After that the upstream had some more commits so now I want to rebase, I guess thats what I have to do.
    But I'm getting these merge conflicts:

    First, rewinding head to replay your work on top of it...
    Applying: Issue 135 homepage refresh
    Using index info to reconstruct a base tree...
    <stdin>:17: trailing whitespace.
          %h4 
    warning: 1 line adds whitespace errors.
    Falling back to patching base and 3-way merge...
    Auto-merging app/views/layouts/application.html.haml
    CONFLICT (content): Merge conflict in app/views/layouts/application.html.haml
    Auto-merging app/views/home/index.html.haml
    CONFLICT (content): Merge conflict in app/views/home/index.html.haml
    Auto-merging app/views/home/_group_projects.html.haml
    CONFLICT (content): Merge conflict in app/views/home/_group_projects.html.haml
    Failed to merge in the changes.
    Patch failed at 0001 Issue 135 homepage refresh
    
    When you have resolved this problem run "git rebase --continue".
    If you would prefer to skip this patch, instead run "git rebase --skip".
    To check out the original branch and stop rebasing run "git rebase --abort".
    

    I don't know how to fix these, please help.

  • pahnin
    pahnin almost 12 years
    yes rebasing is headache, can I use git pull upstream master?
  • iltempo
    iltempo almost 12 years
    Yes you can try that. The difference is that your commits are not put on top of those from the upstream then. Potentially there are less merge conflicts.
  • Jerther
    Jerther over 6 years
    Nice! The help page says to resolve the conflict by committing the change, but no! Here we need to skip the commit and just continue the merge instead! (help page: help.github.com/articles/…)
  • enigmaticPhysicist
    enigmaticPhysicist over 5 years
    @iitempo You don't need to do a commit. Just a git add is enough to allow the rebase to continue.
  • Sam
    Sam almost 3 years
    what does git mergetool do?
  • iltempo
    iltempo over 2 years
    @Sam It runs the conflict resolution tool of your choice. This can be a certain command line tool or one offering an app interface. git-scm.com/docs/git-mergetool
  • jay
    jay over 2 years
    this just saved me a massive headache, thank you! What do you mean by "project is probably in invalid state"? I did everything up to and including git rebase origin/master -X theirs and everything looks fine... I'm reluctant to do the final merge without understanding whether it's necessary.
  • baur
    baur over 2 years
    @jay that command git rebase origin/master -X theirs does just a mechanical merge conflicts resolution. Sometimes it can be enough, and nothing else is needed. But sometimes project even can't compile after it. That's why the additional commit helps restore the correct rebase result, which has been previously done with a traditional merge. I can suggest you do the final merge and see if it introduces changes or not.
  • Jordan Mitchell Barrett
    Jordan Mitchell Barrett about 2 years
    Honestly, I wish this was even clearer. For example, it says mark them as resolved with git add/rm <conflicted_files> - well, should I use git add or git rm?
  • VonC
    VonC about 2 years
    @JordanMitchellBarrett add if you want to keep that file as part of the rebase, rm if you want to get rid of that file.
  • VonC
    VonC about 2 years
    @JordanMitchellBarrett As an example, written yesterday: "How to Resolve Merge Conflicts in Git – A Practical Guide with Examples" from Tapas Adhikary: "In removed file merge conflicts, a dev deletes a file in one branch while another dev edits the same file in another branch. In this case, you need to decide if you want to keep the file or if it was right to delete it."