Select Page

How to change the author of the last commit in Git?

by | Mar 10, 2023 | Miscellaneous | 0 comments

Git is a popular version control system that allows you to track changes to your code and collaborate with other developers. One of the common tasks in Git is changing the author of a commit. Sometimes, you may need to update the author information for a commit due to various reasons such as committing with the wrong user email or name.

In this article, we’ll explore two methods to change the author of the last commit in Git.

Method 1: Using the –author option

The –author option allows you to specify a new author for a commit. To change the author of the last commit, you can use the following command:

git commit --amend --author="New Author Name <newemail@example.com>"

This command will open your default text editor and allow you to modify the commit message as well as change the author information. Replace “New Author Name” and “newemail@example.com” with the desired new author name and email address.

Alternatively, you can use the –no-edit flag to skip opening the text editor and keep the existing commit message:

git commit --amend --no-edit --author="New Author Name <newemail@example.com>"

After running this command, Git will create a new commit with the updated author information, and the previous commit will be replaced by the new one. However, if you have already pushed the original commit to a remote repository, you will need to force-push the updated commit using:

git push --force

Note that force-pushing can be dangerous if you are collaborating with others on the same branch, as it can overwrite their work. So, use it with caution and make sure to communicate with your collaborators beforehand.

Method 2: Using the –reset-author option

The –reset-author option allows you to reset the author information for a commit to the default author specified in your Git configuration. To change the author of the last commit using the current Git config user email, you can use the following command:

git commit --amend --reset-author

This command will use the email address associated with your current Git config user to update the author information for the last commit. If you want to change the name or email address, you can set them using the git config command:

git config --global user.name "New Author Name"
git config --global user.email "newemail@example.com"

Once you have set the new author information, you can run the git commit –amend –reset-author command to update the last commit with the new author information.

After running this command, Git will create a new commit with the updated author information, and the previous commit will be replaced by the new one. However, if you have already pushed the original commit to a remote repository, you will need to force-push the updated commit using:

git push --force

Note that force-pushing can be dangerous if you are collaborating with others on the same branch, as it can overwrite their work. So, use it with caution and make sure to communicate with your collaborators beforehand.

Conclusion

Changing the author of a commit in Git is a simple but important task. In this article, we explored two methods to change the author of the last commit using the –author and –reset-author options. Remember that force-pushing can be dangerous, so use it with caution and communicate with your collaborators beforehand.

Kirk Makse
Author: Kirk Makse

Greetings, my name is Kirk. I’m the creator of Rails Zone and a passionate Ruby on Rails developer. During my free time, I develop helpful tools and tutorials that aid fellow Ruby on Rails developers in creating applications with improved efficiency and speed.