Wrong git commit ? Don’t worry this git command will help you

Pratiyush Prakash
3 min readDec 28, 2021

Git has become an essential part of our life. Whether you work in big multinational software company or an early stage startup you must learn how to use git. And don’t feel that you are alone, we have all been through the journey and it is scary when you are new to the company and you messes up a git commit and now you don’t know what to do.

Don’t worry, there is a very simple way to resolve it. Let’s jump right into it.

GIT COMMAND

git rebase

Let’s understand this through an example.

Let’s say you made this commit but unfortunately there was a typo in the message, so let’s see how we can fix the message or entirely drop the commit.

git commit
git rebase -i HEAD~2 // -i is for interactivity
// the number is configurable, how many commits do you want to refactor.

When you run this command, a very intuitive window will appear which will ask you what exactly do you want to do with these commits.

Interactive window

Here you can see, my incorrect commit is showing up. There are also various commands you can see and each one have a description describing what does it do. By default the pick command is there before every commit. But you can edit the command for one or more commits as per your use case and then save it.

In our case we just have to reword the commit message and use the same commit. So we will use reword or r command. To do that you have to go to INSERT mode. (Click on Insert on your keyboard).

Reword in rebase interactive window

After you are satisfied with the changes. You can save it. (Click ESC -> :wq -> ENTER). As we have chosen reword command it will open another window with that single commit and ask you to reword it. If you had chosen drop then you can ignore this step.

Reword commit message

In this window, you can reword your message by going to INSERT mode and then save it.

Once you are done, it will show the success message, and voila you have successfully reworded your commit message. You can check the logs again.

NOTE: (If you are running commands like squash , merge etc. sometime it can create conflicts and you have to resolve it and then run git rebase --continue to continue or git rebase --abort to abort it. But rarely you will see this, so don’t worry.)

Practice is the best way to learn more and become proficient in git. Just as a challenge you can experiment with all the different commands which you see in the interactive window when you run the rebase command in your local project repo.

If you want to read more about it, go through this doc.

  1. https://git-scm.com/doc

--

--

Pratiyush Prakash

Full stack Dev and Lead @ Texas Instruments. Follow me for short articles on software development.