Learn to edit git commit message(s) after or before push

Learn to edit git commit message(s) after or before push

It often happens that you just wrote a commit message and you wanted to edit it because you made some mistakes, or sometimes you realize your mistake(s) after pushing the code to remote, so you want to edit the message.

In this tutorial, we will learn how to edit a commit message in the safest way possible for both scenarios.

But before we begin, make sure;

  • You have a basic idea of Git/GitHub and its workflow

  • You know how to set up a project with git if you are reading this article then, of course, you know these basic kinds of stuff, I know 😁

CASE I: you want to edit the latest commit message only and you have not pushed the changes to remote

if this is the case then you can do it with a single command.

firstly, amend the local latest commit by running $ git commit --amend then write a detailed commit message and save it.

OR,

if you don't need to write a descriptive commit message then you can run $ git commit --amend -m "new_commit_message"

Example

Suppose, there is a project which has already some commits. Let us assume the following are some recent commits. It can be checked by running git log or git log –oneline.

1.png

We want to edit the latest commit message whose commit id is bd7f5bf. We want to replace users with user’s in the commit message. Then we would simply run $ git commit --amend -m "added user's full name" That’s it. Let’s confirm whether the commit message has been updated or not by running git log –oneline

image.png

As we can see that the commit message has been updated. Interestingly, git has updated the commit id as well.

That's it. All Done!

Note: This technique works for the latest commit only. If you want to edit the commit message of other commits then head over to the second approach. Hint: we will be working with some cool commands 😎

CASE II: Editing git commit messages using git interactive rebasing techniques

This technique is a little advanced than the above one but is very handy when you want to edit your commit message which is not the latest one or you want to update multiple commit messages or you have already pushed the changes to the remote server.

Review: What the heck is git rebase?

Git rebasing is one of the core concepts of git. Git rebase is a powerful technique that is widely used in rewriting histories. What git rebase does is that it creates a new node and points to the previous parent and future commits will be on top of it. In other words, git rebasing creates a new base commit node for a sequence of nodes. Confusing right? Let’s have a look at this image

git rebase.png

We can see in fig I that there is a local main branch whose upstream is set to some remote main branch of any repo. Then, to add some features a new local feature branch has been created and some commits are done, meanwhile, the remote main branch has a few new commits which do not reflect the local main. To make the local main branch know about the remote updates git pull command is run.

Now, it’s time to push our features to the remote branch, but for that, we have to join the feature branch to the local main branch so that we can push the local main to the remote main. We can do this either by using the merge command or by using rebase command. Here, I will be using rebasing to illustrate how to rebase workflow changes base nodes for a sequence of nodes.

Here, in fig -II we can see that the featured branch is now rebased to the local main branch; clearly, the base node has been updated for the feature branch. Now that everything is up-to-date ( it is equivalent to merge ) we can push the local branch to the remote branch.

git rebase-2.png

There are several use cases of rebasing in git but here we are concerned with editing already committed messages. Editing commit messages with interactive rebasing can be used in the following cases:

  1. When you want to edit multiple commit messages
  2. When you want to edit one specific message but not the latest one

Syntax:

git rebase --interactive HEAD~<integer: number_of_commits_you want_to_roll_back>

Note: --interactive can be replaced by -i

Suppose you want to edit the most latest commit message then the command will be: $ git rebase –interactive HEAD~1

Example

Suppose that you have a project in progress and you have already done some commits locally. Let us assume that the following commits have been done. And now your project manager says to replace 2nd, 3rd, 4th with second, third, and four respectively before they can merge it to the main branch. Now that you have to edit messages of three commit messages you cannot use the first approach. This is where the role of rebasing comes into action.

2.png

now run the command $ git rebase -i HEAD~3 The output should be similar to:

3_Screenshot 2021-12-18 234217.png

Now we have to tell the gitConfig that we want to update this message, so replace pick with rewording, just like below: (do not modify the message here). Suppose, you only want to update the commit message of cbf2a08 then you would replace pick with rewording for that specific commit only. In our case the output should look like this:

4_reenshot 2021-12-18 234340.png

NOTE: Use i to edit the file and once editing is done press Esc +: +wq to save and quit the file.

Once you save and exit from the editor, you’ll be prompted with another similar file. See below:

5_Screenshot 2021-12-18 234728.png PS. I have removed unwanted comments

Now you can press i to edit the message, write whatever should be written, in our case it is second commit

6_Screenshot 2021-12-18 234932.png

Again press Esc + : + wq to save and quit the editor.

In this way, you can keep doing the number of messages you want to edit. Here we have done three messages. Once, this recursive task is done you can confirm the updated commit messages by running the same git log –oneline

7_Screenshot 2021-12-18 235103.png

Done! Now that commit messages are updated. There can be two possible scenarios:

  1. you did not push the faulty commit if this is the case then you can push the latest updated changes to remote as usual by following $ git push <repo_name> <branch_name>
  2. you had already pushed the faulty commit to remote if this is the case then you have to tell the remote repository that you have updated the commit message(s) once local edits are done.

Push the edits to the repository again after updating the commit message

DISCLAIMER: Follow along if and only if you have a faulty commit on the remote repository and is yet to be merged or is rejected (in case of a pull request) if this is the case then you have to forcefully push the changes to remote as a normal push will not detect any changes.

The safest way of force push is by using the flag --force-with-lease flag. This flag will halt the process if encounters any conflicts. Syntax: git push <repo name> <branch name> --force-with-lease Example:

To forcefully push the above changes to remote we can run: git push origin gaurav --force-with-lease

That's it. All Done!

That's it for this tutorial. If you have anything to ask, please feel free to leave a comment below. Thank you!