Skip to content

Instantly share code, notes, and snippets.

  • Save ugultopu/61b2b48e26ce41303cc0d7d286fa07d0 to your computer and use it in GitHub Desktop.
Save ugultopu/61b2b48e26ce41303cc0d7d286fa07d0 to your computer and use it in GitHub Desktop.

Summary

After fixing a merge conflict and adding the fixed file(s) to the index by running git add path-to-the-file when running git-rebase, DON'T RUN git commit --amend. Just run git rebase --continue.

If you run git commit --amend instead of git rebase --continue, the commit in which the conflict occurred will disappear from the commit history. Hence, DON'T run git commit --amend after fixing a merge conflict. Run git rebase --continue.

Details

Let's say that when using git-rebase, you amended a previous commit and you ran git rebase --continue. Let's say that a merge conflict occurred. In this case, git-rebase will pause to let you fix the merge conflict. To fix it, you need to:

  • Go through each conflicted file and edit them to fix them.
  • Add them to the index by running git add path-to-the-file.

Now, what should you do? You should run git commit --amend for the fixed merge conflicts to appear in commit history right? WRONG! Instead of git commit --amend, you should just run git rebase --continue, which will amend the conflicted commit and continue with the rebase. Even the rebase status tells you this:

$ git rebase --continue
Auto-merging README.md
CONFLICT (content): Merge conflict in README.md
error: could not apply 4ed2dd6... Specify prerequisites for the project
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".
Could not apply 4ed2dd6... Specify prerequisites for the project
$ 

Now, you should go to the conflicted file, which is README.md in this case, fix the merge conflict and then run git add README.md. The following is after editing README.md to fix the merge conflict:

$ git add README.md 
$ git status
interactive rebase in progress; onto 6d60bf8
Last commands done (9 commands done):
  pick 7ae29df Don't run server automatically after setting up
  pick 4ed2dd6 Specify prerequisites for the project
  (see more in file .git/rebase-merge/done)
Next commands to do (38 remaining commands):
  pick a88878c Add the back end machine to the list of allowed hosts
  pick 75e29c6 Create configuration files
  (use "git rebase --edit-todo" to view and edit)
You are currently rebasing branch 'master' on '6d60bf8'.
  (all conflicts fixed: run "git rebase --continue")

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
  modified:   README.md

$ 

As it says, we run git rebase --continue. When we run it, Git opens our configured text editor to edit the commit message of the commit whose commit message was "Specify prerequisites for the project". We just exit from the text editor, leaving the message as is, since all we want is to pick that commit, not change its message.

Upon exiting from the text editor, Git applies the specified message (which is the same as the original message, since we left it as is), amends that commit with the conflict fixes and continues the rebase. The following is the terminal output after exiting the text editor:

[detached HEAD b427479] Specify prerequisites for the project
Author: ugultopu <[email protected]>
1 file changed, 14 insertions(+), 13 deletions(-)
rewrite README.md (94%)
Successfully rebased and updated refs/heads/master.
$ 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment