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 made to their codebase. One of the features of Git is the ability to modify the last commit's author without changing its contents. This can be useful in situations where the author of a commit needs to be updated for accuracy or accountability purposes.
Change the Last Commit's Author
You have just made a commit to your Git repository, but you realized that the author's name and email address are incorrect. You want to update the author's information without changing the contents of the commit. How can you achieve this using Git?
To change the last commit's author, you can use the git commit --amend command. This command allows you to modify the last commit in your Git repository. Here's an example of how you can change the author's name and email address:
- Clone the Git repository named
https://github.com/labex-labs/git-playgroundto your local machine:
git clone https://github.com/labex-labs/git-playground.git
- Configure Git's identity information using your GitHub account:
cd git-playground
git config user.email "your email"
git config user.name "your username"
- Use the
git commit --amendcommand to modify the last commit's author and save the contents:
git commit --amend --author="Duck Quackers <cool.duck@qua.ck>"
- Verify that the author's information has been updated:
git log
You should see that the last commit's author is now Duck Quackers:
commit d5a385cc354f3528472a215b66cbb7c628ba47d5
Author: Duck Quackers <cool.duck@qua.ck>
Date: Wed Apr 26 14:16:25 2023 +0800
Added file2.txt
Summary
In this lab, you learned how to change the last commit's author without changing its contents using the git commit --amend command. This can be useful in situations where the author's information needs to be updated for accuracy or accountability purposes.