140

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.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
pahnin
  • 5,367
  • 12
  • 40
  • 57
  • Have a look at my answer in this post: https://stackoverflow.com/questions/48307297/how-to-update-forked-repo-with-original-repo-if-both-forked-repo-and-original-re/48337194 – Abhishek Jan 19 '18 at 08:53

5 Answers5

170

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

Houssam ASSANY
  • 413
  • 4
  • 6
iltempo
  • 15,718
  • 8
  • 61
  • 72
  • 3
    yes rebasing is headache, can I use git pull upstream master? – pahnin Jul 29 '12 at 14:39
  • 2
    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. – iltempo Jul 29 '12 at 14:44
  • 8
    @iitempo You don't need to do a commit. Just a git add is enough to allow the rebase to continue. – enigmaticPhysicist Dec 15 '18 at 01:02
  • 5
    what does `git mergetool` do? – Sam Jul 23 '21 at 01:53
  • @Sam It runs the conflict resolution tool of your choice. This can be a certain command line tool or one offering an app interface. https://git-scm.com/docs/git-mergetool – iltempo Aug 22 '21 at 06:18
  • Can you use `git commit` instead of `git rebase --continue`? – Simon Tran Nov 26 '22 at 19:30
  • I have no idea why this comment gets an upvote. `git mergetool` is a completely user dependent configuration and shouldn't be used as an answer. `git rebase --continue`? Ok, what about the steps before that? As you can see, there are _steps missing_ in this answer. @baur has a very good answer although a bit technichal. – VonSchnauzer Feb 13 '23 at 17:25
  • I am getting '.orig' file for those who have conflicts, how to resolve it ? – pandey_shubham Jun 21 '23 at 12:46
  • You can git ignore .orig files and it will be untracked and not included in add/commit. – DeeJay007 Aug 17 '23 at 11:25
65

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.

Adam
  • 1,796
  • 18
  • 19
  • 3
    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. – jay Nov 08 '21 at 02:13
  • 2
    @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. – Adam Nov 09 '21 at 00:22
  • 2
    What I really like about this answer is that 1) I don't have to squash all my carefully thought out commits and 2) can get through the jungle of commit conflicts without much hassle. Doing "regular" rebase, I would handle each commit's conflict. And if you have 439 commits, that might be a whole lot to go through. With your approach I could handle all the conflicts in one go and then rebase. ---<--@ – VonSchnauzer Feb 13 '23 at 17:29
  • I just did this and it worked* but how is this different to just doing merge instead of rebase? surely its the same result as just doing ```git merge origin/master``` on your branch? *however the rebase -X theirs still had conflicts at two steps with a new file – chrispepper1989 Aug 24 '23 at 11:06
  • @chrispepper1989 merge and rebase result in different position of your commits against base branch. The command `rebase -X theirs` shouldn't raise any conflicts actually. – Adam Aug 25 '23 at 17:57
22

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.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 1
    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: https://help.github.com/articles/resolving-merge-conflicts-after-a-git-rebase/) – Jerther Oct 27 '17 at 19:51
  • Honestly, I wish this was even clearer. For example, it says `mark them as resolved with git add/rm ` - well, should I use `git add` or `git rm`? – Jordan Mitchell Barrett May 10 '22 at 04:53
  • @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 May 10 '22 at 04:55
  • @JordanMitchellBarrett As an example, written yesterday: "[How to Resolve Merge Conflicts in Git – A Practical Guide with Examples](https://www.freecodecamp.org/news/resolve-merge-conflicts-in-git-a-practical-guide/#example-2-the-file-is-removed-at-the-remote-other-branch)" from [Tapas Adhikary](https://twitter.com/tapasadhikary): "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." – VonC May 10 '22 at 08:30
8

If you don't mind squashing into one big commit, easiest way I do it is git reset --soft <rebase-target> from your current branch then git commit.

So for example, you're on your branch and your target to rebase on is origin/master. Assuming you have no unstaged/working changes:

# Merge and fix any conflicts from master
git merge origin/master
# Push your changes in case you mess up and want to start over from remote
git push
# Move HEAD to rebase target, leaving all your work staged
git reset --soft origin/master
# Commit staged changes as giant commit
git commit -m "feat(foo): new feature"
# Confirm your changes, run tests, etc. Then force push
git push -f

You don't have to worry about redoing merge conflicts because the entire difference between your branch and rebase target is moved onto the staging area, all ready to be committed.

chaz
  • 568
  • 1
  • 8
  • 22
  • In simple words, this is a sqaush merge into master. – IsmailS May 11 '23 at 12:45
  • To make it more understandable, `git checkout -b copy origin/master` > `git merge your_branch`, resolve conflicts and commit. `copy` is now your branch with 1 commit on top of master. – IsmailS May 11 '23 at 12:50
0

In some overcomplicated cases with deleted, renamed, changed files possible lifesaver may be to use empty "buffer" commit with all files deleted. It gives more flexibility on it's own and additionally next commit after empty then will became more reliable and snapshot-similar.

halt9k
  • 527
  • 4
  • 13
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/32428680) – OznOg Aug 12 '22 at 10:16