-3

I have a situation where a committed file is reviewed by the bug tracker tool and once it reviews the commit message has to change.

So my question is, Is there a way to change the commit message of a file that is already committed into my local git repository?

Iowa
  • 2,171
  • 4
  • 22
  • 31
  • 2
    Asked earlier you could have earned a lot of rep: http://stackoverflow.com/questions/179123/how-do-i-edit-an-incorrect-commit-message-in-git – Kai Huppmann Aug 14 '13 at 11:26
  • 1
    Usually you don't want to change the commit message, because that changes the commit ID, with all sorts of complex consequences. Rather, use `git notes`. – sleske Aug 14 '13 at 11:40
  • 1
    @sleske the original poster wants to change the message for a local commit, which happens all the time, it's fine, as long as the commit isn't shared by anyone else. –  Aug 14 '13 at 12:10
  • @Cupcake: True. The mention of "reviewed by the bug tracker tool" made me think that this probably happens after pushing the commit to some central server, because a bug tracker usually runs on a central server, and cannot access developer's local repos. I may have guessed wrong, of course. – sleske Aug 14 '13 at 14:31

1 Answers1

7

If your commit is at the tip (no other changes committed since) you can:

git commit --amend -m "New commit message"

If other changes have been committed since:

git rebase -i <commit-to-change>~

This will fire up an editor. Replace pick with edit on the correct commit entry, save and exit. Then:

git commit --amend -m "New commit message"
git rebase --continue
cforbish
  • 8,567
  • 3
  • 28
  • 32
  • Is there a way to do it without the aid of the editor, by passing the commit message as an argument directly to the git rebase -i command. Like for example git rebase -i should replace the commit message of that particular commit. – Iowa Aug 14 '13 at 14:38
  • `git rebase -i` is what you would use if you had more commits since the one you wanted to edit. It allows for editing more than one commit at once. I have written scripts that do similar things to what you are looking for, I gave you simplest answer to your question. – cforbish Aug 15 '13 at 02:24
  • assuming the commit is already done on the client machine, what i am looking for is a command/script which accepts commitid and changes the commit message for that particular commit. Can you please share the script if you have it? – Iowa Aug 15 '13 at 10:30
  • is it also possible to change the commit message without changing the commit id? – Iowa Aug 15 '13 at 10:36
  • It is not possible to change the commit message without changing the commit id. The commit id is generated and the log message is a part of the formula. – cforbish Aug 15 '13 at 12:21
  • The scripts that I have are very manual in nature when it comes to resolving conflicts and dealing with merges. The closest one that I have will do a `git reset --hard `, which if there are conflicts, you cannot get back to the original state. In fact if my task was to do what you are asking, I use the `git rebase -i` method that I mentioned. With `git rebase -i` if I want to just go back to the way things were I can just do `git rebase --abort`. – cforbish Aug 15 '13 at 13:09