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
Amending the Last Commit in Git
📖 Scenario: You are working on a project and just made a commit with a small mistake in the commit message. You want to fix this mistake without creating a new commit.
🎯 Goal: Learn how to amend the last commit message using Git commands.
📋 What You'll Learn
Create a file named example.txt with specific content
Add the file to the Git staging area
Make an initial commit with a given message
Amend the last commit message to the correct one
Display the final commit message to verify the change
💡 Why This Matters
🌍 Real World
Fixing mistakes in commit messages is common when working on software projects to keep history clean and understandable.
💼 Career
Knowing how to amend commits helps developers maintain clear project history and collaborate effectively in teams.
Progress0 / 4 steps
1
Create a file and add content
Create a file named example.txt and add the text Hello, Git! inside it.
Git
Hint
Use the echo command to write text into a file.
2
Add the file to Git staging area
Initialize a new Git repository and add the file example.txt to the Git staging area using the git init and git add commands.
Git
Hint
Use git init followed by git add example.txt to initialize the repository and stage the file.
3
Make the initial commit
Make an initial commit with the message Initial commit with typo using git commit -m.
Git
Hint
Use git commit -m "Initial commit with typo" to commit.
4
Amend the last commit message
Amend the last commit message to Initial commit with correct message using the git commit --amend -m command. Then, display the last commit message using git log -1 --pretty=%B.
Git
Hint
Use git commit --amend -m "New message" to change the last commit message.
Use git log -1 --pretty=%B to show the last commit message.
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
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.
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.
Final Answer:
It modifies the last commit by changing its message or content. -> Option A
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
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.
Step 2: Identify incorrect syntax
Options B, C, and D use invalid flags or wrong order, which Git does not recognize.
Final Answer:
git commit --amend -m "New message" -> Option C
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:
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".
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.
Final Answer:
Add file with content update -> Option B
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
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.
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.
Final Answer:
Use git reflog to find the previous commit and reset to it. -> Option D
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
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.
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.
Final Answer:
Add the new file, then run git commit --amend -m "Corrected message" -> Option A
Quick Check:
Stage files before amend to include them [OK]
Hint: Add files first, then amend commit message [OK]