Git Amend and Log Commands Explained
In the previous blog, we learned how to make commits in Git. Now, let’s go a step further and see what happens when you forget to add a file in your commit, or when you want to change your commit message.
We will also explore the git log command, which shows you the history of all commits made so far.
Why Amend a Commit?
Sometimes you commit your code and later realize that:
- You forgot to add a file.
- Your commit message was incorrect.
Instead of creating a new commit for such small fixes, you can amend the last commit.
Checking Current Status
First, let’s run:
git status
Output:
nothing to commit, working tree clean
Now let’s create two new files:
touch file1.txt file2.txt
Check the status:
git status
Output shows:
Untracked files:
file1.txt
file2.txt
Making a Commit Without Adding All Files
Let’s stage only one file:
git add file1.txt
git commit -m "Fourth commit"
At this point, you realize you forgot to add file2.txt. Instead of making a new commit, we will add it to the previous commit.
Using Git Commit Amend
First, add the missing file:
git add file2.txt
Now amend the last commit:
git commit --amend
This opens your text editor where you can edit the commit message.
Or you can directly change the message:
git commit --amend -m "Fourth commit with both files"
Now both file1.txt and file2.txt are included in the last commit.
Viewing Commit History with Git Log
To see all commits:
git log
This shows commit hashes, authors, and messages.
For a cleaner one-line output:
git log --oneline
Output example:
abcd123 Fourth commit with both files
efgh456 Third commit
ijkl789 Second commit
mnop012 First commit
Changing Commit Message
If you realize the commit message was wrong:
git commit --amend -m "User module updated"
Now check again:
git log --oneline
Output:
qrst345 User module updated
efgh456 Third commit
ijkl789 Second commit
mnop012 First commit
How Amending Works (Visual)
Wrap-up
In this blog, we learned:
- How to fix a commit if you forgot to add files.
- How to change the last commit message with
git commit --amend. - How to view commit history with
git logandgit log --oneline.
In the next blog, we will dive deeper into playing with git log and understanding HEAD in Git.
Keep Learning 🚀
👉 Subscribe to Learning Ocean – Subscribers get coupon codes for my courses, early access to blogs and courses, and even exclusive YouTube videos.
👉 My YouTube Channel – More videos, more fun, and lots of learning!
👉 📺 Watch this topic in video form
Stay curious, keep coding, and let’s make learning fun together! 🎉