Skip to main content

Using Git

Amend a Comment in Git

We all make mistakes. The important thing is to make amends. Git allows you that as well.

While using git, we all witness a scenario where we want to make changes to the most recent commit, and it is possible to use the git commit --amend command.

There are two ways you can use the git commit amend:

  • Edit the commit message
  • Removing unwanted file

Edit the commit message

There are times when you made a commit, but the message was inaccurate or needs improvement, and in that case, you can use the git commit --amend command.

To edit the faulty message, you have to follow three simple steps:

  1. Switch to the directory where the file is located.
  2. Execute git commit --amend command to open the editor with the existing message.
  3. Make changes to the existing message and save changes.
  4. Verify the changes using the git log command.

For example, here I made a silly error where I was supposed to explain the meaning of the file, but I just wrote "First commit for hello.txt":

Faulty commit message in git

First, I will execute the git commit --amend command to open the text editor:

git commit --amend
modify the commit message in git

Once you are done making changes, git will notify the changes that were made as shown:

the commit message was modified

To verify if the changes were made as you intended, you can check the log of git using the following command:

git log
verify if the commit comment was modified or not by checking git logs

Remove unwanted files

There are times when you stage unwanted files and in that case, you can use the git commit with the --ammend flag to remove those files.

For that purpose, you'd have to follow 2 simple steps:

  1. Unstage the unwanted files
  2. Use the git commit --amend command. Optionally, you can also make changes to the commit message.

To unstage a file, you'd have to use the reset option with the git command as shown:

git reset unused_file.txt

I want to remove a file named unwanted_file.txt, I will use the following:

git reset unwanted_file.txt
remove unwanted file from git

Finally, use the git commit --amend command to remove it:

git commit --amend

It will open a text editor in case you want to make changes to the commit message, but you can skip that part by closing the text editor.

To verify if the file was removed or not, you can check the status using the following:

git status
Check status of removed file from git commit

If nothing works, perhaps you can undo a commit.

How to Undo a Commit in Git
Not feeling committed enough about your code change? Learn how to undo a commit in git.

I hope you will find this guide helpful.

Sagar Sharma