Updating Email ID and Author Name from multiple commits in Git


Multiple Commits:

git filter-branch --commit-filter '
        if [ "$GIT_COMMITTER_NAME" = "" ];
        then
                GIT_COMMITTER_NAME="";
                GIT_AUTHOR_NAME="";
                GIT_COMMITTER_EMAIL="";
                GIT_AUTHOR_EMAIL="";
                git commit-tree "$@";
        else
                git commit-tree "$@";
        fi' HEAD

Single Commit:

To update the author to a specified name [The committer will be set to your configured user in git config user.name and git config user.email]

git commit --amend --author "New Author Name <email@address.com>"

To set both the author and the committer:

git -c user.name="New Author Name" -c user.email=email@address.com commit \
--amend --reset-author

Interactive Rebase

git rebase -i -p <some HEAD before all of your bad commits>

Then mark all of your bad commits as “edit” in the rebase file. If you also want to change your first commit, you have to manually add it as first line in the rebase file (follow the format of the other lines). Then, when git asks you to amend each commit, do

git commit --amend --author "New Author Name <email@address.com>"

edit or just close the editor that opens, and then do

git rebase --continue

to continue the rebase.

You could skip opening the editor altogether here by appending –no-edit so that the command will be:

git commit --amend --author "New Author Name <email@address.com>" --no-edit && \
git rebase --continue

If there are any merge commits between the current HEAD and your , then git rebase will flatten them (if you use GitHub pull requests, there are going to be a ton of merge commits in your history). This can very often lead to very different history (as duplicate changes may be “rebased out”), and in the worst case, it can lead to git rebase asking you to resolve difficult merge conflicts (which were likely already resolved in the merge commits). The solution is to use the -p flag to git rebase, which will preserve the merge structure of your history. The manpage for git rebase warns that using -p and -i can lead to issues, but in the BUGS section it says “Editing commits and rewording their commit messages should work fine.”

Reference:

About Sayak Sarkar

Web Ninja, Open Source Evangelist, Mozillian, Geek, Currently working as a Web Developer at Red Hat. In the past I've worked at Mozilla Foundation, Software Development & Research Cell at SICSR. Originally from Kolkata, West Bengal. Open Source Contributor with a special interest in Web Development. Follower of all updates related to Free and Open Source Software.

Posted on January 31, 2019, in git, How-to Guides, Technology and tagged , , , , , , , , , , , , , , , , . Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: