specific - git commit message editor




How to modify existing, unpushed commits? (18)

Amending the most recent commit message

git commit --amend

will open your editor, allowing you to change the commit message of the most recent commit. Additionally, you can set the commit message directly in the command line with:

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

…however, this can make multi-line commit messages or small corrections more cumbersome to enter.

Make sure you don't have any working copy changes staged before doing this or they will get committed too. (Unstaged changes will not get committed.)

Changing the message of a commit that you've already pushed to your remote branch

If you've already pushed your commit up to your remote branch, then you'll need to force push the commit with:

git push <remote> <branch> --force
# Or
git push <remote> <branch> -f

Warning: force-pushing will overwrite the remote branch with the state of your local one. If there are commits on the remote branch that you don't have in your local branch, you will lose those commits.

Warning: be cautious about amending commits that you have already shared with other people. Amending commits essentially rewrites them to have different SHA IDs, which poses a problem if other people have copies of the old commit that you've rewritten. Anyone who has a copy of the old commit will need to synchronize their work with your newly re-written commit, which can sometimes be difficult, so make sure you coordinate with others when attempting to rewrite shared commit history, or just avoid rewriting shared commits altogether.


Use interactive rebase

Another option is to use interactive rebase.
This allows you to edit any message you want to update even if it's not the latest message.

In order to do a git squash, follow these steps:

// X is the number of commits to the last commit you want to be able to edit
git rebase -i HEAD~X

Once you squash your commits - choose the e/r for editing the message

Important note about Interactive rebase

When you use the git rebase -i HEAD~X there can be more than X commits. Git will "collect" all the commits in the last X commits and if there was a merge somewhere in between that range you will see all the commits as well so the outcome will be X+.

Good tip:

If you have to do it for more than a single branch and you might face conflicts when amending the content, set up git rerere and let git resolve those conflicts automatically for you.


🔥 Hot-tip

You can also write a small bash function to amend the last git commit message and push it to the remote. Helps me all the time. Here's the function; put it in your .bashrc or similar .zshrc file and reload your shell.

# Amend the last commit message
# Push the changes to remote by force
# USAGE: gamend "Your New Commit Msg"
function gamend() {
    git commit --amend -m "[email protected]"
    git push --force
}

Documentation

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.


Amend

You have a couple of options here. You can do

git commit --amend

as long as it's your last commit.

Interactive rebase

Otherwise if it's not your last commit you can do an interactive rebase,

git rebase -i [branched_from] [hash before commit]

Then inside the interactive rebase you simply add edit to that commit. When it comes up do a git commit --amend and modify the commit message. If you want to roll back before that commit point you could also use git reflog and just delete that commit. Then you just do a git commit again.


As already mentioned, git commit --amend is the way to overwrite the last commit. One note: if you would like to also overwrite the files, the command would be

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

For anyone looking for a Windows/Mac GUI to help with editing older messages (i.e. not just the latest message), I'd recommend SourceTree. The steps to follow are below.

For commits that haven't yet been pushed to a remote:

  1. Make sure you've 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 then click OK.
  6. Repeat steps 3-4 if there are other commit messages to change.
  7. Click OK: Rebasing will commence. If all is well, the output will end "Completed successfully".

...Or... for commits that have already been pushed:

Follow the steps in this answer, which are similar to above but require a further command to be run from the command line to force-push the branch - read it all and apply the necessary caution!


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>

I prefer this way.

git commit --amend -c <commit ID>

Otherwise, there will be a new commit with a new commit ID


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 it's your last commit, just amend the commit:

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

(using the -o (--only) flag to make sure you change only the commit message)


If it's a buried commit, use the awesome interactive rebase:

git rebase -i @~9   # Show the last 9 commits in a text editor

Find the commit you want, change pick to r (reword), and save and close the file. Done!



Miniature vim tutorial (or, how to rebase with only 8 keystrokes 3jcwrEscZZ):

  • Run vimtutor if you have time
  • hjkl correspond to movement keys
  • All commands can be prefixed with a "range", e.g. 3j moves down 3 lines
  • i to enter insert mode — text you type will appear in the file
  • Esc or Ctrlc to exit insert mode and return to "normal" mode
  • u to undo
  • Ctrlr to redo
  • dd, dw, dl to delete a line, word, or letter, respectively
  • cc, cw, cl to change a line, word, or letter, respectively (same as ddi)
  • yy, yw, yl to copy ("yank") a line, word, or letter, respectively
  • p or P to paste after, or before current position, respectively
  • :wEnter to save (write) a file
  • :q!Enter to quit without saving
  • :wqEnter or ZZ to save and quit

If you edit text a lot, then switch to the Dvorak keyboard layout, learn to touch-type, and learn vim. Is it worth the effort? Yes.



ProTip™:   Don't be afraid to experiment with "dangerous" commands that rewrite history* — Git doesn't delete your commits for 90 days by default; you can find them in the reflog:

$ git reset @~3   # go back 3 commits
$ git reflog
c4f708b [email protected]{0}: reset: moving to @~3
2c52489 [email protected]{1}: commit: more changes
4a5246d [email protected]{2}: commit: make important changes
e8571e4 [email protected]{3}: commit: make some changes
... earlier commits ...
$ git reset 2c52489
... and you're back where you started

* Watch out for options like --hard and --force though — they can discard data.
* Also, don't rewrite history on any branches you're collaborating on.


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"

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

Commit/Amend Last Commit

If you have to change an old commit message over multiple branches (i.e., the commit with the erroneous message is present in multiple branches) you might want to use:

git filter-branch -f --msg-filter \
'sed "s/<old message>/<new message>/g"' -- --all

Git will create a temporary directory for rewriting and additionally backup old references in refs/original/.

  • -f will enforce the execution of the operation. This is necessary if the the temporary directory is already present or if there are already references stored under refs/original. If that is not the case, you can drop this flag.

  • -- separates filter-branch options from revision options.

  • --all will make sure, that all branches and tags are rewritten.

Due to the backup of your old references, you can easily go back to the state before executing the command.

Say, you want to recover your master and access it in branch old_master:

git checkout -b old_master refs/original/refs/heads/master

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.


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


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


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.


Wow, so there are a lot of ways to do this.

Yet another way to do this is to delete the last commit, but keep its changes so that you won't lose your work. You can then do another commit with the corrected message. This would look something like this:

git reset --soft HEAD~1
git commit -m 'New and corrected commit message'

I always do this if I forget to add a file or do a change.

Remember to specify --soft instead of --hard, otherwise you lose that commit entirely.


You can use Git rebasing. For example, if you want to modify back to commit bbc643cd, run

$ git rebase bbc643cd^ --interactive

In the default editor, modify 'pick' to 'edit' in the line whose commit you want to modify. Make your changes and then stage them with

$ git add <filepattern>

Now you can use

$ git commit --amend

to modify the commit, and after that

$ git rebase --continue

to return back to the previous head commit.


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^






amend