Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change name, email and/or date for one specific commit, *before* committing

Tags:

git

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?

like image 394
Kevin Avatar asked Dec 03 '15 18:12

Kevin


2 Answers

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

git commit -m "A commit with a different author" --author="Your name here <[email protected]>"

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 <[email protected]>" --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=...
like image 163
Kevin Avatar answered Oct 04 '22 13:10

Kevin


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"
like image 30
Whymarrh Avatar answered Oct 04 '22 13:10

Whymarrh