[git] How to modify existing, unpushed commits?


Answers

git commit --amend -m "your new message"
Question

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.




If you just want to edit the latest commit use:

git commit --amend

or

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.




  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
    



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-

Examples:

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



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"



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>



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




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 are using the Git GUI, you can amend the last commit which hasn't been pushed with:

Commit/Amend Last Commit



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 ;-)




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!)




For anyone looking for a Windows/Mac GUI to edit more than just the latest message (for unpushed commits), I'd recommend SourceTree.

Update: I since discovered the procedure below is described in further detail, with better screenshots and additional steps for when the commits have been pushed in this answer.

Procedure:

  1. Make sure you have committed or stashed all current changes (i.e. so there are no files listed in the "File Status" tab - it won't work otherwise).
  2. In the "Log / History" tab, right click on the entry with an adjoining line in the graph one below the commit(s) you wish to edit and select "Rebase children of <commit ref> interactively..."
  3. Select the whole row for the commit message you wish to change (i.e. click on the "Message" column).
  4. Click the "Edit Message" button.
  5. Edit the message as desired in the dialog that comes up and click OK.
  6. Repeat steps 3-4 if there are other messages to change.
  7. Click OK - rebasing will commence - if all is well, the output will end "Completed successfully".



Use

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.




Related