Horje
How to Change the Commit Author for a Single Commit?

Changing the author of a single commit in Git can be done using an interactive rebase. This process involves rewriting the commit history, so it’s important to proceed carefully, especially if you’ve already shared the commits with others. Here’s a step-by-step guide on how to change the commit author for a single commit

Steps to Change Commit Author

Identify the Commit Hash:

First, find the hash of the commit you want to change. You can use the git log command to see the commit history

git log

This will show a list of commits with their hashes and other details. Note the hash of the commit you want to change.

Start Interactive Rebase:

Begin an interactive rebase starting from the commit before the one you want to change. If the commit you want to change is, for example, the third commit from the most recent one, you should start the rebase three commits back.

git rebase -i HEAD~3

Adjust the number (3 in this case) according to how far back the commit is.

Mark the Commit for Edit:

An editor will open showing a list of commits. Find the commit you want to change and replace the word pick with edit.

pick abc1234 First commit
edit def5678 Commit to change
pick ghi9101 Another commit

Change the Commit Author:

After the rebase process stops at the commit you marked for editing, change the author information using the git commit –amend –author command.

git commit --amend --author="New Author Name <[email protected]>"

This will open the commit message editor. You don’t need to change the message unless you want to; just save and close the editor.

Continue the Rebase:

After amending the commit, continue the rebase process.

git rebase --continue

Git will reapply the subsequent commits automatically.

Force Push (if necessary):

If you have already pushed the commits to a remote repository, you will need to force push to update the remote history. Be cautious with force pushing as it can overwrite the remote history and affect other collaborators.

git push --force

Example Workflow

Assume the commit hash you want to change is def5678 and it is the second commit from the latest one. Here’s the detailed workflow:

View commit history:

git log

Output:

commit ghi9101 (HEAD -> main)
Author: Current Author <[email protected]>
Date: Fri May 21 14:32:00 2024 +0200

Another commit

commit def5678
Author: Current Author <[email protected]>
Date: Thu May 20 14:31:00 2024 +0200

Commit to change

commit abc1234
Author: Current Author <[email protected]>
Date: Wed May 19 14:30:00 2024 +0200

First commit

Start interactive rebase:

git rebase -i HEAD~2

Edit the rebase list:

Change the second commit from pick to edit:

pick abc1234 First commit
edit def5678 Commit to change

Amend the commit:

git commit --amend --author="New Author <[email protected]>"

Continue the rebase:

git rebase --continue

Force push if the commits are already pushed:

git push --force

Conclusion

Changing the author of a single commit involves using an interactive rebase to edit the commit history. This method allows you to amend the commit with the new author information. Always be cautious with rebasing, especially if you’re working in a shared repository, as it rewrites commit history.




Reffered: https://www.geeksforgeeks.org


Git

Related
How to Use Git Rebase? How to Use Git Rebase?
How to Discard Changes in Git? How to Discard Changes in Git?
How to Use the Git Submodule Init and Update Command? How to Use the Git Submodule Init and Update Command?
How to Clone all Remote Branches in Git? How to Clone all Remote Branches in Git?
How to Ignore a File in Git? How to Ignore a File in Git?

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
14