Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tell git to use the correct identity (name and email) for a given project?

I use my personal laptop for both work and personal projects and I would like to use my work email address for my commits at work (gitolite) and my personal email address for the rest (github).

I read about the following solutions which are all either global or temporary:

One solution is to run manually a shell function that sets my environment to work or personal, but I am pretty sure that I will often forget to switch to the correct identity resulting in committing under the wrong identity.

Is there a way of binding a certain repository, project name, etc. to an identity (name, email)? What do people do?

like image 382
Martin Jambon Avatar asked May 24 '11 20:05

Martin Jambon


People also ask

Where you will configure the user setting like name and email in git?

The Git username and email address can be set with the git config command. The values are associated with your commits.

How does git know which account to use?

Each commit in git is tagged with an email address. This can be any email address you'd like, as far as GitHub is concerned. Add that to the . git/config file for each repository you're working on and git will attribute your commits based on the per-repository email.

What should my git user.name be?

You can use your proper name, username or whatever identifier you'd like for user.name. It will be logged in each commit along with your email address, commit message and various other details. And you're exactly right about how GitHub uses the email address. Good luck with git!


7 Answers

Edit the config file with in ".git" folder to maintain the different username and email depends upon the repository

  • Go to Your repository
  • Show the hidden files and go to ".git" folder
  • Find the "config" file
  • Add the below lines at EOF

[user]

name = Bob

email = [email protected]

This below command show you which username and email set for this repository.

git config --get user.name

git config --get user.email

Example: for mine that config file in D:\workspace\eclipse\ipchat\.git\config

Here ipchat is my repo name

like image 30
arulraj.net Avatar answered Sep 29 '22 01:09

arulraj.net


If you use git config user.email "[email protected]" it will be bound to the current project you are in.

That is what I do for my projects. I set the appropriate identity when I clone/init the repo. It is not fool-proof (if you forget and push before you figure it out you are hosed) but it is about as good as you can get without the ability to say git config --global user.email 'ILLEGAL_VALUE'

Actually, you can make an illegal value. Set your git config --global user.name $(perl -e 'print "x"x968;')

Then if you forget to set your non-global values you will get an error message.

[EDIT] On a different system I had to increase the number of x to 968 to get it to fail with "fatal: Impossibly long personal identifier". Same version of git. Strange.

like image 43
Seth Robertson Avatar answered Sep 29 '22 00:09

Seth Robertson


git config user.email "[email protected]"

Doing that one inside a repo will set the configuration on THAT repo, and not globally.

Seems like that's pretty much what you're after, unless I'm misreading you.

like image 102
Dan Ray Avatar answered Sep 28 '22 23:09

Dan Ray


You need to use the local set command below:

local set

git config user.email [email protected]
git config user.name 'Mahmoud Zalt'

local get

git config --get user.email
git config --get user.name

The local config file is in the project directory: .git/config.

global set

git config --global user.email [email protected]
git config --global user.name 'Mahmoud Zalt'

global get

git config --global --get user.email
git config --global --get user.name

The global config file in in your home directory: ~/.gitconfig.

Remember to quote blanks, etc, for example: 'FirstName LastName'

like image 34
Mahmoud Zalt Avatar answered Sep 28 '22 23:09

Mahmoud Zalt


As of Git 2.13 you can use an includeIf in your gitconfig to include a file with a different configuration based on the path of the repository where you are running your git commands.

Since a new enough Git comes with Ubuntu 18.04 I've been using this in my ~/.gitconfig quite happily.

[include]
  path = ~/.gitconfig.alias # I like to keep global aliases separate
  path = ~/.gitconfig.defaultusername # can maybe leave values unset/empty to get warned if a below path didn't match
# If using multiple identities can use per path user/email
# The trailing / is VERY important, git won't apply the config to subdirectories without it
[includeIf "gitdir:~/projects/azure/"]
  path = ~/.gitconfig.azure # user.name and user.email for Azure
[includeIf "gitdir:~/projects/gitlab/"]
  path = ~/.gitconfig.gitlab # user.name and user.email for GitLab
[includeIf "gitdir:~/projects/foss/"]
  path = ~/.gitconfig.github # user.name and user.email for GitHub

https://motowilliams.com/conditional-includes-for-git-config#disqus_thread

To use Git 2.13 you will either need to add a PPA (Ubuntu older than 18.04/Debian) or download the binaries and install (Windows/other Linux).

like image 21
dragon788 Avatar answered Sep 29 '22 00:09

dragon788


If you don't use the --global parameter it will set the variables for the current project only.

like image 32
ismail Avatar answered Sep 29 '22 00:09

ismail


I very like the way of Micah Henning in his article (see Setting Up Git Identities) on this subject. The fact that he apply and force the identity to each repository created/cloned is a good way not to forget to set this up each time.

Basic git configuration

Unset current user config in git:

$ git config --global --unset user.name
$ git config --global --unset user.email
$ git config --global --unset user.signingkey

Force identity configuration on each new local repository:

$ git config --global user.useConfigOnly true

Create Git alias for identity command, we will use later:

$ git config --global alias.identity '! git config user.name "$(git config user.$1.name)"; git config user.email "$(git config user.$1.email)"; git config user.signingkey "$(git config user.$1.signingkey)"; :'

Identities creation

Create an identity with GPG (use gpg or gpg2 depending on what you got on your system). Repeat next steps for each identities you want to use.

Note: [keyid] here is the identifier of created secret key. Example here:

sec   rsa4096/8A5C011E4CE081A5 2020-06-09 [SC] [expires: 2021-06-09]
      CCC470AE787C057557F421488C4C951E4CE081A5
uid                 [ultimate] Your Name <youremail@domain>
ssb   rsa4096/1EA965889861C1C0 2020-06-09 [E] [expires: 2021-06-09]

The 8A5C011E4CE081A5 part after sec rsa4096/ is the identifier of key.

$ gpg --full-gen-key
$ gpg --list-secret-keys --keyid-format LONG <youremail@domain>
$ gpg --armor --export [keyid]

Copy the public key block and add it to your GitHub/GitProviderOfChoice settings as a GPG key.

Add identity to Git config. Also repeat this for each identity you want to add:

Note: here I use gitlab to name my identity, but from your question it can be anything, ex: gitolite or github, work, etc.

$ git config --global user.gitlab.name "Your Name"
$ git config --global user.gitlab.email "youremail@domain"
$ git config --global user.gitlab.signingkey [keyid]

Setup identity for a repository

If a new repo has no identity associated, an error will appear on commit, reminding you to set it.

*** Please tell me who you are.

## parts of message skipped ##

fatal: no email was given and auto-detection is disabled

Specify the identity you want on a new repository:

$ git identity gitlab

You're now ready to commit with the gitlab identity.

like image 43
sgy Avatar answered Sep 29 '22 00:09

sgy