About Abhijeet Sutar

Abhijeet (ajduke) is self-taught, self-organized software developer. His current go to language is Java and also he is exploring other languages such as Scala, Ruby.

Changing commit message in Git

Introduction

Git is distributed version control system and now-a-days, it is de facto version control system for number of open source and closed source software projects, including Eclipse. Git is amazing tool to use and has lot of flexibility.

Talking about flexibility, i was amazed by an interesting feature of git, that it allows you to edit and change previous commit messages.

There are basically two ways do it such as-

  1. Using amend switch
  2. Using rebase

Lets have a look at each of these.

Using amend switch

This is simple way to change commit message for your most recent commit. In this we make use of git commit with amend switch. Usage is shown as follows,

git commit –amend -m “new message”

Note: i am using some of git aliases, such git ol displays the git commit log in proper format , you can browse for some aliases in my .gitconfig in my github repo

Consider the following commits in my repo.

png;base644839458eb1e4ec7c

It is same same commiting but with added amend switch, following shows actual usage:

png;base64d54e553bed62a293

This is quick and nice way for editing your most recent commit message.

Using rebase

Whenever you want to change the multiple or more than one commit messages or one other than recent commit then this is a nice way of doing it. Although rebase is very powerful that can do lot of things. But for this article let’s focus on only editing the commit message.

We use following command for our purpose,

git rebase -i <sha1 of previous commit msg> or <relative-HEAD-pointer>

Consider git repo with few commits shown as follows

png;base6438d9d1f1fd759a9d

Above commit logs are shown in descending order in date-time order when it was committed, hence latest commit is top and least recent is below that and so on.

Suppose, you want to change the 2nd commit message (from bottom) then do it as follows-

As you want to change 2nd (156ce8e) commit log  then we specify SHA1 of one prior to that , in our case 1st (6343229) commit log, for rebase command.

png;base64d1d4ca633f33f58d

Following is command for doing it – Note, SHA1 hash

png;base64f72c4eb95f69bbb8

After you hit enter, you will see following rebase file for editing-

png;base64aa073d994a8c0fbf

In above, focus on top of file which is important to us. It shows commits with some commands /markers at start.

Those are rebase command/markers for several purpose, but for our case we use ‘reword’ which solves our purpose.

Go ahead and change marker of first line (which is our 2nd commit), ‘pick’ to either ‘r’ or ‘reword’ and retain others as it as shown in following

png;base6412b31105924bf0e2

png;base64ac58386caae13d45

After this exit file editing by pressing Ctrl+X, (some other way depend on editor ) then immediately after you will see commit dialog box for new commit message. Enter new commit message and then exit.

png;base64999bd49b0709e191

then we are done. after that you should see following

png;base64f2651dd74a8171e8

Check commit logs for new commit message

png;base64a4cdf1893a49fcf4

Above steps are for changing only one commit message but for editing, changing multiple, you can change marker of the other commits to ‘reword’ as per requirement and accordingly after that git will ask for entering new commit message box for each marked commit message and you are done

png;base64a4cdf1893a49fcf4

Thats all folks for this article.
 

Reference: Changing commit message in Git from our JCG partner Abhijeet Sutar at the ajduke’s blog blog.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.

Leave a Reply


three − = 2



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close