Git

How to Modify Existing, Unpushed Git Commit Messages?

While working on Git, developers save all additional changes to the Git local repository to update it. But sometimes, after committing, they may want to make changes in the commit message for multiple reasons, such as fixing typing mistakes, specifying additional information, or deleting sensitive information. In this situation, you can modify a commit message by using the “$ git commit –amend -m <commit-message>” command.

This post provides the method to change the existing or unpushed commit messages.

How to Change Existing, Unpushed Commit Messages?

To modify the existing or unpushed commit messages, navigate to the Git directory where the desired local repository exists. Then, initialize the Git local repository. Create a new file and track it. Next, save the added change into the repository and check the Git log history. Lastly, use the “$ git commit –amend -m <commit-message>” command to modify the commit message.

Now, implement the above-given procedure!

Step 1: Navigate to Directory
Move to the Git local repository using the “cd” command:

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

Step 2: Initialize Git Repository
Execute the “git init” command to initialize the empty Git repository:

$ git init

Step 3: Create File
Create a new file in the local repository by utilizing below-given command:

$ touch file1.txt

Step 4: Track File
Track a newly created file from the working directory to the staging area:

$ git add file1.txt

Step 5: Commit Changes
Save the added changes to the Git local repository through the “git commit” command along with the “-m” option to add commit message:

$ git commit -m "first file added"

Step 6: Check Git Log History
Run the “git log .” command to view the log history of the current repository:

$ git log .

Step 7: Modify Existing Unpushed Commit Message
To modify the existing and unpushed commit message, execute the “git commit” command with the “–amend” option and “-m” option to add a new commit message:

$ git commit --amend -m "My first file is added"

Step 8: Check Git Log History
To verify the performed operation, run the “git log .” command:

$ git log .

As you can see, the most recent commit message is modified successfully:

We have provided the method to modify the existing or unpushed commit messages.

Conclusion

To modify the existing or unpushed commit messages, first, move to the Git directory where the desired local repository exists. Initialize the Git local repository and create a new file. Then, track the newly created file to the repository. Save the added change into the repository and check the Git log history. Lastly, execute the “$ git commit –amend -m <commit-message>” command. This post demonstrated the procedure to modify the existing or unpushed 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.