How do you execute code as another user?

I want to be able to do this for a script. I essentially recreated the entire version history of some code in Git - it currently uses a different version control system. I need a script to be able to add to commits to Git, preserving the original author (and date).

Assuming I know the author of the commit and the date / time of the change, was there a Git command that allows me to do this? I guess there is, because git -p4 does something similar. I am just asking for a better way to do this.

+75
git command-line commit
Sep 12 '10 at 22:35
source share
2 answers

Check the --author git commit parameter:

On the page :

--author=<author>

Replace author commit. Indicate the explicit author using the standard format AU Thor <author@example.com> . Otherwise, <author> is considered a model and is used to search for an existing commit to this author (ie rev-list --all -i --author=<author> ); the author of the commit is then copied from the first such commit found.

+109
Sep 12 '10 at 22:59
source share

Just add to this: The --author option mentioned in the accepted answer will only override the author, not the commit information about the commit.

In most cases, this is the correct behavior, but if for some reason you need to manually override the information <commander , use the GIT_COMMITTER_NAME and GIT_COMMITTER_EMAIL environment variables (there is GIT_COMMITTER_DATE ). See Git-Internals-Environment-Variables

 $ GIT_COMMITTER_NAME="New Name" GIT_COMMITTER_EMAIL="name@email.com" git commit --author="New Name <name@email.com>" 

This will make the commit appear as if it were the author and committed by the specified user.

+55
May 19 '15 at 20:48
source share



All Articles