git amend specific - How to modify existing, unpushed commits?

13 Answers

git commit --amend -m "your new message"
change message of

I wrote the wrong thing in a commit message. Alternatively, I've forgotten to include some files.

How can I change the commit message/files? The commit has not been pushed yet.

To amend the previous commit, make the changes you want and stage those changes, and then run

git commit --amend

This will open a file in your text editor representing your new commit message. It starts out populated with the text from your old commit message. Change the commit message as you want, then save the file and quit your editor to finish.

To amend the previous commit and keep the same log message, run

git commit --amend -C HEAD

To fix the previous commit by removing it entirely, run

git reset --hard HEAD^

If you want to edit more than one commit message, run

git rebase -i HEAD~commit_count

(Replace commit_count with number of commits that you want to edit.) This command launches your editor. Mark the first commit (the one that you want to change) as “edit” instead of “pick”, then save and exit your editor. Make the change you want to commit and then run

git commit --amend
git rebase --continue

Note: You can "Make the change you want" also from the editor opened by git commit --amend

You also can use git filter-branch for that.

git filter-branch -f --msg-filter "sed 's/errror/error/'" $flawed_commit..HEAD

It's not as easy as a trivial git commit --amend, but it's especially useful, if you already have some merges after your erroneous commit message.

Note that this will try to rewrite EVERY commit between HEAD and the flawed commit, so you should choose your msg-filter command very wise ;-)

If you are using the Git GUI tool, there is a button named amend last commit. Click on that button and then it will display your last commit files and message. Just edit that message and you can commit it with new commit message.

Or use this command from a console/terminal:

git commit -a --amend -m "My new commit message"

  1. If you only want to modify your last commit message, then do:

    git commit --amend

    That will drop you into your text exitor and let you change the last commit message.

  2. If you want to change the last 3 commit messages, or any of the commit messages up to that point, supply HEAD~3 to the git rebase -i command:

    git rebase -i HEAD~3


git commit --amend

To understand it in detail, an excellent post is 4. Rewriting Git History. It also talks about when not to use git commit --amend.

If you are using the Git GUI, you can amend the last commit which hasn't been pushed with:

Commit/Amend Last Commit

I use the Git GUI as much as I can, and that gives you the option to amend the last commit:

Also, git rebase -i origin/masteris a nice mantra that will always present you with the commits you have done on top of master, and give you the option to amend, delete, reorder or squash. No need to get hold of that hash first.

If you just want to edit the latest commit use:

git commit --amend


git commit --amend -m 'one line message'

But if you want to edit several commits in a row you should use rebasing instead:

git rebase -i <hash of one commit before the wrong commit>

In a file like the one above write edit/e or one of the other option and hit save and exit.

Now you'll be at the first wrong commit. Make changes in the files, and they'll be automatically staged for you. Type

git commit --amend

save and exit that and type

git rebase --continue 

to move to next selection until finished with all your selections.

Note that these things change all your SHA hashes after that particular commit.

If you only want to change your last message you should use the --only flag or its shortcut -o with commit --amend:

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

This ensures that you don't accidentally enhance your commit with staged stuff. Of course it's best to have a proper $EDITOR configuration. Then you can leave the -m option out, and git will pre-fill the commit message with the old one. In this way it can be easily edited.

On this question there are a lot of answers but none of them explains in super detail how to change older commit messages using VIM. I was stuck trying to do this myself, so here I'll write down in detail how I did this especially for people who have no experience in VIM!

I wanted to change my five latest commits that I already pushed to the server. This is quite 'dangerous' cause if someone else already pulled from this you can mess things up by changing the commit messages. However when you’re working on your own little branch and are sure no one pulled it you can change it like this:

Let's say you want to change your five latest commits, then you type this in the terminal:

git rebase -i HEAD~5 *Where 5 is the number of commit messages you want to change. (so if you want to change the 10th to last commit you type in 10)

This command will get you into VIM there you can ‘edit’ your commit history. You’ll see your last 5 commits at the top like this:

pick <commit hash> commit message

Instead of pick you need to write reword. You can do this in VIM by typing in i, that makes you go in to INSERT-mode. (You see that you’re in insert mode by the word INSERT at the bottom) For the commits you want to change type in reword instead of pick

Then you need to save and quit this screen, you do that by first going in to ‘command-mode’ by pressing the esc button. (you can check that you’re in command-mode if the word INSERT at the bottom has disappeared) Then you can type in a command by typing :, the command to save and quit is wq. So if you type in :wq you’re ont he right track.

Then VIM wil go over every commit message you want to reword, here you can actually change the commit messages. You’ll do this by going into INSERT-mode, changing the commit message, going into the command-mode and save and quit. Do this 5 times and you’re out of VIM!

Then, if you already pushed your wrong commits, you need to git push --force to overwrite them. Remember that git push --force is quite a dangerous thing to do, so make sure that no-one pulled from the server since you pushed your wrong commits!

Now you have changed your commit messages!

(As you see I'm not that experienced in VIM so if I used wrong 'lingo' to explain what's happening, feel free to correct me!)

You can use git-rebase-reword

It is designed to edit any commit (not just last) same way as commit --amend

$ git rebase-reword <commit-or-refname>

It is named after the action on rebase interactive to amend a commit: "reword". See this post and man -section interactive mode-


$ git rebase-reword b68f560
$ git rebase-reword HEAD^

I like to use the following:

  1. git status
  2. git add --all
  3. git commit -am "message goes here about the change"
  4. git pull <origin master>
  5. git push <origin master>