How to Modify Existing Unpushed Commit Messages in Git

As a software developer, you probably already know how important it is to have meaningful commit messages.

Not only do they help your team understand the changes you made to the codebase, but they can also serve as a reference when reviewing your work later on.

Unfortunately, sometimes you may realize that a commit message you already wrote is not quite right.

In this case, you’ll need to know how to modify existing unpushed commit messages in Git.

In this article, we’ll cover the following topics:

  • Understanding Git Commit Messages
  • How to Modify Existing Unpushed Commit Messages
  • Best Practices for Writing Git Commit Messages.

Understanding Git Commit Messages

Before we dive into how to modify commit messages, let’s take a step back and understand what Git commit messages are.

In Git, a commit represents a set of changes you made to the codebase.

When you make a commit, you can attach a message to it that describes what you changed and why.

These messages are stored with the commit and can be viewed later on using the Git log.

Here’s an example of a commit message:

fix: typo in readme file

As you can see, this message is short and concise.

It tells us what was changed (a typo in the readme file) and what kind of change it was (a fix).

This is a great example of a well-written Git commit message.

How to Modify Existing Unpushed Commit Messages

So, you’ve made a commit and now you realize that the commit message isn’t quite right.

How can you change it?

The good news is that modifying existing unpushed commit messages in Git is a simple process.

Here are the steps to modify an existing commit message:

  • Open the terminal and navigate to your Git repository.
  • Use the command git rebase -i HEAD~n, where n is the number of commits you want to modify. This command opens an interactive rebase session, where you can edit the commit messages.
  • In the interactive rebase session, replace pick with reword for the commit you want to modify.
  • Save and close the file.
  • Git will then open a new editor for you to modify the commit message.
  • Once you’re finished, save and close the file.
  • Continue the rebase with the command git rebase –continue.

Here’s an example of how you can modify an existing commit message in Git:

# change from this
$ git log
commit f7f3f6d3c6af4f6d3c6af4f6d3c6af4f6d3c6af
Author: John Doe
Date:   Mon Feb 1 11:11:11 2022

fix typo

# to this
$ git rebase -i HEAD~1

# in the rebase file, change "pick" to "reword"
pick f7f3f6d3c6af4f6d3c6af4f6d3c6af4f6d3c6af fix typo

# to
reword f7f3f6d3c6af4f6d3c6af4f6d3c6af4f6d3c6af fix typo

# save and close the file

# Git will open a new editor for you to modify the commit message

In the editor, modify the commit message and save the file. In this example, let’s change the message to:

fix: typo in the readme file

Close the editor and continue the rebase with the command git rebase –continue.

That’s it! You have successfully modified an existing commit message in Git.

Best Practices for Writing Git Commit Messages

Now that you know how to modify existing commit messages, let’s talk about some best practices for writing Git commit messages.

Here are a few tips to keep in mind:

  • Keep the message concise and descriptive. Aim for 50-70 characters in the first line and a maximum of 72 characters per line.
  • Use present tense and avoid using words like “I” or “you”.
  • Start the first line with a verb, such as “fix”, “add”, or “remove”.
  • Use proper grammar and capitalization.
  • Include a detailed explanation in the body of the message, if necessary.

Here’s an example of a well-written commit message:

fix: typo in the readme file

A typo was found in the readme file that needed to be corrected. This commit fixes the typo and updates the file to reflect the change.

By following these best practices, you’ll ensure that your Git commit messages are clear, concise, and meaningful.

In conclusion, modifying existing unpushed commit messages in Git is a straightforward process that can be done with just a few commands.

By writing meaningful commit messages and following best practices, you’ll make your code history easier to understand and maintain