How do you commit code as a different user?

2019-01-21 06:18发布

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

Assuming I know the commit author and the date/time the change was made, is there a Git command that allows me to do this? I'm assuming there is, because git-p4 does something similar. I'm just asking for the best way to do it.

2条回答
该账号已被封号
2楼-- · 2019-01-21 06:39

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

That is the correct behavior in most cases, but if for some reason you need to manually override the committer information as well, use the GIT_COMMITTER_NAME and GIT_COMMITTER_EMAIL environment variables (there is a GIT_COMMITTER_DATE as well). 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 look like it was authored and committed by the specified user.

查看更多
别忘想泡老子
3楼-- · 2019-01-21 06:50

Check out the --author option for git commit:

From the man page:

--author=<author>

Override the commit author. Specify an explicit author using the standard A U Thor <author@example.com> format. Otherwise <author> is assumed to be a pattern and is used to search for an existing commit by that author (i.e. rev-list --all -i --author=<author>); the commit author is then copied from the first such commit found.

查看更多
登录 后发表回答