Amend
Amend is a Git feature that allows developers to modify the most recent commit. It is commonly used to correct mistakes such as adding a missing file, fixing an incorrect commit message, or making minor adjustments without creating an entirely new commit.
This feature is particularly helpful for maintaining a clean and concise commit history, ensuring that changes are logically grouped and easy to understand.
Syntax
The syntax for using the amend feature in Git is:
git commit --amend
To amend both the content of the commit and the commit message, the following would be used:
git commit --amend -m "Your new commit message"
Example 1
Suppose, the developer created and committed a feature, but forgot to include a file:
# Original commitecho "Initial code" > feature.txtecho "Forgotten content" > forgotten-file.txtgit add feature.txt # Dev forgot to add forgotten-file.txtgit commit -m "Add initial code for the feature"
Here’s the original commit history:
git log --onelineabc1234 Add initial code for the feature
The developer realized that he hasn’t included a file in the original commit. So, he performs the amend operation to both include the file in that commit and change the commit message:
# Amending the original commitgit add forgotten-file.txtgit commit --amend -m "Add initial code for the feature and forgotten file"
Here’s the amended commit history:
git log --onelinedef5678 Add initial code for the feature and forgotten file # Commit abc1234 has been replaced by def5678
Example 2
Suppose, the developer is going through another commit history:
git log --onelineabc1234 Original commit message
While going through it, the developer didn’t like the above commit message. So, he decides to use the amend operation to only change the commit message without touching the contents of the commit:
git commit --amend -m "Corrected commit message"
Here’s the amended commit history:
git log --onelineabc1234 Corrected commit message
Note: Use
git commit --amend
carefully if the commit has already been shared with others, as it rewrites history.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Git on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Course
Learn Git & GitHub
Use our beginner friendly Git course to integrate Git and GitHub and manage versions of your projects using Git branches.With CertificateBeginner Friendly4 hours