1

This problem is kinda difficult to describe without sharing the full repo, but I'll try.

I realized I had made a mistake in the last but one commit. I decided to fix it using interactive rebase.

git rebase -i @~~ 

Now I'm taken to Vim, where I change the command in the first line:

pick 80c90b55788 First commit message <-- change 'pick' to 'edit' pick 712be094f96 Second commit message 

Git responds with:

Stopped at 80c90b55788... First commit message You can amend the commit now, with git commit --amend Once you are satisfied with your changes, run git rebase --continue 

I modify the find that needs a fix (I delete the offending line). Then I continue with:

git add <file> git commit --amend git rebase --continue 

Now git detects a conflict in the file I edited. (Two consecutive lines were deleted - one originally in the second commit, the other one that I've deleted in the previous step.) To me, this step looks superfluous, as Git basically does the right thing - it has merged the changes correctly. But it waits for me to stage the changes.

Without any modifications to the file, I do again:

git add <file> git commit --amend git rebase --continue 

Git reponds with:

Successfully rebased and updated refs/heads/mybranch. 

BAM! Now the two commits are merged into one. The result contains changes from both commits. The first commit message is used.

What is the reason? I just wanted to modify the last but one commit and let git to rebase the second commit onto it.

9
  • knittl's answer covers this, but I just want to emphasize that the requirement that you not use --amend on the conflict resolution case is bad user-experience. It's something you have to get used to in Git. Commented Jul 26, 2022 at 3:35
  • @torek I checked the official documentation, --amend is prescribed there: By replacing the command "pick" with the command "edit", you can tell git rebase to stop after applying that commit, so that you can edit the files and/or the commit message, amend the commit, and continue rebasing. Commented Jul 26, 2022 at 12:40
  • Yes: but that's only when using edit (where Git stops, but not because of conflict resolution). Why? Because Git exposes you to the raw internal details of how commits work, instead of providing a proper user experience, that's why. :-) Git works pretty well (which is why it's beaten up so much of its competition), but it has a terrible set of interfaces. Commented Jul 26, 2022 at 20:32
  • In particular, when rebase stops due to a conflict, the new-and-improved commit is not yet made. But when rebase stops because you used the word edit, the new-and-improved commit is made at that point. If you wish to replace that commit using git commit you must use git commit --amend. But when the new-and-improved commit isn't made yet, and you use git commit --amend, you're amending the previous commit (one that was made, or was already there), which is what people call "squashing" or a "fixup" (combining two commits). Commented Jul 26, 2022 at 20:36
  • At some point (long after git rebase --interactive was first written) the rebase code was smartened up enough to do the appropriate kind of git commit for you if and when necessary, but the documentation still describes the original "the programmer must be very careful and precise about which operation he is finishing now" sequence of operations. Probably the documentation should just say "you can now edit your working tree, run git add, and run git rebase --continue" although the internal tests would need to be updated to match. Commented Jul 26, 2022 at 20:40

1 Answer 1

5

If you have conflicts while rebasing, you must not call git commit --amend. Once you have resolved the conflict and staged the changes, call git rebase --continue.

Even with action "edit", the git commit --amend step is redundant. While editing, calling git rebase --continue will automatically amend the commit being currently edited.

Sign up to request clarification or add additional context in comments.

2 Comments

But... but... git says it explicitly: You can amend the commit now, with git commit --amend. Once you are satisfied with your changes, run git rebase --continue.
@peter.slizik Yes, I know. I'm not sure if that's intended behavior or not. But for the conflict case, if you read the message carefully, it never mentions --amend.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.