Bird
Raised Fist0
Gitdevops~10 mins

Amending the last commit in Git - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Process Flow - Amending the last commit
Make changes to files
Stage changes with git add
Run git commit --amend
Edit commit message if needed
Save and close editor
Last commit updated with new changes
This flow shows how you change files, stage them, then use git commit --amend to update the last commit with new content or message.
Execution Sample
Git
echo "New line" >> file.txt
  git add file.txt
  git commit --amend -m "Updated last commit with new line"
Adds a new line to file.txt, stages it, then amends the last commit with the updated content and message.
Process Table
StepActionCommandResult
1Modify file.txt by adding a new lineecho "New line" >> file.txtfile.txt content updated
2Stage the changed filegit add file.txtfile.txt staged for commit
3Amend the last commit with new changes and messagegit commit --amend -m "Updated last commit with new line"Last commit replaced with new content and message
4Check commit loggit log -1 --onelineShows amended commit with new message
💡 Amend replaces the last commit with new staged changes and message
Status Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
file.txt contentOriginal contentOriginal content + New lineStaged changes include new lineCommit updated with new lineCommit includes new line
Last commit message"Initial commit""Initial commit""Initial commit""Updated last commit with new line""Updated last commit with new line"
Key Moments - 2 Insights
Why does git commit --amend replace the last commit instead of creating a new one?
Because git commit --amend rewrites the last commit with the staged changes and new message, as shown in execution_table step 3 where the last commit is replaced.
What happens if you forget to stage changes before git commit --amend?
The last commit message can be changed but the content stays the same, since only staged changes are included. See execution_table step 2 and 3 for staging importance.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the result after step 2?
Afile.txt content updated
BLast commit replaced
Cfile.txt staged for commit
DCommit log shows amended commit
💡 Hint
Check the 'Result' column for step 2 in the execution_table
At which step does the last commit message change?
AStep 3
BStep 1
CStep 2
DStep 4
💡 Hint
Look at the 'Last commit message' in variable_tracker after each step
If you skip 'git add' before 'git commit --amend', what changes in the execution?
ALast commit content updates with new changes
BOnly commit message changes, content stays the same
CNew commit is created instead of amending
DGit throws an error and stops
💡 Hint
Refer to key_moments about staging importance before amending
Concept Snapshot
git commit --amend lets you update the last commit.
First, change files and stage them with git add.
Then run git commit --amend to replace the last commit.
You can also edit the commit message.
If no changes are staged, only the message updates.
Use git log -1 to verify the amended commit.
Full Transcript
To amend the last commit in git, first modify your files as needed. Then stage those changes using git add. After staging, run git commit --amend with an optional new message. This command replaces the last commit with the staged changes and new message. If you do not stage changes, only the commit message updates. Finally, check your commit with git log -1 to confirm the amendment.

Practice

(1/5)
1. What does the git commit --amend command do?
easy
A. It modifies the last commit by changing its message or content.
B. It deletes the last commit permanently.
C. It creates a new commit without changing previous commits.
D. It resets the repository to the initial commit.

Solution

  1. Step 1: Understand the purpose of git commit --amend

    This command is used to change the last commit, either by editing its message or adding new changes.
  2. Step 2: Compare with other options

    Deleting commits or resetting the repository are different commands like git reset. Creating a new commit does not amend the last one.
  3. Final Answer:

    It modifies the last commit by changing its message or content. -> Option A
  4. Quick Check:

    Amend last commit = modify last commit [OK]
Hint: Amend changes last commit, not create or delete [OK]
Common Mistakes:
  • Thinking it deletes the last commit
  • Confusing amend with creating a new commit
  • Assuming it resets the whole repo
2. Which of the following is the correct syntax to amend the last commit message in Git?
easy
A. git commit -amend "New message"
B. git amend commit -m "New message"
C. git commit --amend -m "New message"
D. git commit --edit-message "New message"

Solution

  1. Step 1: Recall the correct syntax for amending commit message

    The correct command is git commit --amend -m "New message" to directly change the last commit message.
  2. Step 2: Identify incorrect syntax

    Options B, C, and D use invalid flags or wrong order, which Git does not recognize.
  3. Final Answer:

    git commit --amend -m "New message" -> Option C
  4. Quick Check:

    Correct amend syntax = git commit --amend -m [OK]
Hint: Use --amend before -m to change message [OK]
Common Mistakes:
  • Swapping order of flags
  • Using non-existent flags like --edit-message
  • Typing 'amend' as a separate command
3. Given the following commands executed in order:
echo "Hello" > file.txt
 git add file.txt
 git commit -m "Add file"
 echo "World" >> file.txt
 git add file.txt
 git commit --amend -m "Add file with content update"

What will git log -1 --pretty=%B output?
medium
A. Add file
B. Add file with content update
C. Add file Add file with content update
D. No commit message

Solution

  1. Step 1: Understand the commit history after commands

    First commit message is "Add file". Then file.txt is updated and staged. The git commit --amend replaces the last commit message with "Add file with content update".
  2. Step 2: Check the latest commit message output

    The git log -1 --pretty=%B shows the last commit message, which is now "Add file with content update" after amend.
  3. Final Answer:

    Add file with content update -> Option B
  4. Quick Check:

    Amended commit message = updated message [OK]
Hint: Amend replaces last commit message and content [OK]
Common Mistakes:
  • Thinking amend adds a new commit instead of replacing
  • Expecting both messages to appear
  • Assuming commit message stays unchanged
4. You ran git commit --amend but accidentally removed some changes from the last commit. How can you fix this?
medium
A. Delete the repository and clone again.
B. Run git commit --amend again without changes.
C. Use git push --force to overwrite remote.
D. Use git reflog to find the previous commit and reset to it.

Solution

  1. Step 1: Understand the problem with amend

    Amending rewrites the last commit, so if changes were lost, the previous commit state is still in Git history.
  2. Step 2: Use git reflog to recover

    git reflog shows recent commit states. You can find the commit before amend and reset to it to restore lost changes.
  3. Final Answer:

    Use git reflog to find the previous commit and reset to it. -> Option D
  4. Quick Check:

    Recover lost commit with reflog [OK]
Hint: Use reflog to recover lost commits after amend [OK]
Common Mistakes:
  • Trying to fix by amending again without changes
  • Deleting repo instead of recovering
  • Forcing push without fixing local history
5. You committed a file with a typo in the message and forgot to add a new file. Which sequence correctly fixes both issues using amend?
hard
A. Add the new file, then run git commit --amend -m "Corrected message"
B. Run git commit --amend -m "Corrected message" first, then add the new file
C. Delete the last commit, add the new file, then commit again
D. Push the commit, then fix the message and add file in a new commit

Solution

  1. Step 1: Stage the new file before amending

    To include the new file in the last commit, you must add it first with git add.
  2. Step 2: Amend the commit with the corrected message

    Run git commit --amend -m "Corrected message" to update the commit message and include the staged new file.
  3. Final Answer:

    Add the new file, then run git commit --amend -m "Corrected message" -> Option A
  4. Quick Check:

    Stage files before amend to include them [OK]
Hint: Add files first, then amend commit message [OK]
Common Mistakes:
  • Amending before adding new files
  • Deleting commits unnecessarily
  • Pushing before fixing local commit