7

I would like to create a commit in a Git repository, but have the name and email associated with the author in the commit be different from the information usually associated with me.

I'd also like to have the timestamp on the commit be something different than my current local time.

I know that I can rewrite my project's history after committing to change this information. But I haven't made the commit yet. Is there some way to change this information at the time I make the commit?

Kevin
  • 14,655
  • 24
  • 74
  • 124
  • 1
    I already know the answer to my question, but had a hard time finding the answer on StackOverflow. [So I'm posting the question and answer I would have found helpful to help future programmers.](http://blog.stackoverflow.com/2011/07/its-ok-to-ask-and-answer-your-own-questions/) – Kevin Dec 03 '15 at 18:09

2 Answers2

7

To change the author of the commit, use git commit --author:

git commit -m "A commit with a different author" --author="Your name here <yourEmailHere@includeTheAngleBrackets.com>"

To change the date of the commit, use git commit --date="YYYY-MM-DD HH:MMxm:

git commit -m "A commit made to celebrate Christmas" --date="2015-12-25 12:00am"

These options can be combined:

git commit -m "Ho ho ho" --author="Santa Claus <santa@northpole.org>" --date="2015-12-25 12:00am"

The git merge command doesn't have --author and --date options. To change the date and author of a merge command, first make the merge regularly:

git merge other_branch

Then, as long as a merge commit was created, you can use git commit --amend before pushing to change the merge commit's metadata:

git commit --amend --author=... --date=...
Kevin
  • 14,655
  • 24
  • 74
  • 124
  • For more detailed information about the format of the `--date` option, including how to set timezones, see this question: https://stackoverflow.com/questions/19742345/what-is-the-format-for-date-parameter-of-git-commit – Kevin Dec 03 '15 at 18:24
  • By default, when you use the `--date` option, Git will use your local timezone for the date. – Kevin Dec 03 '15 at 18:25
1

You can also set the appropriate environment variables during your commit. From the documentation:[1]

The final creation of a Git commit [...] uses these environment variables as its primary source of information, falling back to configuration values only if these aren't present.

  • GIT_AUTHOR_NAME is the human-readable name in the "author" field
  • GIT_AUTHOR_EMAIL is the email for the "author" field
  • GIT_AUTHOR_DATE is the timestamp used for the author field
  • GIT_COMMITTER_NAME sets the human name for the "committer" field
  • GIT_COMMITTER_EMAIL is the email address for the "committer" field
  • GIT_COMMITTER_DATE is used for the timestamp in the "committer" field

Example:

GIT_AUTHOR_NAME="foo" GIT_AUTHOR_EMAIL="bar" git commit -m "baz"
Whymarrh
  • 13,139
  • 14
  • 57
  • 108
  • I'm accepting Whymarrh's answer because it explains how to change the committer and author metadata, while mine only changes the author metadata. – Kevin Nov 01 '16 at 21:34