159

I have a different git repository for my office and a different git repo for my hobby projects.

When I do git config --global user.name the user name changes globally and this creates a confusion of committing to a repo with user name.

Hence the question is how can i have the same username across all my hobby projects and the same username across the office projects. I use the same machine to work on both the places.

vivekian2
  • 3,795
  • 9
  • 35
  • 41
whatf
  • 6,378
  • 14
  • 49
  • 78

2 Answers2

297

Just use --local instead of --global. In fact, local is the default so you can just do

git config user.email personal@example.org
git config user.name "whatf hobbyist"

in one repo, and

git config user.email work@example.com
git config user.name "whatf at work"

in another repo

The values will then be stored in in the .git/config for that repo rather than your global configuration file.

mikej
  • 65,295
  • 17
  • 152
  • 131
  • shouldn't user.name contain the person's _name_, not email address? `user.email` contains the email address – Jonathan Wakely Mar 07 '13 at 10:31
  • @JonathanWakely I think you're right. I'm not sure where I got the example to put both values in one setting. I've updated the answer. – mikej Mar 07 '13 at 13:55
  • 1
    @mikej `git commit --author=` allows that syntax, but not setting through config or environment variables. – Joe Mar 09 '13 at 04:48
  • Ah, cheers @Joe, that might be where I've used it in the past. – mikej Mar 09 '13 at 16:13
  • 2
    Can't we have something like .ssh/config Host *.workdomain email work@example.com Host *github.com personal@example.org ? – Sérgio Sep 29 '14 at 21:14
  • 1
    When I do this, it does not ask me for my password and does not allow me to push to the repository; it just assumes I'm using the global. This did not work for me. – Pro Q Feb 18 '18 at 21:00
  • Hi @ProQ - this question/answer is about controlling the name and email address recorded when you make a commit. If you're having trouble pushing to a remote that sounds like a different question. – mikej Feb 18 '18 at 21:31
  • 2
    Ah, yes. My issue is with pushing to a remote. Committing works just fine for me. Thank you. – Pro Q Feb 18 '18 at 21:33
  • 1
    @ProQ, if you're using SSH for auth, check out the instructions in [this gist](https://gist.github.com/jexchan/2351996). be sure to read the first couple of comments though, because the gist itself doesn't clearly explain how SSH hosts work.. –  Aug 18 '18 at 21:28
  • what can you do if the local repo has multiple submodules ? manually add the name in all submodules or is there an accelerator ? – nass Oct 21 '21 at 11:30
14

Omit the --global from your call to git config:

git config user.name "A. U. Thor"

This will set the property in the current repository.

Bombe
  • 81,643
  • 20
  • 123
  • 127