Git

How to Amend a Git Commit Message

While working with a decentralized version control system, you may face a situation where it is required to modify a commit message. There are multiple reasons why you need to do so, such as deleting sensitive data, adding some additional info, correcting typing mistakes, and many more. In such a situation, the “$ git commit –amend -m <“new-message”>” can rescue you.

In this study, we will explain the method to amend Git commit messages.

How to Amend a Git Commit Message?

To amend a Git commit message, first, we will move to the Git root folder and create a new Git directory. Then, create and add a new file into the directory and commit changes. After that, we will execute the “$ git commit –amend -m <“new-message”>” command to change the most recent commit message.

Now, check out the below-mentioned steps to modify a recent commit message!

Step 1: Move to Git Root Folder
First, navigate to the Git root folder through the “cd” command:

$ cd "C:\Users\nazma\Git"

Step 2: Create New Directory
Next, create a new directory using the provided command:

$ mkdir Demo12

Step 3: Move to Created Directory
Now, run the “cd” command to move to the recently created directory:

$ cd Demo12

Step 4: Create and Update File
To create and update a file, run the “echo” command:

$ echo "Demo12 added"> demo12.txt

Step 5: Update Directory
Next, update the directory and track the file by executing provided command:

$ git add .

Step 6: Commit Changes
Now, commit all changes with the required message to the Git repository:

$ git commit -m "new file added"

Step 7: View Log History
After that, use the “git log” command with the “–oneline” option to view the Git repository reference log history:

$ git log --oneline

As you see, the HEAD points to the most recently added commit:

Step 8: Amend Git Commit
Finally, execute the below command to amend the most recent added commit message:

$ git commit --amend -m "demo12.txt is added"

Below output shows that the previously committed message is replaced with the new message:

Step 9: Verify Amend Git Commit
Lastly, execute the “git log” command to ensure that the commit message is replaced or not:

$ git log --oneline

It can be seen that we have successfully amended the recent commit message:

We have offered the procedure to amend a Git commit message

Conclusion

To amend a Git commit message, first, navigate to the Git root folder and create a new Git directory. Then create a new file and add it to the Git repository. After that, commit changes by executing the “$ git commit -m <commit-message>”. Lastly, update the commit message using the “$ git commit –amend -m <new-message>” command. This study illustrated how to amend Git commit messages.

About the author

Maria Naz

I hold a master's degree in computer science. I am passionate about my work, exploring new technologies, learning programming languages, and I love to share my knowledge with the world.