The git --amend
command can be used to modify the last commit. Once it has modified the last commit, it replaces it with a new commit.
git --amend
git commit --amend -m "commit message"
We use the
-m
option with the commit message to pass a new message from the command line, similar to how we usegit --commit
.
Let’s create a file and initialize it with git.
On your terminal, create a folder called dummy
by using the mkdir
command as shown below.
mkdir dummy
Change your directory to dummy
folder by using cd
command and then create a new file with the touch
command as shown below.
cd dummy
touch dummy.txt
Initialize the dummy folder with the git
command as shown below.
git init
Stage the dummy file with the git add
command as shown below.
git add dummy.txt
Now make the first commit.
git commit -m "first commit"
To see the commit history, we type in the log command as shown below.
git log --oneline
The output of the git log --oneline
command is shown below.
Add some dummy text to the dummy.txt
file and commit the changes.
git add dummy.txt
git commit -m "add dummy text to dummy.txt file"
Check the commit history in the image below.
Now, let’s modify the last commit with git --amend
.
git commit --amend -m "ammend last commit"
Finally, let’s find out if this new commit has replaced the last commit by using the git log
command.
With git --amend
we were able to modify the last commit and replace it with the modified one.