Amend The Last Commit
Part of the Version Control section of Coddy's Terminal journey — lesson 42 of 58.
If you just created a commit and immediately realize the message is wrong, or you forgot to include a file, you can rewrite that commit with:
git commit --amend -m "New message"This replaces the most recent commit with a new one that has your updated message and includes whatever is currently staged.
To add a forgotten file to the previous commit, stage it first, then amend without changing the message:
git add forgotten.txt
git commit --amend --no-editThe --no-edit flag keeps the existing message.
One catch: amending creates a brand-new commit (with a new ID), even though it looks like an edit. Only amend commits you have not shared with anyone yet, or you will confuse collaborators who already have the old version.
Challenge
EasyThe folder contains app.py. Initialize the repo, stage and commit the file with the message Inital commit (intentional typo).
Amend the commit to fix the message to Initial commit, then print the latest commit subject using git log -1 --pretty=%s.
Cheat sheet
Fix the most recent commit message with --amend:
git commit --amend -m "New message"Add a forgotten file to the previous commit without changing the message:
git add forgotten.txt
git commit --amend --no-editAmending creates a new commit ID — only amend commits not yet shared with others.
Try it yourself
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Version Control
2Getting Started
Initialize A RepositoryThe .git FolderConfigure Your IdentityGit StatusRecap - First Repo8Merging
What Is A MergeFast-Forward MergeThree-Way MergeMerge ConflictsResolve A ConflictRecap - Merge Master11Feature Branch Project
Project OverviewInitialize Main3Tracking Changes
The Staging AreaGit AddGit CommitModifying A Tracked FileGit LogRecap - First Commits6Recipe Site Project
Project OverviewInitialize And Ignore9Undoing Changes
Discard Unstaged ChangesUnstage A FileAmend The Last CommitRevert A CommitReset A BranchRecap - Time Machine