Change the Last Commit's Message

GitGitBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

Git is a powerful version control system that allows developers to track changes in their codebase. One of the key features of Git is the ability to modify the commit history. In this lab, you will learn how to change the last commit's message without changing its contents.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL git(("`Git`")) -.-> git/BasicOperationsGroup(["`Basic Operations`"]) git/BasicOperationsGroup -.-> git/commit("`Create Commit`") subgraph Lab Skills git/commit -.-> lab-12768{{"`Change the Last Commit's Message`"}} end

Change the Last Commit's Message

Imagine you have just committed some changes to your Git repository, but you realize that you made a typo in the commit message. You want to correct the mistake without changing the actual changes you made. How can you do this?

To demonstrate how to change the last commit's message, let's use the repository from https://github.com/labex-labs/git-playground. Follow these steps:

  1. Clone the repository, navigate to the directory and configure the identity:
    git clone https://github.com/labex-labs/git-playground
    cd git-playground
    git config --global user.name "your-username"
    git config --global user.email "your-email"
  2. Correct the commit message of the last commit to read "Fix the network bug":
    git commit --amend -m "Fix the network bug"
    This will open your default text editor where you can modify the commit message. Save and close the editor to complete the process.
  3. Verify that the commit message has been changed:
    git log --oneline

You should see the updated commit message in the log:

54b830b (HEAD -> master) Fix the network bug
cf80005 Added file1.txt
b00b937 Initial commit

Summary

In this lab, you learned how to change the last commit's message without changing its contents using the git commit --amend -m <message> command. This can be useful when you need to correct a mistake in the commit message or provide more context about the changes you made. Remember to use this command with caution, as it can modify the commit history and potentially cause issues for other developers working on the same repository.

Other Git Tutorials you may like