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 challenge, 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-12682{{"`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?

Tasks

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

  1. Navigate to the repository directory and configure your GitHub identity.
  2. Correct the commit message of the last commit to read "Fix the network bug".
  3. Verify that the commit message has been changed.

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 challenge, 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