How do you commit code as a different user?

Viewed 78010

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.

3 Answers

Use -c option along with git-commit to override any previous configuration. It will not touch your global/project configuration. For example, to override name and email:

git -c user.name='My Name' -c user.email='my@email.com' commit -m "Custom message"

However, if you intend to keep it as an additional setting, I would suggest to use an alias. Edit your ~/.gitconfig file and append a new alias for each non-default user and email.

[user]
  name = My Name
  email = default@email.com

[alias]
  commit-x = -c user.name='My X Name' -c user.email='mr_x@email.com' commit
  commit-y = -c user.name='My Y Name' -c user.email='mr_y@email.com' commit
  commit-z = -c user.name='My Z Name' -c user.email='mr_z@email.com' commit

Alias will be applied globally. Test it.

git commit -m "Custom message with committer and author My Name <default@email.com>"
git commit-x -m "Custom message with committer and author My X Name <mr_x@email.com>"
git commit-y -m "Custom message with committer and author My Y Name <mr_y@email.com>"
git commit-z -m "Custom message with committer and author My Z Name <mr_z@email.com>"
Related