442

I'm currently working on 2 projects, which expect that I configure my local username and email with different data when I push to them. For that I'm updating my config all the time like:

git config --local user.email "[email protected]"

Since they are different repositories, is there a way I could define an local email for each repository?

Maybe in the .gitconfig?

2
  • 2
    repo = all branches, all versions, etc. So when you say "since there are different branches...can I define email for each repo?" this doesn't make sense. What do you want: different email settings for each repo? or different email for each branch? Commented Feb 10, 2017 at 19:41
  • 1
    For others coming here, check out the answer to this other question that is similar, and the answer is based on folder structure + a separate .gitconfig for all repos under that directory Commented Sep 27, 2023 at 22:20

6 Answers 6

843

For just one repo, go into to the relevant repo DIR and:

git config user.name "Your Name Here"
git config user.email [email protected]

For (global) default email (which is configured in your ~/.gitconfig):

git config --global user.name "Your Name Here"
git config --global user.email [email protected]

You can check your Git settings with: git config user.name && git config user.email

If you are in a specific repo which you setup a new user/config for (different to global) then it should show that local config, otherwise it will show your global config.

Sign up to request clarification or add additional context in comments.

6 Comments

Just in case it helps anyone else you can check what your current settings are by omitting the last part e.g. git config user.email
This doesn’t work for me: if I set a different local name/email for a repository, then the push to origin still takes the credentials from the global settings — which is exactly what I wanted to avoid.
@ChrisOwens I edited the answer to add your really useful comment. Cheers.
@Jens what helped for me was running git commit --amend --reset-author --no-edit this reset the current commit to use the local config (after running the above commands).
what if you haven't cloned yet and cannot clone because the user is wrong in global but it's right for your other origins. seems like there should be a way to configure it based on host name for origin
|
45

...or just edit the .git/config file and add these three lines somewhere:

[user]
    name = YourName
    email = [email protected]

4 Comments

is this in each/per repo ?
@Pure.Krome yes and no. You can do both. Add this to each repo or only once to the .gitconfig under your user profile folder (if you're on Windows; I don't know the location for Linux).
That file will be edited implicitly by git config user.name "Your Name Here" etc.
Sure, but also think it's helpful to be aware of where these settings are actually saved so that you can make a backup or copy them to another machine, another repo, etc.
29

You can confirm that by printing on the terminal:

  1. Global user:git config --global user.name
  2. Local user: git config user.name

Comments

15

It can be achieved with below commands,

git config --local credential.helper ""
git push origin master

It asks for username and password for current repo.

1 Comment

After add git config user.email <myemail> then above command. Confirm do the trick!
13

I generally tend to keep diff name/email for my company project and personal project(on github)

Run below command in the git repo where you need to specify the user/email

git config user.name <user-name>
git config user.email <user-email>

Comments

2

One trick that has been reliably working for me is to set both a global config credential.username option as well as a local one. They will ask for a password for authentication. This works even for Git Credential Manager say on a Mac. For more information, please see: https://docs.github.com/en/get-started/getting-started-with-git/caching-your-github-credentials-in-git. So you can cache like at least two different passwords for two different GitHub accounts.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.