Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to configure user.name and user.email per wildcard domains in .gitconfig?

Tags:

git

github

I have a work computer, and it's configured globally to use my work email and name when committing. This is good. However, I'd like to make some sort of rule that says, "if the repo origin is github, use user X and email Y"

I realize you can make a config entry per repository, but I'd like it to be more automatic: if the clone is github, it should use github user details. If I clone from work, it should use work details.

Is there any way to configure this globally based on the remote domain? Or another way?

EDIT/UPDATE

I have accepted the answer below, but modified the script just a bit:

#!/usr/bin/env bash  # "Real" git is the second one returned by 'which' REAL_GIT=$(which -a git | sed -n 2p)  # Does the remote "origin" point to GitHub? if ("$REAL_GIT" remote -v 2>/dev/null | grep '^origin\b.*github.com.*(push)$' >/dev/null 2>&1); then      # Yes.  Set username and email that you use on GitHub.     export GIT_AUTHOR_NAME=$("$REAL_GIT" config --global user.ghname)     export GIT_AUTHOR_EMAIL=$("$REAL_GIT" config --global user.ghemail)  fi  "$REAL_GIT" "$@" 

The primary addition is the requirement for two git config values.

git config --global user.ghname "Your Name" git config --global user.ghemail "[email protected]" 

This avoids hard coding the values in the script, allowing it to be more portable. Maybe?

like image 761
Andrew Avatar asked Dec 06 '12 19:12

Andrew


People also ask

Can I specify multiple users for myself in Gitconfig?

With conditional includes in Git 2.13, it is now possible to have multiple user/email coexist on one machine with little work. user. gitconfig has my personal name and email. work-user.

Where can we configure username in git in Jenkins?

At the Jenkins web page, go to Manage Jenkins-> Configure System, find the git settings. You should be able to fill in "Global Config user.name Value" and "Global Config user. email Value" there. Thanks!

What is the command to set the user email for the current repository?

Open your terminal and navigate to your git repository. Change Git user name by running: git config --global user.name “Your Name” Change Git user email by running: git config --global user. email “[email protected]


1 Answers

Git 2.13 adds support for conditional config includes. If you organize your checkouts into directories for each domain of work, then you can add custom settings based on where the checkouts are. In your global git config:

[includeIf "gitdir:code/work/"]     path = /Users/self/code/work/.gitconfig 

And then in ~/code/work/.gitconfig:

[user]     email = [email protected] 

And of course you can do that for as many domains of work as you like.

like image 79
Jason R. Coombs Avatar answered Sep 19 '22 21:09

Jason R. Coombs