149

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.

Mark Amery
  • 143,130
  • 81
  • 406
  • 459
Carl
  • 43,122
  • 10
  • 80
  • 104
  • 1
    Have you tried using fast-import? – alternative Sep 12 '10 at 23:03
  • I agree, [fast-import](http://www.kernel.org/pub/software/scm/git/docs/git-fast-import.html) is likely to be the best way to go. You could use *git commit* (or more properly the underlying plumbing: *hash-object*, *update-index*, *write-tree*, *commit-tree*, *update-ref*, etc.), but *fast-import* would be, well, faster. – Chris Johnsen Sep 12 '10 at 23:39
  • I did think of fast-import, but as I'm quite the git beginner myself, I decided to use the basic commands. To be perfectly honest I do not fully understand how fast-import works and am therefore a little hesitant to use it until I understand it better. Yes, it will be a lot slower using commit, but at least I will know what to expect - especially while debugging. – Carl Sep 12 '10 at 23:47
  • 1
    git commit --author="Name " -a -m "commit msg" – Masih Nov 23 '16 at 06:04

3 Answers3

179

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.

ahong
  • 1,041
  • 2
  • 10
  • 22
Tim Henigan
  • 60,452
  • 11
  • 85
  • 78
  • 20
    Also, the `--date` option to override the date. – Chris Johnsen Sep 12 '10 at 23:21
  • Can you give a specific example, everything I try – studgeek Jun 17 '11 at 17:13
  • @Tim Henigan: It looks like documentation is now hosted on [Github](http://schacon.github.com/git/git-commit.html) so the *man page* link you posted is dead. Can you confirm the new page is the same thing (in case there are other answers that need links updated)? – Roman Feb 06 '12 at 19:11
  • @R0MANARMY: I updated the URL. The GitHub pages are correct. I updated my link since the man pages are no longer hosted on kernel.org. Thanks for letting me know about the change. – Tim Henigan Feb 06 '12 at 19:37
  • 1
    Here is what I ended up using: `git commit -a --author="$user_details" --date="submit_date $submit_time" --file=/tmp/commit_msg` – Carl Apr 03 '12 at 20:50
148

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.

friederbluemle
  • 33,549
  • 14
  • 108
  • 109
27

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>"
BeardOverflow
  • 938
  • 12
  • 15
  • 1
    I get an error: "fatal: Option -m cannot be combined with -c/-C/-F." On removing "-m", getting another error: "fatal: could not lookup commit user.email=" – Arnab Biswas Dec 04 '20 at 07:13
  • 1
    @ArnabBiswas Thanks for your feedback. I have just edited my answer because I had typed the first example wrongly: commit must go after of config options. – BeardOverflow Dec 04 '20 at 12:39