17

I work with my own projects and every-now-and-then I do some work for third-parties where I need to use their contact-information etc in commits. I usually use just global commit names etc but now I need to specify commit names and emails according to the directory. How can I do that in Git?

Perhaps useful relating information

  1. What is the difference between author and committer in Git?

  2. Git commit with no email

Community
  • 1
  • 1
hhh
  • 50,788
  • 62
  • 179
  • 282
  • Possible duplicate of [Can I specify multiple users for myself in .gitconfig?](https://stackoverflow.com/questions/4220416/can-i-specify-multiple-users-for-myself-in-gitconfig) – Don Branson Feb 20 '18 at 13:25

3 Answers3

32

You can have a per-repository username and email address that supercedes your global settings. It will affect commits made in the current repository, but not in other repositories.

git config user.name "Foo Bar"
git config user.email "foo.bar@example.com"

You can confirm that your local information is overriding your global defaults with:

git config -l | fgrep user.

It may show multiple entries (e.g both a global and a local configuration setting), but whatever is shown last in the list takes precedence.

Todd A. Jacobs
  • 81,402
  • 15
  • 141
  • 199
3

Use the following command to commit your changes

git commit -m "Commit message" --author "Your Name <email>"

Then you will be able to set the author, email explicitly for that commit.

  • 1
    ...no way to make this more implicit/automatic perhaps in some file? Perhaps something centralized like gitolite but just for managing commit credentials-and-contacts (not sure whether possible, well hence the q)? – hhh Jun 17 '12 at 05:08
1

If you need to specify different author/committer name/email according to folders. You can add a line like this in your .bashrc

PROMPT_COMMAND='[[ $PWD =~ ^path/to/dir.*$ ]] && { export GIT_COMMITTER_EMAIL=email && export GIT_COMMITTER_NAME=name && export GIT_AUTHOR_EMAIL=email && export GIT_AUTHOR_NAME=name; } || { unset GIT_COMMITTER_NAME && unset GIT_COMMITTER_EMAIL && unset GIT_AUTHOR_NAME && unset GIT_AUTHOR_EMAIL; }'
siddhant
  • 837
  • 9
  • 13