25

Here is the scenario:

  • I am rebasing a branch in git, I had conflicts and just resolved them (did git add and everything).
  • Normally, I would just run git rebase --continue to proceed with the rebase
  • I want to edit the commit message of this commit, to explain the changes I made when resolving the conflict

I don't usually do this, but this particular conflict required some serious recoding, which I feel needs to be documented.

How do I do this? I'm hoping there is a git rebase --continue --let-me-edit-the-message-first command of some sort, but I have not been able to find it in the documentation

I know that:

  • I could run git commit, manually copying the existing commit message and then modifying it. I don't want to do this. git rebase must have the commit message stored somewhere, because when it commits it will put the original message in. I just want to insert my edits in between.
  • I could just run git rebase --continue, and then use git rebase -i to go back and edit the commit messages in question later on. This requires me to remember what I changed in which commits throughout the entire (potentially long) rebase process. Yuck.

I'm a little surprised that I couldn't find an answer to this already... I'm hooping there's a way to do this. If there is an answer for this I had difficulty finding it. I found things like confusion that amounts to the need for git add or git rm after resolving changes (like this), general git rebase wisdom (like this and this), and lots of questions that seem very specific to a particular case (like this).

Since I'm so surprised I couldn't find this answer, I have a secondary question: is there a fundamentally better way to document changes made during rebase?

5
  • 3
    Till you asked I was sure Git just does this (rebase, conflict, add, continue -> editor). Have you ever seen a commit message editor when rebasing? Which Git version are you talking about? Commented Dec 24, 2014 at 20:05
  • I don't believe I have ever seen the commit message editor after running git rebase --continue. I am working with git version 1.9.1, but I would be surprised (though not too surprised) if there were huge differences across recent versions. Commented Dec 24, 2014 at 20:39
  • I've not worked with Git 1.9.x so far, I guess, so now I can only speculate: maybe your GIT_EDITOR environment or core.editor config is messed up? Do you see any error after issuing git rebase --continue? Just to be on the safe side: which rebase mode are you doing? Rebase onto or against a branch, interactive rebase to squash/delete/reword/edit commits? Please complete the scenario in your post together with the version by editing (this will make me upvoting your post too if it's complete ;). Commented Dec 24, 2014 at 20:52
  • I just confirmed again that upon running git rebase --continue I do not get an editor. I do get an editor at other times (which running git commit for instance). GIT_EDITOR is not set for me, and git config core.editor produces no output (and an exit code of 1) Commented Dec 24, 2014 at 22:07
  • I think you'll need to use the approach from your second bullet point. As you say, it's unusual for conflict resolutions to need documenting in the commit message (you're not changing what the commit is for, and the code change can speak for itself), so this is a bit of an edge case. If you're expecting to have lots of conflicts when rebasing in future then you might find it easier to use a branch/merge approach instead. Commented Dec 24, 2014 at 23:50

3 Answers 3

11

After coming to this question with the exact same problem and being disappointed with the answer, I think I came up with a reasonably elegant solution for this, but see my note at the end.

git commit -t .git/rebase-apply/msg-clean git rebase --skip 

msg-clean appears to contain the original commit message, so doing a commit using it as a template gives you the original message in an editor. After your commit, the skip moves the rebase along without trying to re-apply the commit you just made.

NOTE: I'm no guru, so someone please verify this. It seemed to work with a test repo I slapped together, but I wasn't brave enough to try it on my real work repo with many hours of conflict resolution work. Please don't hold me responsible if this blows something up.

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

6 Comments

With Git 2.9.3, .git/rebase-apply/msg-clean does not exist during a rebase, neither does a file msg-clean exist anywhere else.
.git/rebase-apply/final-commit
At least as-of git version 2.20.1 (Apple Git-117) (output by git --version), git commit -t .git/rebase-apply/final-commit and then git rebase --skip seemed to work perfectly.
Given when you're rebasing another author's commits, won't this commit's author be different from the original's?
@TimLandscheidt good point. I have Git v2.34.1 and it has file .git/rebase-merge/message. I use it to change the message from script and trigger GIT_EDITOR=true grbc to continue rebase without vim popping up for message confirmation. This is good for automation. In other cases vim is great to tweak the message.
|
5

I never realized that this is an issue. (My Git Bash at work is a different version, so maybe it works differently.)

Either way, until you find a solution to that problem you can do the following:

git reset --hard HEAD git cherry-pick $(head -1 .git/rebase-apply/patch) 

Resolve conflicts

git commit git rebase --skip 

You can make aliases as well:

git config alias.rebase2cherry "! git reset --hard HEAD; git cherry-pick $(head -1 .git/rebase-apply/patch)" git config alias.commitskip "! git commit; git rebase --skip" 

Interactive rebase does bring up an edit message screen when you have conflicts even if you chose pick for that commit. However, it does not use the same format as the cherry pick with the conflicting files included by default. (It is not even included in the commented out section.)

For interactive a different command is needed to emulate cherry pick

git cp $(cat .git/rebase-merge/stopped-sha) 

and if you are using the --merge option you need to enter

git cherry-pick $(cat $DIR/rebase-merge/current)

Therefore, to simplify things it would be best to use this alias:

git config alias.rebase2cherry '! git reset --hard HEAD; DIR=$(git rev-parse --git-dir); if [[ -f $DIR/rebase-apply/patch ]]; then git cherry-pick $(head -1 $DIR/rebase-apply/patch); elif [[ -f $DIR/rebase-merge/current ]] ; then git cherry-pick $(cat $DIR/rebase-merge/current); else git cherry-pick $(cat $DIR/rebase-merge/stopped-sha); fi' 

3 Comments

I looked through the source code. I believe that rebase never calls an editor, even in earlier versions.
I can confirm that this had the desired effect for me. That git reset --hard in the non-interactive case is unfortunate, as I would typically realize I need to do this after the fact, and so the things I would be discarding would be my valuable edits. I suppose that using git diff --cached > changes.patch would allow me to preserve those changes for reintroduction after the cherry pick.
You could do git read-tree -u --reset ORIG_HEAD` when the cherry-pick asks you to resolve conflicts.
0

In current version git version 2.40.1, it does show editor and asks if you want to edit the commit message which is causing the conflict.

git switch feat git rebase master 

master and feat snapshot before the rebase

master and feat snapshot after the rebase

We can see that the commit msg on feat branch is changed from "f3 in main.txt" to "f3 in main.txt | rebase msg". I added the " | rebase msg" part in the commit msg when the vim editor opened.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.