Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Fix the Last Commit With --amend

In this lesson, you watched a forgotten file go into the last commit in the practice folder, and you saw Git replace that commit with a new one. Both files are in The Final Code below, so you can try it in your own practice folder.

Key Points

  • A commit can come out incomplete. Your commit took the new import in prices.js, but git status still listed the helper file format.js under “Untracked files”. This happens to everyone, most often with a new file, because git add stages only the files you name.
  • That commit’s snapshot has a prices.js that imports a format.js the snapshot doesn’t have, so anyone who gets these files back from it gets an error. You could commit the forgotten file on its own, but your history would then hold a commit that can’t run without the next one. The file belongs in the commit you made last.
  • To put it there, stage the file as usual. Check with git status that it’s the only staged file, because amend takes in everything that’s staged. Then run git commit --amend. The --amend option tells Git to replace your last commit rather than add another one after it, and --no-edit keeps the message you already wrote. The Date: line Git prints is the author date, when you first made the commit. Git also records when you amended, as the committer date, but doesn’t print it here.
  • The amended commit gets a new hash. Git calculates a hash from what’s in the commit, such as its snapshot, its label and its parent, and the snapshot now has one more file. Git doesn’t edit your commit. It makes a new one with the same parent and the same message, and marks it as the commit you’re on. The old commit leaves the list that git log shows.
  • Check the result with git show --stat, which lists every file the last commit changed.
  • Amend can fix the message too. Leave out --no-edit, and Git opens your editor with your last message. Change it, save, and close the tab, as when you wrote a commit body. Because amend takes in anything staged, run git status first and check that nothing is staged:
Language: Shell
$ git status
On branch main
nothing to commit, working tree clean
  • --amend only ever replaces your latest commit. If you spot a typo three commits back and amend, the fix lands in your latest commit, and the older one stays as it was. For an older commit, fix the file and make a new commit of its own.
  • Because amend swaps in a new commit, use it only on commits nobody else has a copy of. Everything you’ve committed so far lives only on your computer, so you’re free to amend.

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 In the last lesson, you threw away an edit before it reached a commit. This time, the slip is already in a commit. It happens in the practice folder, where a mistake costs nothing.

00:10 Watch first. Both files are below the video, so you can try it there afterward. A small helper in a new file turns cents into dollars. Your prices file imports it, and Node prints the egg price as $4.99.

00:26 You stage the prices file and commit it. Git reports one file changed. Only one? git status shows the other file, still untracked. So your commit’s snapshot has a prices file that imports a helper the snapshot doesn’t have.

00:42 Anyone who gets these files back from this commit gets an error. This happens to everyone, most often with a new file, because git add stages only the files you name.

00:55 You could commit the helper on its own, and Git would accept it. But your history would then hold a commit that can’t run without the next one. The helper belongs in the commit you made last, so put it there.

01:08 First, stage the forgotten file as usual. git status lists it as a new file, and nothing else is staged. Check that, because the next step takes in everything staged.

01:19 Then run git commit with the --amend option. It tells Git to replace your last commit rather than add another one after it. The --no-edit option keeps the message you already wrote.

01:31 Before you press Enter, look at the hash Git printed for your commit a moment ago. Your turn: pause the video and predict whether the commit keeps its hash.

01:47 It doesn’t. Git prints a new hash, and two files changed this time, with the helper created. The date line still shows when you first made the commit. The hash had to change.

02:01 Git calculates it from what’s in the commit, such as its snapshot, its label and its parent, and the snapshot now has one more file.

02:10 So Git doesn’t edit your commit. It makes a new one with the same parent and the same message, and marks it as the commit you’re on. Run git log with the --oneline option.

02:21 The top line has the new hash and your message. The old commit has left the list, so your history reads as if you’d never forgotten the file. To check, run git show with the --stat option.

02:34 It lists both files in your last commit now.

02:38 Amend can fix the message too. Without the --no-edit option, Git would have opened your editor with your last message. You’d change it, save, and close the tab, as when you wrote a commit body.

02:50 As before, check first that nothing is staged.

02:54 Amend only ever replaces your latest commit. If you spot a typo three commits back and amend, the fix lands in your latest commit, and the older one stays as it was.

03:04 For an older commit, fix the file and make a new commit of its own. And because amend swaps in a new commit, use it only on commits nobody else has a copy of.

03:16 Everything you’ve committed so far lives only on your computer, so you’re free to amend. Sharing commits comes later, and that’s all you need about it for now.

03:27 So when your last commit is missing a file, stage the file, then amend. Git replaces that commit with a complete new one.

03:36 Your store’s site still has its five commits, untouched. In Project: A Week of To-Dos for the Store, you’ll work through the store’s to-do list with every habit from this course, including this fix when a commit comes out incomplete.

Become a Member to join the conversation.