Bird
Raised Fist0
Gitdevops~20 mins

Amending the last commit in Git - Practice Problems & Coding Challenges

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
Challenge - 5 Problems
🎖️
Git Amend Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
Output of amending last commit with new message
What is the output of the following command sequence in a git repository?

git commit --amend -m "Updated commit message"
Git
git commit --amend -m "Updated commit message"
ASuccessfully amended the last commit with the new message.
BError: You have uncommitted changes. Commit aborted.
Cfatal: no changes added to commit (use "git add" and/or "git commit -a")
DError: No previous commit to amend.
Attempts:
2 left
💡 Hint
Amending changes the last commit message or content if staged.
🧠 Conceptual
intermediate
1:30remaining
Effect of amending last commit on commit hash
What happens to the commit hash when you amend the last commit in git?
AThe commit hash changes only if files are added, not if message changes.
BThe commit hash stays the same because only the message changes.
CThe commit hash changes because the commit content or metadata changes.
DThe commit hash stays the same if the commit is pushed already.
Attempts:
2 left
💡 Hint
Commit hash depends on commit content and metadata.
Troubleshoot
advanced
2:00remaining
Error when amending last commit with unstaged changes
You run git commit --amend but get the error:

fatal: no changes added to commit (use "git add" and/or "git commit -a")

What is the cause?
AYou have unstaged changes; git cannot amend without staged changes.
BYour git repository is corrupted.
CYou are trying to amend a pushed commit without force push.
DYou have no previous commit to amend.
Attempts:
2 left
💡 Hint
Amend requires staged changes or message edit.
🔀 Workflow
advanced
2:30remaining
Correct workflow to amend last commit with new file added
Which sequence correctly amends the last commit by adding a new file notes.txt?
Atouch notes.txt<br>git commit --amend --amend
Btouch notes.txt<br>git add notes.txt<br>git commit --amend --no-edit
Cgit add notes.txt<br>git commit -m "Add notes"<br>git commit --amend
Dtouch notes.txt<br>git commit --amend -m "Add notes"<br>git add notes.txt
Attempts:
2 left
💡 Hint
You must stage changes before amending.
Best Practice
expert
3:00remaining
Best practice when amending a commit already pushed to shared repository
You amended your last commit after pushing it to a shared repository. What is the best practice to avoid problems for others?
AAsk all collaborators to reset their branches before you push.
BForce push the amended commit immediately to update the remote branch.
CDelete the remote branch and push the amended commit as new branch.
DAvoid amending pushed commits; instead, create a new commit to fix issues.
Attempts:
2 left
💡 Hint
Rewriting history after pushing can cause conflicts for others.

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