LAMagnotti LAMagnotti - 1 year ago 81
Ruby Question

Renaming an Old Commit in Git

I spelled some words wrong in my last commit message and want to change those words. They were not from the last commit,(5 commits ago) so amend doesn't work here I don't believe. What would be the easiest way to fix this mistake?


Answer Source

Identify the commit you want to change with git log --oneline.

$ git log --oneline
5ebe0d0 foo
66096c8 bar
d83d7a7 *typo*
a9f4c59 baz

Then do an interactive rebase onto the parent of that commit (identified with the ^).

$ git rebase --interactive 'd83d7a7^'

This should fire up the default editor – which, unless you've changed it, is vim.

(This is for those less comfortable with vim.)

Press cw and type the word edit. Press esc and type :wq to save and quit the file.

Now all you have to do is git commit --amend, and you'll be prompted to edit your commit message. Then finish the rebase with git rebase --continue.

NOTE: if you have any merge history, this will flatten it. If you really don't want this, use the --preserve-merges option with git rebase (as well as --interactive).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download