Introduction
🧑💻 New to Git or LabEx? We recommend starting with the Quick Start with Git course.
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.
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:
- 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" - Correct the commit message of the last commit to read "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.git commit --amend -m "Fix the network bug" - 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.