Junior — Middle
How does the command `git commit --amend` work and in what cases is it used?
sobes.tech AI
Answer from AI
The git commit --amend command is used to modify the last commit. It allows you to correct the commit message or add new changes if you forgot to include something.
How it works:
- An editor opens for changing the commit message (unless specified otherwise).
- The current changes in the index (staged) are added to the last commit, replacing it.
Example usage:
# Made a commit but forgot to add a file
git add forgotten_file
# Correcting the last commit by adding a new file
git commit --amend
Important: If the commit has already been pushed to a shared repository, using --amend will change history, which can lead to conflicts when working collaboratively. In such cases, it is better to create a new commit.