Git amend previous commit
Rewriting commit history with git
Premature commits happen by accident more often than not. Sometimes you just missed adding a file or maybe you realized that you should have added a much more descriptive commit message. Either way, your intent is to edit your most recent commit. Thats where
git commit --amend command comes to your rescue.
There are two major use cases of
git commit --amend.
- To edit just the previous commit message without changing its snapshot.
- To combine staged changes with the previous commit instead of committing it as an entirely new snapshot, without having to edit the commit message.
Edit a previous commit message in GitThe simple command is the following.
git commit --amend -m "your new message"Below example, shows an example usage for it. [wpgist id="73cbec7e3ccece226448" file="git-amend.sh"]
Combine staged changes with the previous commit without creating a new snapshotThe simple command is the following.
git commit --amend --no-editSuppose, you added few files and committed the change. Now, you realize you want to add one more file to that previous commit itself. Below example, demonstrates how to do it. Another, use case would be after committing something you realize you want to make some more changes and amend it to the previous commit instead of creating a new snapshot. [wpgist id="73cbec7e3ccece226448" file="git-amend-1.sh"]