How do I reword the very first git commit message?
Asked Answered
B

3

152

I have a working tree containing 3 commmits:

➜ ~myproject git:(master) git log

commit a99cce8240495de29254b5df8745e41815db5a75
Author: My Name <[email protected]>
Date:   Thu Aug 16 00:59:05 2012 +0200

    .gitignore edits

commit 5bccda674c7ca51e849741290530a0d48efd69e8
Author: My Name <[email protected]>
Date:   Mon Aug 13 01:36:39 2012 +0200

    Create .gitignore file

commit 6707a66191c84ec6fbf148f8f1c3e8ac83453ae3
Author: My Name <[email protected]>
Date:   Mon Aug 13 01:13:05 2012 +0200

    Initial commit (with a misleading message)

Now I wish to reword the commit message of my first commit (6707a66)

➜ ~myproject git:(master) git rebase -i 6707

(…entering vim)

pick 5bccda6 Create .gitignore file
pick a99cce8 .gitignore edits

# Rebase 6707a66..a99cce8 onto 6707a66
#
# Commands:
#  p, pick = use commit
#  r, reword = use commit, but edit the commit message
#  e, edit = use commit, but stop for amending
#  s, squash = use commit, but meld into previous commit
#  f, fixup = like "squash", but discard this commit's log message
#  x, exec = run command (the rest of the line) using shell
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out

In this case, I wish to correct (reword in git parlance) the commit message in question:

Initial commit (with a misleading message)

…to something appropriate.

Unsurprisingly, my attempt above didn't succeed since the first commit obviously doesn't have any parent commit. (And when you rebase, you need to reference the next oldest commit prior to the one you wish to reword, right?)

The gist of my question, thus, can you achieve this by any other means of doing it?

Breakout answered 16/8, 2012 at 13:11 Comment(7)
Or you could just leave it for all time as a repository tawdry quirk.Phyte
possible duplicate of Changing the message of the first commit? (git)Acierate
^ Very true... I thought I had searched properly for this particular question but that's the same one as mine. There goes a huge amount of perfecting my question's copywriting. :-PBreakout
@hced: :) your copywriting doesn't go to waste - it'll help other people to find a solution in the future, even it it were to get closed as a duplicateAcierate
Anyone who comes across this question may find my answer to Changing the message of the first commit? (git) to be helpful.Brominate
Possible duplicate of Change first commit of project with Git?Mum
Possible duplicate of Edit the root commit in Git?Interdepartmental
B
298

Do git rebase -i --root

(point to root instead of pointing to a specific commit)

This way, the first commit is also included and you can just reword it like any other commit.

The --root option was introduced in Git v1.7.12 (2012). Before then the only option was to use filter-branch or --amend, which is typically harder to do.

Note: see also this similar question and answer.

Buerger answered 10/1, 2014 at 15:48 Comment(0)
S
12

pcreux's gist has a good way to reword the first commit:

# You can't use rebase -i here since it takes the parent commit as argument.
# You can do the following though:
git checkout FIRST_COMMIT_SHA && git commit --amend && git rebase HEAD master
Syphon answered 30/12, 2012 at 13:38 Comment(1)
As of git 1.7.12, git rebase -i --root is the way to go, as suggested by florisla.Syphon
F
11

You can always use git filter-branch --msg-filter:

git filter-branch --msg-filter \
  'test $GIT_COMMIT = '$(git rev-list --reverse master |head -n1)' &&
echo "Nice message" || cat' master
Frendel answered 16/8, 2012 at 13:13 Comment(5)
fork0: That's great, thanks. Curious, is this to be considered "legitimate" practice, for lack of better word. I mean, is it common/recommended to do it like this? Also, can you do this time and time again in cases with faulty commit messages? Reason for asking that is because I first did it with the wrong commit SHA-1, copying your snippet (yours was the latest commit whereas I wanted to change the very first one). After using the command once again, this time with correct SHA-1 (first commit; 6707a66), it barfed on me.Breakout
Well, it is common :) And yes, you can repeat it. If you just add -f it will go ahead and always rewrite the commits of the given branch. The branch reference value from the first time was saved in refs/original/master, before you ran the command.Frendel
Of course, you can just remove (or rename) the saved reference.Frendel
I updated the code to make sure the mistake with copied commit id does not happen. Now the code is even copy-pastable. A word of warning, though: it does not work correctly if there are more than one initial commit (i.e. when you merged two or more unrelated branches)Frendel
@hced: You should be aware that rewriting any commit that's considered to be "published history" is usually a bad idea. In your case, that would mean that you generally shouldn't do this if anyone else would ever have been working on a commit that had your root commit as an ancestor.Acierate

© 2022 - 2024 — McMap. All rights reserved.