Search code examples
gitgit-rewrite-history

Rewording a commit message in a Git repository with a non linear history


I need to change the message of a past commit.

I tried the method described in How to reword a Git commit by its SHA1 hash?, viz. checking out the commit in question, amending its message and rebasing back to the current branch manually. This fails :

$ git checkout dad667e92fcea4c
Note: switching to 'dad667e92fcea4c'.
You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.
(...)
HEAD is now at dad667e Merge branch 'master' of <MESSAGE>

$ git commit --amend
[detached HEAD 650dc88] Merge branch 'master' of master on GitLab
 Author: laroc <[email protected]>
 Date: Tue Mar 5 11:36:02 2019 +0100

$ git rebase HEAD master
Auto-merging src/main/java/ch/SomeClass.java
CONFLICT (content): Merge conflict in src/main/java/SomeClass.java
Auto-merging src/main/java/OtherClass.java
error: could not apply 369e489... OPENSRC-75
hint: Resolve all conflicts manually, mark them as resolved with
hint: "git add/rm <conflicted_files>", then run "git rebase --continue".
hint: You can instead skip this commit: run "git rebase --skip".
hint: To abort and get back to the state before "git rebase", run "git rebase --abort".
Could not apply 369e489... OPENSRC-75
(...)

So I tried another method, decribed in How do I rebase while skipping a particular commit? : git interactive rebase. This fails exacly in the same manner as the first method :

$ git rebase -i HEAD~172
Auto-merging src/main/java/SomeClass.java
CONFLICT (content): Merge conflict in src/main/java/SomeClass.java
Auto-merging src/main/java/OtherClass.java
CONFLICT (content): Merge conflict in src/main/java/OtherClass.java
error: could not apply 369e489... OPENSRC-75
(...)

The trouble seems to stem from the fact that the Git history of the project is not linear: it contains branches, as well as merge commits to solve conflicts made by concurrent developers. It looks like the commands above try to solve again all past conflicts. But I don't want to change anything in the commits' source code - I just want to reword a commit!


Solution

  • Instead of git rebase, I suggest that you

    1. edit the commit message using git replace --edit dad667e92fcea4c,

    2. and then bake the new replacement commit into the history using git filter-repo.

    You can find instructions how to do the second step in this answer.