How to change a Git commit message after a push

Key takeaways:

  • Commit messages to explain the changes made in a code commit. They help developers understand the codebase's history.

  • The git commit --amend -m "New message" command allows us to update the message for the latest commit in your local repository.

  • Using git push --force-with-lease repository-name branch-name is safer than git push --force when pushing an updated commit message. --force-with-lease aborts the push if there are upstream changes to the repository, preventing conflicts.--force can overwrite other developers' work.

  • To change messages for older commits, we can use the interactive rebase feature in Git, which allows us to review and modify previous commits.

Commits to Git are accompanied by a commit message that explains what changes the commit has made to the code. However, a situation can arise where the commit message will need to be changed after it has been pushed to make it more meaningful. ​Meaningful messages help other developers understand changes in the code. As Martin Fowler once said:

Good programmers write code that humans can understand.

svg viewer

Amend the last Git commit message

If the message to be changed is for the latest commit to the repository, then the following commands are to be executed:

  1. git commit --amend -m "New message"
    
  2. git push --force repository-name branch-name
    

Note: Using --force is not recommended unless we’re absolutely sure that no one else has cloned our repository after the latest commit.

A safer alternative is to use the following:

git push --force-with-lease repository-name branch-name

Unlike --force, which destroys any changes someone else has pushed to the branch, --force-with-lease aborts if there’s an upstream change to the repository.

Hands-on practice

Let us run the following command in the terminal given below. Following the code snippet given below, you'll first write the text # Initial Commit in the README.md file. To add files for Git to track, use the git add command. Next, pass your email and username in the git config command and run git commit to save the changes to the local repository. Here we use the -m flag to set the commit message: Initial commit. To change commit message, use the git commit command with the --amend option.

cd git
git init
echo "# Initial Commit" > README.md
git add README.md
git config --global user.email <Add email here>
git config --global user.name <Add username here>
git commit -m "Initial commit"
git log --oneline
git commit --amend -m "New message"
git log --oneline
Commands for updating the commit message

Let’s execute the above commands one by one and see how they change the commit message:

Terminal 1
Terminal
Loading...

Changing older commit messages

If the message needs to be amended for an older commit, then the interactive rebase tool can be used:

  1. Navigate to the repository that contains the commit we want to amend and open a terminal window.

  2. Use git rebase -i HEAD~n command to display a list of the last nn commits in our default text editor. For example, the following command displays a list of the last three commits in our current branch:

    git rebase -i HEAD~3 
    

The list will be similar to this:

pick e499d89 Delete CNAME
pick 0c39034 Better README
pick f7fde4a Change the commit message but push the same commit.

# Rebase 9fdb3bd..f7fde4a onto 9fdb3bd
#
# 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 the previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
#
# 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
  1. Replace pick with reword before each commit message that needs to be changed:
    pick e499d89 Delete CNAME
    reword 0c39034 Better README
    reword f7fde4a Change the commit message but push the same commit.
    
  2. Save and close the commit list file.
  3. In each resulting commit file, type the new commit message, save the file, and close it.
  4. Force push the amended commits using the git push --force command.

Frequently asked questions

Haven’t found what you were looking for? Contact Us


What is version control?

Version control is a management system that allows you to record and track changes in your source code and files, allowing you to recall specific versions later. It’s like a Google Doc for programming, where you can collaborate with multiple people working on the same code and see the source code’s history.


Can we edit a commit message after pushing?

Yes, we can edit a commit message after pushing using the git commit --amend -m "New message" command.


How do we commit changes in Git using cmd?

To commit changes in Git using cmd:

  • Stage the changes: Use the git add <file> command.
  • Commit the changes: Use the git commit -m "Your commit message" command.

What is the difference between git commit and git push?

The git commit command is used to save our changes to the local repository, while the git push command is used to upload local repository content to a remote repository.


How do we see the commit history?

We can see the commit history using the git log command.


What is the use of the git revert command?

We can revert the changes made to a repository using git revert command.


Free Resources

Copyright ©2024 Educative, Inc. All rights reserved