views:

9267

answers:

6

I was writing a simple script in the school computer, and commiting the changes to git (in a repo that was in my pendrive, cloned from my computer at home). After several commits I realized I was commiting stuff as root.

Is there any way to change the author of these commits to my name?

+9  A: 

Changing the author (or committer) would require re-writing all of the history. If you're okay with that and think it's worth it then you should check out git filter-branch. The man page includes several examples to get you started. Also note that you can use environment variables to change the name of the author, committer, dates, etc. -- see the "Environment Variables" section of the git man page.

Pat Notz
+30  A: 

One liner:

git-filter-branch --env-filter "export GIT_AUTHOR_NAME='New name'; export GIT_AUTHOR_EMAIL='New email'" HEAD
Brian Gianforcaro
Your "broken up" version doesn't have any effect: you need to change the environment variables inside an env-filter fragment, otherwise the setting you exported before invoking filter-branch are overwritten by the values from the commit for each filter run.
araqnid
Thanks for the catch, misread the documentation.
Brian Gianforcaro
Don't forget GIT_COMMITTER_NAME and GIT_COMMITTER_EMAIL, which will likely be wrong too if the author is.
foolip
Minor point, the export is actually superfluous although it does no harm. e.g. git-filter-branch --env-filter "GIT_AUTHOR_NAME='New name';GIT_AUTHOR_EMAIL='New email'" HEAD.
Alec the Geek
Yes, but don't forget the committer's name/email. What worked for me was `git filter-branch -f --env-filter "GIT_AUTHOR_NAME='Newname'; GIT_AUTHOR_EMAIL='newemail'; GIT_COMMITER_NAME='Newname'; GIT_COMMITTER_EMAIL='newemail';" HEAD`Otherwise git will keep track of the old name as a committer!
Olivier
+33  A: 

As demonstrated here, you can also do:

git filter-branch --commit-filter '
        if [ "$GIT_COMMITTER_NAME" = "<Old Name>" ];
        then
                GIT_COMMITTER_NAME="<New Name>";
                GIT_AUTHOR_NAME="<New Name>";
                GIT_COMMITTER_EMAIL="<New Email>";
                GIT_AUTHOR_EMAIL="<New Email>";
                git commit-tree "$@";
        else
                git commit-tree "$@";
        fi' HEAD
Rognon
+26  A: 

You could do git rebase -i <some HEAD before all of your bad commits>. Then mark all of your bad commits as "edit" in the rebase file, and when git asks you to amend each commit, do git commit --amend --author="<new author, in "Author Name <[email protected]>" format>, edit or just close the editor that opens, and then do git rebase --continue to continue the rebase.

I don't know if there is a more streamlined way to do this with multiple commits.

asmeurer
That would take *forever* if you had even as few as a hundred commits to pick through.
Matthew Iselin
Great for the odd commit though - useful if you're pairing and forget to change the author
mloughran
+1 for mentioning the usecase for the typical one-mistake fix: git commit --amend --author=username
Nathan Kidd
This is perfect, my most common usecase is that I sit down at another computer and forget to set up author and thus usually have < 5 commits or so to fix.
Zitrax
+7  A: 

For a single commit:

git commit --amend --author="<new author, in "Author Name <[email protected]>" format>"

(extracted from asmeurer's answer)

blueyed
+6  A: 

Github has a nice solution, which is the following shell script:

#!/bin/sh

git filter-branch --env-filter '

an="$GIT_AUTHOR_NAME"
am="$GIT_AUTHOR_EMAIL"
cn="$GIT_COMMITTER_NAME"
cm="$GIT_COMMITTER_EMAIL"

if [ "$GIT_COMMITTER_EMAIL" = "[email protected]" ]
then
    cn="Your New Committer Name"
    cm="Your New Committer Email"
fi
if [ "$GIT_AUTHOR_EMAIL" = "[email protected]" ]
then
    an="Your New Author Name"
    am="Your New Author Email"
fi

export GIT_AUTHOR_NAME="$an"
export GIT_AUTHOR_EMAIL="$am"
export GIT_COMMITTER_NAME="$cn"
export GIT_COMMITTER_EMAIL="$cm"
'
Olivier
I basically have this same thing posted at my blog so +1 :)
Xeross