Git commit amend: A Beginner’s Guide
The git commit –amend command lets you modify your last commit. You can change your log message and the files that appear in the commit. The old commit is replaced with a new commit which means that when you amend your old commit it will no longer be visible in the project history.
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.
The Git Commit Amend Command
The git commit –amend command modifies your latest commit. This command lets you change files in your last commit or your commit message. Your old commit is replaced with a new commit that has its own ID.
- Career Karma matches you with top tech bootcamps
- Get exclusive scholarships and prep courses
The syntax for the amend command is as follows:
git commit --amend
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. You can only modify the last commit in your repository, which is referred to as a Git HEAD.
Because the old commit is replaced, you should double check to make sure that you want to amend your previous commit.
The git commit –amend command only works on the most recent commit. The git rebase command or the git reset command let you rewrite history further back than the last commit. The rebase command offers an interactive rebasing environment in which to rewrite history.
Git Amend Commit Message
You can use the git commit –amend command to edit a commit message. To do so, use the -m flag and specify a new commit message in quotation marks.
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. Here is a command line operation that lets us replace our last commit:
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.
Git Commit Amend: Change a 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. app.py will become a committed file when we save our changes. 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 lets you rewrite your most recent commit. You can change the commit message associated with the commit and the changes you made to files in that commit.
Before you start amending commits, you should make sure that you do not amend public commits. 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.
To learn more about working with Git, read our How to Learn Git guide.
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.