Amending the last commit
We have yet to add the <h1>
tags to the hello.html
file. The result should look like this:
<h1>Hello, World!</h1>
Wrap the content of hello.html
with <h1>
tags, stage, and commit the changes.
Tip: If you forgot the necessary commands, check the git add and git commit commands in Gitopedia.
Oh, sorry, didn't I say that the last commit message should say exactly Added HTML tags to hello.html
?
Silly as it may sound, mistakes in commit messages are quite common: typos, missing ticket IDs, etc. But don't worry, we can fix it easily.
All you need to do is run the git commit
command again, but with the --amend
option. This option tells git to replace the last commit with the new one.
git commit --amend -m "Added HTML tags to hello.html"
As usual, the -m
option allows you to specify the new commit message directly from the command line. If you omit it, the default text editor will open with the last commit message.
If you ran the commit command without the -m
option, you may have noticed that your terminal changed: the history is gone and you are presented with a blank screen. This is a text editor that Git has opened for you to write the commit message. You can write the message in this editor, save it, and close it to complete the commit. The editor is usually a command line text editor like vim
or nano
.
Sounds easy, but what could go wrong? People not familiar with these editors may find it difficult to even exit them. If you're one of them, here are some videos that will help you exit these editors: Vim, Nano. If nothing helps, kill the terminal with the little icon on the right side of the terminal and start over.
Amend the last commit message using the command above.
This is great, but what if we missed adding other important bits to the last commit? For example, let's surround our text with proper html
and body
tags like this:
<html>
<body>
<h1>Hello, World!</h1>
</body>
</html>
To add missing changes to the last commit, first we need to stage them using the git add
command. Then we can use the --amend
option to add them to the last commit.
Edit the hello.html
file to include the missing tags and stage the changes. Don't commit just yet!
At this point, we can simply run git commit -m "Another commit"
to commit the changes as a new commit. But if we want to add them to the last commit, we can use the --amend
option again.
git commit --amend -m "Added H1, HTML, and BODY tags to hello.html"
Amend the last commit using the command above.
Hi! I'm Alex, creator of GitByBit.
This page is a part of the interactive course about Git version control.
It's a one-of-a-kind course that is integrated into the VS Code code editor. Learning directly in VS Code lets you operate Git exactly as you would in real life, doing real jobs, writing real code.
In addition, the course has access to your actual terminal, so it can point out mistakes, suggest workarounds, etc.
The course is FREE, there are no Ads or other bullshit. There are optional premium add-ons you can purchase, mainly to support my work (regrettably, I have to eat every day), but that's totally up to you.