Did you forget to include something in your last commit message? Did you make a typo? Did you add something into your commit message that is not relevant? Don’t worry! Git has a solution for you: the git commit –amend command.
In this guide, we’re going to talk about how to modify your last commit using the git commit –amend command. We’ll walk through an example to help you get started.
How to Use the git amend Command
The git commit –amend command modifies your latest commit. You can use this command to change a commit message or modify the files in the commit.
This command is useful because it allows you to undo changes without having to create a new commit. Creating a new commit to undo changes would make the history of a repository less clear. You’d only be reverting a mistake and that does not usually need a commit of its own.
Amending a commit does not just change a commit. It replaces it with a new commit which will have its own ID.
The git commit –amend command only works on the most recent commit. To modify a commit that is further back in your repository’s history, you will need to use a tool like the git rebase command or the git reset command. These commands are often used to rewrite history from the command line in a Git repository.
Change a Commit Message
You’ve just made a typo in your last commit message. What should you do? Panic is not the right answer. Developers make this sort of mistake all the time in Git. You can use the git commit –amend command to edit the commit message:
git commit --amend -m “feat: Revised commit message”
This command will replace the single commit log message in your last commit with the one that you state. We use the -m flag to tell Git that we want to change a commit message.
You can use this command without the -m flag. If you do, an interactive text editor will be opened up in which you can replace the message from your older commit. Save and exit the text editor and your change will be made.
Changing a Committed File
You have just realized that your last commit is missing a crucial configuration file. Without it, the changes you have pushed will not function correctly. We can use the git commit –amend command to get out of this bind and fix the issue.
To change the files in a commit, first add the files you want to be included in your commit:
git add config.py
This adds the file app.py to our repository. If you want to remove a file from a commit, you can use git rm:
git rm config_old.py
Once you have made the changes to a repository, you are ready to amend your commit. You can do this by using the –no-edit flag:
git commit --amend --no-edit
This command will change the files in your last commit. It will not change the message associated with the commit because we have not used the -m flag.
When we run this command, config.py is added to our Git repository. config_old.py is removed from the repository. Once you have made a commit, you can push it to your remote repository using git push.
The git commit –amend command allows you to change a commit message or change the files included inside a commit.
Before you start amending commits, you should make sure that you do not amend public commits. As we said earlier, amended commits are new versions of a commit. This means that if you amend a commit, the history of your repository will change. If other developers have already started to use the code from your commit, amending it could be confusing.
Now you’re ready to start amending Git commits like an expert!