Function of git commit --amend in Version Control

200-901 By islever Editorial Last updated

Explain the advantages of version control Utilize common version control operations with Git

Short answer

B Git commit --amend modifies the last commit by incorporating currently staged changes into it.

Question

What does the git commit --amend command do?
  1. Reverts the last commit
  2. Modifies the last commit by adding staged changes Correct Answer
  3. Amends the commit message of a pushed commit
  4. Creates a new branch from the last commit

Key Takeaways

The git commit --amend command updates the most recent commit by integrating staged changes, allowing for correction of errors or message edits without creating new commit entries.

Expert Analysis

git commit --amend modifies the most recent commit by adding currently staged changes and/or allowing the commit message to be edited. It should not be used on commits that have already been pushed to a shared repository.

The command git commit --amend is a powerful tool in Git for refining the history of a repository, specifically targeting the tip of the current branch. Its primary function is to update the most recent commit object. When executed, it does not create a new commit entry in the linear sense of appending; rather, it replaces the existing HEAD commit with a new one that incorporates any changes currently present in the staging area (index). This mechanism allows developers to correct mistakes, such as forgetting to include a file or needing to adjust the commit message, without leaving a trail of corrective commits like "fix typo" or "oops forgot file".

Option B correctly identifies this behavior: it modifies the last commit by adding staged changes. The critical constraint here is that only staged changes are included. If changes exist in the working directory but have not been added to the index via git add, they will not be part of the amended commit unless explicitly staged first. This distinction is vital for maintaining a clean history. The command effectively takes the parent of the current HEAD, applies the current index contents, uses the existing commit message (unless overridden), and creates a new commit hash. Because the commit hash is derived from its content, parents, and metadata, amending a commit always results in a different SHA-1 identifier.

Option A is incorrect because reverting the last commit is achieved using git revert. While git reset --hard HEAD~1 can also remove the last commit, --amend does not delete the previous commit's work; it integrates it into a revised version. Reverting creates a new commit that undoes changes, preserving history, whereas amending rewrites history.

Option C suggests amending a committed that has already been pushed. This is dangerous and generally discouraged in collaborative environments. Amending a pushed commit rewrites public history. If others have pulled the original commit, their histories will diverge, leading to complex merge conflicts when they try to sync. While technically possible if forced pushed (git push --force-with-lease), it violates best practices for shared repositories. The reference explanation explicitly warns against this use case, making Option C an unsafe practice rather than a standard definition of the command's utility.

Option D is incorrect because creating a new branch is done with git branch <name> or git checkout -b <name>. git commit --amend operates on the current branch's history and does not affect branch creation or movement. It modifies the commit object itself, not the pointer to the branch.

Understanding git commit --amend requires grasping Git's immutable commit model. Each commit is a snapshot linked to a parent. Amending creates a new snapshot that looks like the old one plus some changes, pointing to the same parent. This makes it ideal for local, unpublished work where history cleanup is desired before sharing. It is a core operation in iterative development workflows, particularly when combined with interactive rebasing or squashing, though it stands alone as a specific tool for the very tip of the branch.

Exam Strategy

When analyzing Git commands, distinguish between operations that rewrite history (like amend and rebase) versus those that append history (like commit and merge). For 'amend' questions, always check if the changes are staged; unstaged changes are ignored unless explicitly added. Remember that amending pushes a new commit hash, which impacts shared repositories.

Practice All 200-901 Questions

Access 105 questions with complete answers and detailed explanations.

View Full 200-901 Practice Test →

← Back to 200-901 Study Guide