Git: How to change the most recent commit with `--amend`

Kent C. Dodds
InstructorKent C. Dodds
Share this video with your friends

Social Share Links

Send Tweet
Published 8 years ago
Updated 5 years ago

If you make a mistake with a commit and need to make changes or you'd like to update the commit message of the most recent comment, you can do this with the git commit --amend command.

[00:00] We have some changes to commit here. We'll just run git commit -m and then we'll provide a commit message. Once we let the git hooks run, we'll see that our commit succeeded. Now if we look at the git status we'll notice that we made a mistake. We didn't add all the files we meant to.

[00:15] There are various ways to fix this. For this scenario we'll simply use the amend flag for the git commit command. First we'll run git add . to add all the files we want. Now let's double-check that we have all the files that we want with git status. It looks like we're good.

[00:31] Now we'll run git commit amend and because this project has a git hook for a commit message we're going to see that run before anything else happens. Once that's done our default editor for git will come up, allowing us to change the commit message and we'll also see the files that will be associated with this commit.

[00:47] Looks like we're good. Let's save and close the file. Now if we run git status we'll see that we have nothing else to commit, and if we run git log we'll see that the most recent commit is the one we just executed. The amend flag can also be used to change the most recent commit message. Let's go ahead and try that.

[01:03] Even though we have nothing staged, if we run git commit --amend, again, it will execute the commit message git hooks, but then we'll see that we can still change the commit message here. Let's add an emoji just for fun. Now we'll save and close the file.

[01:18] Now if we run git log we'll see our new commit message. It looks like git log doesn't like emoji very much. You need to remember that this is changing commit history and as always if you're changing anything that's already been pushed to a remote repository, you'll have to force push to alter the history on the remote repository. Otherwise your push will be rejected.

[01:39] Make sure that you know what you're doing before you do this. Also if you need to alter the message of an older commit, you can use the reword command in an interactive rebase, which is a whole another beast.

egghead
egghead
~ 8 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today