Git

Git Edit Commit Message Example

In this post, we present a Git Edit Commit Message Example.

1. Introduction

Performing a commit operation in Git is not something to be taken lightly. Suppose we unintentionally apply the wrong commit message. Are we doomed? Fortunately, we can edit the message after the fact.

In this example, we will demonstrate how to edit a commit message when the commit has yet to be pushed to a remote repository. We will also show how to edit a commit message when the commit has already been pushed to a remote repository.

1.1 Tools Used in this Example

  • Git 2.17
  • Bitbucket

Git downloads are available here: https://git-scm.com/downloads.

Bitbucket is a Git repository management system for development teams. You can sign up for a free Bitbucket account at https://bitbucket.org/account/signup/.

2. Git Edit Commit Message Example

2.1 Editing the Message of the Last Commit

Suppose that we add several dependencies to the pom.xml of a Maven project. After testing the modification, we decide to stage and commit the change to our local repository:

$ git commit -a -m 'Added spring-security-web dependency'
[master 90b8029] Added spring-security-web dependency
 1 file changed, 3 insertions(+), 3 deletions(-)

Note: The -m option allows us to add a commit message inline.

After some consideration, we realize the commit message is misleading since we added other dependencies, not the spring-security-web dependency only. As all dependencies are related to Spring Security, a better message is “Added Spring Security dependencies”. We can edit the commit message using the amend option:

$ git commit --amend -m 'Added Spring Security dependencies'
[master 7bf2d01] Added Spring Security dependencies
 Date: Thu Sep 6 18:16:32 2018 -0700
 1 file changed, 3 insertions(+), 3 deletions(-)

Note: If you have staged any other changes since the last commit, they too will be included in the commit so this option should be used with care.

If you run the git log command, you will see that message for the last commit has been updated.

$ git log --oneline
7bf2d01 Added Spring Security dependencies
945ceee Merge branch 'master' of https://bitbucket.org/gilbert_lopez/ebookstore.git
9492190 Spring Security SQL script for USERS and Authorities tables.
c464a25 README updated with Spring Security Database Schema location information.
5b61da8 README.md edited online with Bitbucket
53571f6 Added Spring Security using JDBC authentication.
61d10f2 Added form field validation for Book domain model.
c8659b8 Moved image file saving functionality to the service layer.
a0fcb77 Added edit/update function
215bb63 Added upload file functionality.
c9e85b0 Check in remaining files.
a791f39 Merge branch 'master' of https://gilbert_lopez@bitbucket.org/gilbert_lopez/ebookstore.git
92b22b1 README.md created online with Bitbucket
302bb61 Initial e-Commerce project check-in

The oneline option directs the log command to display the commit’s abbreviated hash and message only.

2.2 Editing the Message for a Recent Commit

If you want to edit the message for a commit other than the most recent one, you can use the rebase tool. Let’s run the Git log command to view a list of commits in our local repository.

$ git log --oneline
7bf2d01 Added Spring Security dependencies
945ceee Merge branch 'master' of https://bitbucket.org/gilbert_lopez/ebookstore.git
9492190 Spring Security SQL script for USERS and Authorities tables.
c464a25 README updated with Spring Security Database Schema location information.
5b61da8 README.md edited online with Bitbucket
53571f6 Added Spring Security using JDBC authentication.
61d10f2 Added form field validation for Book domain model.
c8659b8 Moved image file saving functionality to the service layer.
a0fcb77 Added edit/update function
215bb63 Added upload file functionality.
c9e85b0 Check in remaining files.
a791f39 Merge branch 'master' of https://gilbert_lopez@bitbucket.org/gilbert_lopez/ebookstore.git
92b22b1 README.md created online with Bitbucket
302bb61 Initial e-Commerce project check-in

Suppose you want to edit the message for the third commit in the list – “Spring Security SQL script for USERS and Authorities tables”. To do this, run the following command:

$ git rebase -i HEAD~3

(The -i options allows you to run the rebase tool interactively.)

Since we want to edit the third commit message, we specify 3 for the number of commits, starting from the latest, that we would like to edit.

The rebase tool will display a file that includes the last three commits and a help section for different actions you can apply. Since we want to change the commit message, we’ll use the ‘reword’ option. Replace ‘pick’ with ‘reword’ (or just ‘r’) in front of the commit we would like to update and save the file.

Git rebase Command Output

r 9492190 Spring Security SQL script for USERS and Authorities tables.
pick cb2a773 README updated with Spring Security Database Schema location information.
pick 13775d3 Added Spring Security dependencies

# Rebase 5b61da8..13775d3 onto 5b61da8 (3 commands)
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out

This opens the COMMIT_EDITMSG file for the commit, where you can edit the message. Make your changes and save the file.

Edit Commit Message

Added Spring Security SQL scripts for USERS and Authorities tables.

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date:      Sun May 20 21:26:47 2018 -0700
#
# interactive rebase in progress; onto 5b61da8
# Last command done (1 command done):
#    r 9492190 Spring Security SQL script for USERS and Authorities tables.
# Next commands to do (2 remaining commands):
#    pick cb2a773 README updated with Spring Security Database Schema location information.
#    pick 13775d3 Added Spring Security dependencies
# You are currently editing a commit while rebasing branch 'master' on '5b61da8'.
#
# Changes to be committed:
#       new file:   ebookstore/src/main/resources/spring_security.sql
#

Run git log –oneline again to verify that the message has been changed.

Git log Command Output

$ git log --oneline
95fbca1 Added Spring Security dependencies
f878c94 README updated with Spring Security Database Schema location information.
c3045db Added Spring Security SQL scripts for USERS and Authorities tables.
5b61da8 README.md edited online with Bitbucket
53571f6 Added Spring Security using JDBC authentication.
61d10f2 Added form field validation for Book domain model.
c8659b8 Moved image file saving functionality to the service layer.
a0fcb77 Added edit/update function
215bb63 Added upload file functionality.
c9e85b0 Check in remaining files.
a791f39 Merge branch 'master' of https://gilbert_lopez@bitbucket.org/gilbert_lopez/ebookstore.git
92b22b1 README.md created online with Bitbucket
302bb61 Initial e-Commerce project check-in

2.3 Editing the Message for the Last Commit of a Remote Repository

Note: This section assumes you have access to a remote repository.

If you want to edit a commit message but have already pushed the changes to a remote repository, you must do two things.

  • Edit the commit message locally
  • Push the commit to the remote repository using the force option

Warning: This procedure is risky if the repository is shared with others.

Before starting, log in to GitHub or Bitbucket to view your commits for the repository.

Git Edit Commit Message - Commit View in Bitbucket
Commit View in Bitbucket

First, we’ll change the message for the last commit locally.

$ git commit --amend -m 'Added Spring Security dependencies to pom.xml'
[master fcafda0] Added Spring Security dependencies to pom.xml
 Date: Thu Sep 6 18:16:32 2018 -0700
 1 file changed, 3 insertions(+), 3 deletions(-)

Secondly, we’ll push the change to the remote repository with the force (-f) option:

$ git push -f origin master
Counting objects: 4, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 438 bytes | 0 bytes/s, done.
Total 4 (delta 1), reused 0 (delta 0)
To https://bitbucket.org/gilbert_lopez/ebookstore.git
 + 6a5ac0d...fcafda0 master -> master (forced update)

If you go back to your commit view and refresh the page, you will see the change.

Git Edit Commit Message - View in Bitbucket
Commit View in Bitbucket

3. Summary

In this post, we demonstrated how to edit commit messages with Git.

Gilbert Lopez

Gilbert Lopez is an application developer and systems integration developer with experience building business solutions for large and medium-sized companies. He has worked on many Java EE projects. His roles have included lead developer, systems analyst, business analyst and consultant. Gilbert graduated from California State University in Los Angeles with a Bachelor of Science degree in Business.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Brice
Brice
6 years ago

I strongly recommend to use the option —force-with-lease instead of —force. This will prevent from overriding non fetched commits of the branch you want to push on.

Back to top button