As you may already be aware, committing the state of your project with a
git commit records any changes (additions or deletions) to the official project history. And every “commit” prompts you for a brief message, in which you will notate what happened in that commit. (For more detailed information, you might want to write Git notes.) But what if you need to go back, change commit message, or “amend”, an entire commit to include or exclude file contents? There is an option for that.
- Change Commit Message Using “Amend”
--amendTo Change Files In Commit
If you don’t need cPanel, don't pay for it. Only pay for what you need with our Cloud VPS solutions.
CentOS, Debian, or Ubuntu No cPanel Bloat SSH Key Management
The default application of
--amend simulates a reset followed by a another commit. Using “amend” instead of a reset saves you the step of having to do a reset. Plus, resets can be destructive if you use the wrong option. With an
--amend you can safely change the most recent commit without daring a reset.
Change Commit Message Using “Amend”
By default, the
--amend option will apply your changes to the previous commit, or the “tip” of the branch. (“Tip of the branch” refers to the most recent commit regardless of which branch you are on. Make sure you are amending a commit on the correct branch.)
This is why “amend” is a great option to use if you simply forgot something, want to make a quick change, or just want to otherwise alter what you just committed without having to create a new commit.
Just run this command:
git commit --amend
And you will be prompted to edit your most recent commit in your text editor of choice. You will also notice that the previous commit message is printed into the text editor. This is normal. You can edit the message or erase it and write a completely new message.
Or, if you want to bypass the text editor, you can use the
-m option and put your new commit message in quotes.
git commit --amend -m "New commit message"
--amend To Change Files In Commit
It’s easy to also change the contents of the files in the commit by using the
--amend option. All you need to do is edit the files as you desire, stage them as usual, and run the commit again with the
--amend option as you did above.
As a side note, you may also want to check your work by running:
This command will let you know which files you just edited and whether or not they are staged for commit. In order to include any changes in the recent commit, they will need to be staged (just as in a regular commit).