Can I specify multiple users for myself in .gitconfig?
up vote
538
down vote
favorite
In my ~/.gitconfig
, I list my personal email address under [user]
, since that's what I want to use for Github repos.
But, I've recently started using git for work, too. My company's git repo allows me to commit, but when it sends out announcements of new changesets, it says they are from Anonymous because it doesn't recognize the email address in my .gitconfig
- at least, that's my theory.
Is it possible to specify multiple [user]
definitions in .gitconfig
? Or is there some other way to override the default .gitconfig
for a certain directory? In my case, I check out all work code in ~/worksrc/
- is there a way to specify a .gitconfig
for only that directory (and its subdirectories)?
git
|
show 2 more comments
up vote
538
down vote
favorite
In my ~/.gitconfig
, I list my personal email address under [user]
, since that's what I want to use for Github repos.
But, I've recently started using git for work, too. My company's git repo allows me to commit, but when it sends out announcements of new changesets, it says they are from Anonymous because it doesn't recognize the email address in my .gitconfig
- at least, that's my theory.
Is it possible to specify multiple [user]
definitions in .gitconfig
? Or is there some other way to override the default .gitconfig
for a certain directory? In my case, I check out all work code in ~/worksrc/
- is there a way to specify a .gitconfig
for only that directory (and its subdirectories)?
git
See git-config#FILES.
– Josh Lee
Nov 18 '10 at 22:48
I'm surprised that your company server would do that - it would have to CHANGE the sha of your commit for that to work. If you make a commit to a local checkout, what username do you see?
– Alex Brown
Nov 18 '10 at 22:59
@Alex: Presumably the notification hook is trying to match the author information (email, perhaps) with some other list/database, maybe in order to canonicalize it, maybe looking for some other information. Obviously not the best approach for this particular case, though!
– Cascabel
Nov 18 '10 at 23:48
1
@Alex: Forgot the important bit there - it could easily just be a name on the email notification, not actually changing anything on the commit.
– Cascabel
Nov 19 '10 at 0:49
1
You can use a git-hook to automate this recurring work: github.com/DrVanScott/git-clone-init
– Henning
Apr 22 '17 at 17:40
|
show 2 more comments
up vote
538
down vote
favorite
up vote
538
down vote
favorite
In my ~/.gitconfig
, I list my personal email address under [user]
, since that's what I want to use for Github repos.
But, I've recently started using git for work, too. My company's git repo allows me to commit, but when it sends out announcements of new changesets, it says they are from Anonymous because it doesn't recognize the email address in my .gitconfig
- at least, that's my theory.
Is it possible to specify multiple [user]
definitions in .gitconfig
? Or is there some other way to override the default .gitconfig
for a certain directory? In my case, I check out all work code in ~/worksrc/
- is there a way to specify a .gitconfig
for only that directory (and its subdirectories)?
git
In my ~/.gitconfig
, I list my personal email address under [user]
, since that's what I want to use for Github repos.
But, I've recently started using git for work, too. My company's git repo allows me to commit, but when it sends out announcements of new changesets, it says they are from Anonymous because it doesn't recognize the email address in my .gitconfig
- at least, that's my theory.
Is it possible to specify multiple [user]
definitions in .gitconfig
? Or is there some other way to override the default .gitconfig
for a certain directory? In my case, I check out all work code in ~/worksrc/
- is there a way to specify a .gitconfig
for only that directory (and its subdirectories)?
git
git
asked Nov 18 '10 at 22:45
Brock Boland
5,00092835
5,00092835
See git-config#FILES.
– Josh Lee
Nov 18 '10 at 22:48
I'm surprised that your company server would do that - it would have to CHANGE the sha of your commit for that to work. If you make a commit to a local checkout, what username do you see?
– Alex Brown
Nov 18 '10 at 22:59
@Alex: Presumably the notification hook is trying to match the author information (email, perhaps) with some other list/database, maybe in order to canonicalize it, maybe looking for some other information. Obviously not the best approach for this particular case, though!
– Cascabel
Nov 18 '10 at 23:48
1
@Alex: Forgot the important bit there - it could easily just be a name on the email notification, not actually changing anything on the commit.
– Cascabel
Nov 19 '10 at 0:49
1
You can use a git-hook to automate this recurring work: github.com/DrVanScott/git-clone-init
– Henning
Apr 22 '17 at 17:40
|
show 2 more comments
See git-config#FILES.
– Josh Lee
Nov 18 '10 at 22:48
I'm surprised that your company server would do that - it would have to CHANGE the sha of your commit for that to work. If you make a commit to a local checkout, what username do you see?
– Alex Brown
Nov 18 '10 at 22:59
@Alex: Presumably the notification hook is trying to match the author information (email, perhaps) with some other list/database, maybe in order to canonicalize it, maybe looking for some other information. Obviously not the best approach for this particular case, though!
– Cascabel
Nov 18 '10 at 23:48
1
@Alex: Forgot the important bit there - it could easily just be a name on the email notification, not actually changing anything on the commit.
– Cascabel
Nov 19 '10 at 0:49
1
You can use a git-hook to automate this recurring work: github.com/DrVanScott/git-clone-init
– Henning
Apr 22 '17 at 17:40
See git-config#FILES.
– Josh Lee
Nov 18 '10 at 22:48
See git-config#FILES.
– Josh Lee
Nov 18 '10 at 22:48
I'm surprised that your company server would do that - it would have to CHANGE the sha of your commit for that to work. If you make a commit to a local checkout, what username do you see?
– Alex Brown
Nov 18 '10 at 22:59
I'm surprised that your company server would do that - it would have to CHANGE the sha of your commit for that to work. If you make a commit to a local checkout, what username do you see?
– Alex Brown
Nov 18 '10 at 22:59
@Alex: Presumably the notification hook is trying to match the author information (email, perhaps) with some other list/database, maybe in order to canonicalize it, maybe looking for some other information. Obviously not the best approach for this particular case, though!
– Cascabel
Nov 18 '10 at 23:48
@Alex: Presumably the notification hook is trying to match the author information (email, perhaps) with some other list/database, maybe in order to canonicalize it, maybe looking for some other information. Obviously not the best approach for this particular case, though!
– Cascabel
Nov 18 '10 at 23:48
1
1
@Alex: Forgot the important bit there - it could easily just be a name on the email notification, not actually changing anything on the commit.
– Cascabel
Nov 19 '10 at 0:49
@Alex: Forgot the important bit there - it could easily just be a name on the email notification, not actually changing anything on the commit.
– Cascabel
Nov 19 '10 at 0:49
1
1
You can use a git-hook to automate this recurring work: github.com/DrVanScott/git-clone-init
– Henning
Apr 22 '17 at 17:40
You can use a git-hook to automate this recurring work: github.com/DrVanScott/git-clone-init
– Henning
Apr 22 '17 at 17:40
|
show 2 more comments
19 Answers
19
active
oldest
votes
up vote
773
down vote
accepted
You can configure an individual repo to use a specific user / email address which overrides the global configuration. From the root of the repo, run
git config user.name "Your Name Here"
git config user.email your@email.com
whereas the default user / email is configured in your ~/.gitconfig
git config --global user.name "Your Name Here"
git config --global user.email your@email.com
66
you can see the effects of these settings in the.git/config
file
– Abizern
Nov 18 '10 at 23:15
19
You can manually edit those config files withgit config --edit
andgit config --global --edit
. And in case you missed Abizern’s comment, a repository’s config file is at<repo-root>/.git/config
.
– Rory O'Kane
Apr 25 '12 at 20:01
12
You should also unset GIT_AUTHOR_EMAIL and GIT_COMMITTER_EMAIL (and *_NAME) as they will override the local settings
– ACyclic
Sep 14 '12 at 9:58
6
Is there a way to do this for all repos in a given folder, rather than for individual repos? See my question here: stackoverflow.com/questions/21307793/…
– scubbo
Jan 23 '14 at 12:13
4
See this answer below for more upto-date solution withGit 2.13
released today.
– tejasbubane
May 10 '17 at 11:20
|
show 6 more comments
up vote
207
down vote
Since git 2.13, it is possible to solve this using newly introduced Conditional includes.
An example:
Global config ~/.gitconfig
[user]
name = John Doe
email = john@doe.tld
[includeIf "gitdir:~/work/"]
path = ~/work/.gitconfig
Work specific config ~/work/.gitconfig
[user]
email = john.doe@company.tld
22
This is the best answer now that git 2.13 has been released.
– tejasbubane
May 10 '17 at 11:18
Would this solution work for all subdirectories within the one specified in the include statement, assuming they have no .gitconfig files of their own? I would think so, but so far testing hasn't borne this out.
– Gary
Jun 30 '17 at 19:28
@Gary Yes, according to my experience and the docs: “If the pattern ends with /, ** will be automatically added. For example, the pattern foo/ becomes foo/**. In other words, it matches "foo" and everything inside, recursively.”, “; include for all repositories inside $HOME/to/group [includeIf "gitdir:~/to/group/"]”
– Tomáš Janoušek
Jun 30 '17 at 21:38
This is exactly what I needed. I did have to go to 2.13.1 docs to find out more about theincludeIf
section: git-scm.com/docs/git-config/2.13.1#_conditional_includes
– gligoran
Aug 15 '17 at 9:51
@gligoran There seems to be something wrong with the website. It says there are no changes between 2.13.0 and 2.13.1 version of git-config manpage, yet the 2.13.0 one doesn't mention includeIf. :-(
– Tomáš Janoušek
Aug 15 '17 at 11:26
|
show 2 more comments
up vote
96
down vote
Or you can add following information in your local .git/config
file
[user]
name = Your Name
email = your.email@gmail.com
18
...which is the manual way to do what the commands recommended by @discomurray do for you.
– user456584
Jul 10 '14 at 21:15
2
I wish I could upvote this 9 more times
– Genia S.
Jul 30 '14 at 3:52
1
is it possible to add multiple lines of this per remote?
– Abel Callejo
Jul 14 '17 at 0:22
sometimes doing things manually is not that bad.
– xxks-kkk
Jun 13 at 14:54
add a comment |
up vote
33
down vote
After getting some inspiration from Orr Sella's blog post I wrote a pre-commit hook (resides in ~/.git/templates/hooks
) which would set specific usernames and e-mail addresses based on the information inside a local repositorie's ./.git/config
:
You have to place the path to the template directory into your ~/.gitconfig
:
[init]
templatedir = ~/.git/templates
Then each git init
or git clone
will pick up that hook and will apply the user data during the next git commit
. If you want to apply the hook to already exisiting repos then just run a git init
inside the repo in order to reinitialize it.
Here is the hook I came up with (it still needs some polishing - suggestions are welcome).
Save it either as
~/.git/templates/hooks/pre_commit
or
~/.git/templates/hooks/post-checkout
and make sure it is executable: chmod +x ./post-checkout || chmod +x ./pre_commit
#!/usr/bin/env bash
# -------- USER CONFIG
# Patterns to match a repo's "remote.origin.url" - beginning portion of the hostname
git_remotes[0]="Github"
git_remotes[1]="Gitlab"
# Adjust names and e-mail addresses
local_id_0[0]="my_name_0"
local_id_0[1]="my_email_0"
local_id_1[0]="my_name_1"
local_id_1[1]="my_email_1"
local_fallback_id[0]="${local_id_0[0]}"
local_fallback_id[1]="${local_id_0[1]}"
# -------- FUNCTIONS
setIdentity()
{
local current_id local_id
current_id[0]="$(git config --get --local user.name)"
current_id[1]="$(git config --get --local user.email)"
local_id=("$@")
if [[ "${current_id[0]}" == "${local_id[0]}" &&
"${current_id[1]}" == "${local_id[1]}" ]]; then
printf " Local identity is:n"
printf "» User: %sn» Mail: %snn" "${current_id[@]}"
else
printf "» User: %sn» Mail: %snn" "${local_id[@]}"
git config --local user.name "${local_id[0]}"
git config --local user.email "${local_id[1]}"
fi
return 0
}
# -------- IMPLEMENTATION
current_remote_url="$(git config --get --local remote.origin.url)"
if [[ "$current_remote_url" ]]; then
for service in "${git_remotes[@]}"; do
# Disable case sensitivity for regex matching
shopt -s nocasematch
if [[ "$current_remote_url" =~ $service ]]; then
case "$service" in
"${git_remotes[0]}" )
printf "n»» An Intermissionn» %s repository found." "${git_remotes[0]}"
setIdentity "${local_id_0[@]}"
exit 0
;;
"${git_remotes[1]}" )
printf "n»» An Intermissionn» %s repository found." "${git_remotes[1]}"
setIdentity "${local_id_1[@]}"
exit 0
;;
* )
printf "n» pre-commit hook: unknown errorn» Quitting.n"
exit 1
;;
esac
fi
done
else
printf "n»» An Intermissionn» No remote repository set. Using local fallback identity:n"
printf "» User: %sn» Mail: %snn" "${local_fallback_id[@]}"
# Get the user's attention for a second
sleep 1
git config --local user.name "${local_fallback_id[0]}"
git config --local user.email "${local_fallback_id[1]}"
fi
exit 0
EDIT:
So I rewrote the hook as a hook and command in Python. Additionally it's possible to call the script as a Git command (git passport
), too. Also it's possible to define an arbitrary number of IDs inside a configfile (~/.gitpassport
) which are selectable on a prompt. You can find the project at github.com: git-passport - A Git command and hook written in Python to manage multiple Git accounts / user identities.
1
This works neatly. However, it worked better for me by making this a post-checkout (instead of post-commit) hook. My suggestions to improve this answer are, mention that: 1. the snippet needs to be saved as ~/.git/templates/hooks/post-checkout and be given permissionchmod +x post-checkout
, 2.git_remotes
values are beginning portion of the entire hostname, e.g.git@github.com
, 3.local_id
values should be edited by user to respective names and email addresses.
– Shantanu Kumar
Dec 24 '14 at 16:37
@ShantanuKumar Thanks for your comment. I adjusted the answer as you proposed. Maybe I will rewrite the script in Python soon.
– Saucier
Dec 29 '14 at 16:41
Added support for regular expressions and for repositories without remotes. For easy download all merged here. Regexps to distinguish identities for different projects on the same service. And supporting remoteless repos with e.g. a default identity makes sense if yougit init
new projects from an IDE such aseclipse
(which cannot handle interactive pre-commit triggers)
– cfi
Oct 27 '15 at 12:57
add a comment |
up vote
23
down vote
One command github accounts switch
This solution takes the form of a single git alias. Once executed, the current project user will be attached to another account
Generate ssh keys
ssh-keygen -t rsa -C "rinquin.arnaud@gmail.com" -f '/Users/arnaudrinquin/.ssh/id_rsa'
[...]
ssh-keygen -t rsa -C "arnaud.rinquin@wopata.com" -f '/Users/arnaudrinquin/.ssh/id_rsa_pro'
Link them to your GitHub / Bitbucket accounts
- copy default public key
pbcopy < ~/.ssh/id_rsa.pub
- login to your GitHub acount
- paste the key in the
add SSH key
github page - copy other public key
pbcopy < ~/.ssh/id_rsa_pro.pub
- repeat and adapt steps 2 to 4 for every other account
Step 1. Automatic ssh key switching.
We can configure ssh
to send a use a specific encryption key depending on the host
. The nice thing is that you can have several aliases for the same hostname
.
See this example ~/.ssh/config
file:
# Default GitHub
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa
# Professional github alias
Host github_pro
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_pro
git remote configuration
You can now use these aliases in the git remotes by changing git@github.com
by git@github_pro
.
You can either change your existing projects remotes (using something like git remote origin set-url git@github_pro:foo/bar.git
) or adapt them directly when cloning them.
git clone git@github.com:ArnaudRinquin/atom-zentabs.git
using alias, it become:
git clone git@github_pro:ArnaudRinquin/atom-zentabs.git
Step 2. Changing git user.email
Git config settings can be global or per project. In our case, we want a per project settings. It is very easy to change it:
git config user.email 'arnaud.rinquin@wopata.com'
While this is easy, it takes way to long for the developers we are. We can write a very simple git alias for that.
We are going to add it to the ~/.gitconfig
file.
[user]
name = Arnaud Rinquin
email = rinquin.arnaud@gmail.com
...
[alias]
setpromail = "config user.email 'arnaud.rinquin@wopata.com'"
Then, all we have to do is git setpromail
to have our email changed for this project only.
Step 3. One command switch please?!
Wouldn’t it be nice to switch from default account to a specified one with a single parameter-less command? This is definitely possible. This command will have two steps:
- change current project remotes to the chosen aliases
- change current project user.email config
We already have a one command solution for the second step, but the first one is way harder.
One command remote host change
Here comes the solution in the form of another git alias command to add to your ~/.gitconfig
:
[alias]
changeremotehost = !sh -c "git remote -v | grep '$1.*fetch' | sed s/..fetch.// | sed s/$1/$2/ | xargs git remote set-url"
This allows changing all remotes from one host to another (the alias). See the example:
$ > git remote -v
origin git@github.com:ArnaudRinquin/arnaudrinquin.github.io.git (fetch)
origin git@github.com:ArnaudRinquin/arnaudrinquin.github.io.git (push)
$ > git changeremotehost github.com github_pro
$ > git remote -v
origin git@github_pro:ArnaudRinquin/arnaudrinquin.github.io.git (fetch)
origin git@github_pro:ArnaudRinquin/arnaudrinquin.github.io.git (push)
Combine them all
We now just have to combine the two commands into one, this is quite easy. See how I also integrate bitbucket host switching.
[alias]
changeremotehost = !sh -c "git remote -v | grep '$1.*fetch' | sed s/..fetch.// | sed s/$1/$2/ | xargs git remote set-url"
setpromail = "config user.email 'arnaud.rinquin@wopata.com'"
gopro = !sh -c "git changeremotehost github.com github_pro && git changeremotehost bitbucket.com bitbucket_pro && git setpromail"
Source Link -Tutorial
This is brilliant, thanks. I work with a bunch of repos per email so mysetpromail
alias does aconfig --global
instead (and I have other set aliases for setting different email addresses). It works!
– michel-slm
Jun 8 '16 at 17:08
1
For those 2 different accounts how can I sign with different gpg keys ? I have 2x gpg keys for 2x github accounts and would like to sign differently. "git config --global user.signingkey xxxx"
– hakkican
Apr 6 at 8:09
This answer is awesome. I nearly didn't read it because I thought I found what from the other answer. Definitely deserves more upvotes. PS. It is even better when combined withuseConfigOnly = true
from the other answer.
– Steiny
Jun 14 at 21:13
add a comment |
up vote
23
down vote
If you do not want to have a default email address (email address links to a github user), you can configure that you want to be asked. How you can do that depends on the version of git you use, see below.
The (intended) drawback is that you have to configure your email address (and your name) once for every repository. So, you cannot forget to do it.
Version < 2.7.0
[user]
name = Your name
email = "(none)"
in your global configuration ~/.gitconfig
as stated in a comment by Dan Aloni in Orr Sella's blog post. When trying to do the first commit in a repository, git fails with the nice message:
*** Please tell me who you are.
Run
git config --global user.email "you@example.com"
git config --global user.name "Your Name"
to set your account's default identity.
Omit --global to set the identity only in this repository.
fatal: unable to auto-detect email address (got '(none)')
The name is taken from the global config when the email address is set locally (the message is not perfectly accurate).
2.7.0 ≤ Version < 2.8.0
The behaviour in versions < 2.7.0 was not intended and fixed with 2.7.0. You can still use a pre-commit hook as described in Orr Sella's blog post. This solution works also for other versions, but the other solutions not for this version.
Version ≥ 2.8.0
Dan Aloni added an option to achieve that behaviour (see release notes). Use it with:
[user]
useConfigOnly = true
To make it work you may not give a name or email address in the global config. Then, at the first commit, you get an error message
fatal: user.useConfigOnly set but no name given
So the message is not very instructive, but since you set the option explicitly, you should know what to do. In contrast to the solution of versions < 2.7.0, you always have to set both name and email manually.
I use this method for a few months, it worked great. I have multiple email addresses (personal, work) and I do not want to have a "default" one configured in global gitconfig. With special value "(none)" git will prompt me to provide a valid address every time I spin up a new repo, instead of guessing one based on username and hostname (which is annoying and I need to --amend it). However, recently with upgraded version of Git (2.7.0, maybe earlier), I find out that special value "(none)" does not trigger fatal errors any more. Instead it will just use "John Doe <(none)>" as is ...
– Zhuoyun Wei
Feb 2 '16 at 13:11
@wzyboy: Oh, you are right. I usedgit bisect
to find that commit 19ce497c... introduced this behaviour. However, independent of the version (2.5 - 2.7) I can useemail =
(with no argument) in the config and it shows the same behaviour asemail = "(none)"
in old versions. Can you confirm this? If so, I'll edit my answer. I am just sceptical as it seems so obvious and I did not use it before.
– John
Feb 3 '16 at 17:17
I triedemail =
in 2.7.0, Git still guesses email address based on username and hostname. I now usepre-commit
approach in Sella's blog. I also notified the Dan Aloni who came up with the"(none)"
idea in Sella's post and he filed up a patch to formally implement this as a feature: permalink.gmane.org/gmane.comp.version-control.git/285301
– Zhuoyun Wei
Feb 4 '16 at 3:25
3
since git-2.8: The "user.useConfigOnly" configuration variable can be used to force the user to always set user.email & user.name github.com/git/git/blob/master/Documentation/RelNotes/2.8.0.txt
– rofrol
Mar 29 '16 at 13:33
@rofrol Thanks for the info. I updated my answer accordingly.
– John
Mar 29 '16 at 16:12
add a comment |
up vote
16
down vote
Another option to get git
to work with multiple names / emails is by aliasing git
and using the -c
flag to override the global and repository-specific config.
For example, by defining an alias:
alias git='/usr/bin/git -c user.name="Your name" -c user.email="name@example.com"'
To see whether it works, simply type git config user.email
:
$ git config user.email
name@example.com
Instead of an alias, you could also put a custom git
executable within your $PATH
.
#!/bin/sh
/usr/bin/git -c user.name="Your name" -c user.email="name@example.com" "$@"
An advantage of these method over a repository-specific .git/config
is that it applies to every git
repository when the custom git
program is active. In this way, you can easily switch between users/names without modifying any (shared) configuration.
add a comment |
up vote
8
down vote
There is a simple solution that seems to work well for avoiding mistakes.
Simply remove the [user]
section from your ~/.gitconfig
, which will prevent you from making any commits without setting user.name
for each repository.
In your ~/.bashrc
, add some simple aliases for the user and email:
alias ggmail='git config user.name "My Name";git config user.email me@gmail.com'
alias gwork='git config user.name "My Name";git config user.email me@work.job'
add a comment |
up vote
8
down vote
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.gitconfig
has my work name and email. Both files are at ~
path.
So my personal name/email applies by default. For c:/work/
dir, my work name/email is applied. For c:/work/github/
dir, my personal name/email is applied. This works as the last setting gets applied.
# ~/.gitconfig
[include]
path = user.gitconfig
[includeIf "gitdir/i:c:/work/"]
path = work-user.gitconfig
[includeIf "gitdir/i:c:/work/github/"]
path = user.gitconfig
gitdir
is case-sensitive and gitdir/i
is case-insensitive.
"gitdir/i:github/"
would apply the conditional include for any directory with github
in its path.
While your answer is appreciated and good Tomáš Janoušek gave the same 20 days before you. Please consider deleting this answer.
– Hedge
Sep 15 '17 at 17:03
3
@Hedge Yes, I upvoted his answer, but it took me a while to configure on windows and that's wheregitdir/i
helped me out (which his answer does not mention).
– hIpPy
Sep 15 '17 at 21:44
add a comment |
up vote
7
down vote
git aliases (and sections in git configs) to the rescue!
add an alias (from command line):
git config --global alias.identity '! git config user.name $(git config user.$1.name); git config user.email $(git config user.$1.email); :'
then, set, for example
git config --global user.github.name "your github username"
git config --global user.github.email your@github.email
and in a new or cloned repo you can run this command:
git identity github
This solution isn't automatic, but unsetting user and email in your global ~/.gitconfig would force git to remind you to set them manually in each new or cloned repo.
git config --global --unset user.name
git config --global --unset user.email
I like your solution; however, when I unset my global it just makes the commit with my computer's host name rather than having git remind me to set them in any given repo :(
– ENG618
Jun 20 '17 at 14:59
add a comment |
up vote
6
down vote
This answer is partially inspired by the post by @Saucier, but I was looking for an automated way to set user.name
and user.email
on a per repo basis, based on the remote, that was a little more light weight than the git-passport package that he developed. Also h/t to @John for the useConfigOnly setting. Here is my solution:
.gitconfig
changes:
[github]
name = <github username>
email = <github email>
[gitlab]
name = <gitlab username>
email = <gitlab email>
[init]
templatedir = ~/.git-templates
[user]
useConfigOnly = true
post-checkout hook which should be saved to the following path: ~/.git-templates/hooks/post-checkout
:
#!/usr/bin/env bash
# make regex matching below case insensitive
shopt -s nocasematch
# values in the services array should have a corresponding section in
# .gitconfig where the 'name' and 'email' for that service are specified
remote_url="$( git config --get --local remote.origin.url )"
services=(
'github'
'gitlab'
)
set_local_user_config() {
local service="${1}"
local config="${2}"
local service_config="$( git config --get ${service}.${config} )"
local local_config="$( git config --get --local user.${config} )"
if [[ "${local_config}" != "${service_config}" ]]; then
git config --local "user.${config}" "${service_config}"
echo "repo 'user.${config}' has been set to '${service_config}'"
fi
}
# if remote_url doesn't contain the any of the values in the services
# array the user name and email will remain unset and the
# user.useConfigOnly = true setting in .gitconfig will prompt for those
# credentials and prevent commits until they are defined
for s in "${services[@]}"; do
if [[ "${remote_url}" =~ "${s}" ]]; then
set_local_user_config "${s}" 'name'
set_local_user_config "${s}" 'email'
break
fi
done
I use different credentials for github and gitlab, but those references in the code above could be replaced or augmented with any service that you use. In order to have the post-checkout hook automatically set the user name and email locally for a repo after a checkout make sure the service name appears in the remote url, add it to the services array in the post-checkout
script and create a section for it in your .gitconfig
that contains your user name and email for that service.
If none of the service names appear in the remote url or the repo doesn't have a remote the user name and email will not be set locally. In these cases the user.useConfigOnly
setting will be in play which will not allow you to make commits until the user name and email are set at the repo level, and will prompt the user to configure that information.
1
Great solution. Do not forget tochmod 755
the hook script. Otherwise, it will be copied but never executed.
– onekiloparsec
Apr 11 '17 at 15:41
add a comment |
up vote
4
down vote
GIT_AUTHOR_EMAIL
+ local .bashrc
.bashrc_local
: don't track this file, put it only on your work computer:
export GIT_AUTHOR_EMAIL='me@work.com'
export GIT_COMMITTER_EMAIL="$GIT_AUTHOR_EMAIL"
.bashrc
: track this file, make it the same on both work and home computers:
F="$HOME/.bashrc_local"
if [ -r "$F" ]; then
. "$F"
fi
I'm using https://github.com/technicalpickles/homesick to sync my dotfiles.
If only gitconfig would accept environment variables: Shell variable expansion in git config
add a comment |
up vote
3
down vote
Windows Environment
Additional this can be modified from Git Extensions --> Settings --> Global Settings
, if you have it installed in your systems.
gitextensions-latest-release
Right Click on a folder/directory in Windows Environment to access these settings.
Update : How to switch/maintain multiple settings in Version 2.49
How does this address multiple users, instead of just editing them?
– D_N
Feb 14 at 21:44
@D_N Updated with new screenshot with options to switch settings.
– Abhijeet
Feb 15 at 10:15
Open local repo folder and select git-config from navigation menu then click the local repo tab, the contents will be applied to $local_repo_path/.git/config as above answer.
– maxwu
May 12 at 12:37
add a comment |
up vote
2
down vote
May be it is simple hack, but it is useful. Just generate 2 ssh keys like below.
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/GowthamSai/.ssh/id_rsa): work
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in damsn.
Your public key has been saved in damsn.pub.
The key fingerprint is:
SHA256:CrsKDJWVVek5GTCqmq8/8RnwvAo1G6UOmQFbzddcoAY GowthamSai@Gowtham-MacBook-Air.local
The key's randomart image is:
+---[RSA 4096]----+
|. .oEo+=o+. |
|.o o+o.o= |
|o o o.o. + |
| =.+ . = |
|= *+. S. |
|o*.++o . |
|=.oo.+. |
| +. +. |
|.o=+. |
+----[SHA256]-----+
Same way create one more for personal. So, you have 2 ssh keys, work and company. Copy work.pub, work, personal.pub, personal to ~/.ssh/ Directory.
Then create shell script with the following lines and name it as crev.sh (Company Reverse) with following content.
cp ~/.ssh/work ~/.ssh/id_rsa
cp ~/.ssh/work.pub ~/.ssh/id_rsa.pub
Same way, create one more called prev.sh (Personal Reverse) with the following content.
cp ~/.ssh/personal ~/.ssh/id_rsa
cp ~/.ssh/personal.pub ~/.ssh/id_rsa.pub
in ~/.bashrc add aliases for those scripts like below
alias crev="sh ~/.ssh/crev.sh"
alias prev="sh ~/.ssh/prev.sh"
source ~/.bashrc
When ever you wanna use company, just do crev, and if you wanna use personal do prev :-p.
Add those ssh keys to your github accounts. Make sure, you don't have id_rsa generated perviously, because those scripts will overwrite id_rsa. If you have already generated id_rsa, use that for one of the account. Copy them as personal and skip generation of personal keys.
add a comment |
up vote
1
down vote
I made a bash function that handle that. Here is the Github repo.
For record:
# Look for closest .gitconfig file in parent directories
# This file will be used as main .gitconfig file.
function __recursive_gitconfig_git {
gitconfig_file=$(__recursive_gitconfig_closest)
if [ "$gitconfig_file" != '' ]; then
home="$(dirname $gitconfig_file)/"
HOME=$home /usr/bin/git "$@"
else
/usr/bin/git "$@"
fi
}
# Look for closest .gitconfig file in parents directories
function __recursive_gitconfig_closest {
slashes=${PWD//[^/]/}
directory="$PWD"
for (( n=${#slashes}; n>0; --n ))
do
test -e "$directory/.gitconfig" && echo "$directory/.gitconfig" && return
directory="$directory/.."
done
}
alias git='__recursive_gitconfig_git'
add a comment |
up vote
0
down vote
Something like Rob W's answer, but allowing different a different ssh key, and works with older git versions (which don't have e.g. a core.sshCommand config).
I created the file ~/bin/git_poweruser
, with executable permission, and in the PATH:
#!/bin/bash
TMPDIR=$(mktemp -d)
trap 'rm -rf "$TMPDIR"' EXIT
cat > $TMPDIR/ssh << 'EOF'
#!/bin/bash
ssh -i $HOME/.ssh/poweruserprivatekey $@
EOF
chmod +x $TMPDIR/ssh
export GIT_SSH=$TMPDIR/ssh
git -c user.name="Power User name" -c user.email="power@user.email" $@
Whenever I want to commit or push something as "Power User", I use git_poweruser
instead of git
. It should work on any directory, and does not require changes in .gitconfig
or .ssh/config
, at least not in mine.
add a comment |
up vote
0
down vote
Here is what I just found after following the steps in many answers here
How to set up Multiple SSH Keys settings for different github account
You might want to start checking your currently saved keys
$ ssh-add -l
If you decide to delete all cached keys before (optional, carefull about this)
$ ssh-add -D
Then you can create a ssh pub/priv key linked to each email/account that you wish/need to use
$ cd ~/.ssh
$ ssh-keygen -t rsa -C "work@company.com" <-- save it as "id_rsa_work"
$ ssh-keygen -t rsa -C "pers@email.com" <-- save it as "id_rsa_pers"
After performing this commands you will have the following files created
~/.ssh/id_rsa_work
~/.ssh/id_rsa_work.pub
~/.ssh/id_rsa_pers
~/.ssh/id_rsa_pers.pub
Make sure authentication agent is running
$ eval `ssh-agent -s`
Add the generated keys as following (from the ~/.ssh folder)
$ ssh-add id_rsa_work
$ ssh-add id_rsa_pers
Now you can check your saved keys again
$ ssh-add -l
Now you need to add the generated public keys to your github/bickbuket server Acces Keys
Clone each of the repos to different folders
Go to the folder where user work will be used and do this
$ git config user.name "Working Hard"
$ git config user.email "work@company.com"
Just to see what this does check the contents of the ".git/config"
Go to the folder where user activehacker will be used and do this
$ git config user.name "Personal Account"
$ git config user.email "pers@email.com"
Just to see what this does check the contents of the ".git/config"
After all this you will be able to commit your personal and work code by switch folders
If you have any problems let me know mgg.isco@gmail.com
add a comment |
up vote
0
down vote
Although most questions sort of answered the OP, I just had to go through this myself and without even googling I was able to find the quickest and simplest solution. Here's simple steps:
- copy existing
.gitconfg
from your other repo - paste into your newly added repo
- change values in
.gitconfig
file, such as name, email and username
[user]
name = John
email = john@email.net
username = john133
- add filename to
.gitignore
list, to make sure you don't commit.gitconfig
file to your work repo
add a comment |
up vote
0
down vote
Just add this to your ~/.bash_profile to switch between default keys for github.com
# Git SSH keys swap
alias work_git="ssh-add -D && ssh-add -K ~/.ssh/id_rsa_work"
alias personal_git="ssh-add -D && ssh-add -K ~/.ssh/id_rsa"
add a comment |
19 Answers
19
active
oldest
votes
19 Answers
19
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
773
down vote
accepted
You can configure an individual repo to use a specific user / email address which overrides the global configuration. From the root of the repo, run
git config user.name "Your Name Here"
git config user.email your@email.com
whereas the default user / email is configured in your ~/.gitconfig
git config --global user.name "Your Name Here"
git config --global user.email your@email.com
66
you can see the effects of these settings in the.git/config
file
– Abizern
Nov 18 '10 at 23:15
19
You can manually edit those config files withgit config --edit
andgit config --global --edit
. And in case you missed Abizern’s comment, a repository’s config file is at<repo-root>/.git/config
.
– Rory O'Kane
Apr 25 '12 at 20:01
12
You should also unset GIT_AUTHOR_EMAIL and GIT_COMMITTER_EMAIL (and *_NAME) as they will override the local settings
– ACyclic
Sep 14 '12 at 9:58
6
Is there a way to do this for all repos in a given folder, rather than for individual repos? See my question here: stackoverflow.com/questions/21307793/…
– scubbo
Jan 23 '14 at 12:13
4
See this answer below for more upto-date solution withGit 2.13
released today.
– tejasbubane
May 10 '17 at 11:20
|
show 6 more comments
up vote
773
down vote
accepted
You can configure an individual repo to use a specific user / email address which overrides the global configuration. From the root of the repo, run
git config user.name "Your Name Here"
git config user.email your@email.com
whereas the default user / email is configured in your ~/.gitconfig
git config --global user.name "Your Name Here"
git config --global user.email your@email.com
66
you can see the effects of these settings in the.git/config
file
– Abizern
Nov 18 '10 at 23:15
19
You can manually edit those config files withgit config --edit
andgit config --global --edit
. And in case you missed Abizern’s comment, a repository’s config file is at<repo-root>/.git/config
.
– Rory O'Kane
Apr 25 '12 at 20:01
12
You should also unset GIT_AUTHOR_EMAIL and GIT_COMMITTER_EMAIL (and *_NAME) as they will override the local settings
– ACyclic
Sep 14 '12 at 9:58
6
Is there a way to do this for all repos in a given folder, rather than for individual repos? See my question here: stackoverflow.com/questions/21307793/…
– scubbo
Jan 23 '14 at 12:13
4
See this answer below for more upto-date solution withGit 2.13
released today.
– tejasbubane
May 10 '17 at 11:20
|
show 6 more comments
up vote
773
down vote
accepted
up vote
773
down vote
accepted
You can configure an individual repo to use a specific user / email address which overrides the global configuration. From the root of the repo, run
git config user.name "Your Name Here"
git config user.email your@email.com
whereas the default user / email is configured in your ~/.gitconfig
git config --global user.name "Your Name Here"
git config --global user.email your@email.com
You can configure an individual repo to use a specific user / email address which overrides the global configuration. From the root of the repo, run
git config user.name "Your Name Here"
git config user.email your@email.com
whereas the default user / email is configured in your ~/.gitconfig
git config --global user.name "Your Name Here"
git config --global user.email your@email.com
edited Aug 25 '15 at 11:04
mitch
171311
171311
answered Nov 18 '10 at 22:56
discomurray
8,97711711
8,97711711
66
you can see the effects of these settings in the.git/config
file
– Abizern
Nov 18 '10 at 23:15
19
You can manually edit those config files withgit config --edit
andgit config --global --edit
. And in case you missed Abizern’s comment, a repository’s config file is at<repo-root>/.git/config
.
– Rory O'Kane
Apr 25 '12 at 20:01
12
You should also unset GIT_AUTHOR_EMAIL and GIT_COMMITTER_EMAIL (and *_NAME) as they will override the local settings
– ACyclic
Sep 14 '12 at 9:58
6
Is there a way to do this for all repos in a given folder, rather than for individual repos? See my question here: stackoverflow.com/questions/21307793/…
– scubbo
Jan 23 '14 at 12:13
4
See this answer below for more upto-date solution withGit 2.13
released today.
– tejasbubane
May 10 '17 at 11:20
|
show 6 more comments
66
you can see the effects of these settings in the.git/config
file
– Abizern
Nov 18 '10 at 23:15
19
You can manually edit those config files withgit config --edit
andgit config --global --edit
. And in case you missed Abizern’s comment, a repository’s config file is at<repo-root>/.git/config
.
– Rory O'Kane
Apr 25 '12 at 20:01
12
You should also unset GIT_AUTHOR_EMAIL and GIT_COMMITTER_EMAIL (and *_NAME) as they will override the local settings
– ACyclic
Sep 14 '12 at 9:58
6
Is there a way to do this for all repos in a given folder, rather than for individual repos? See my question here: stackoverflow.com/questions/21307793/…
– scubbo
Jan 23 '14 at 12:13
4
See this answer below for more upto-date solution withGit 2.13
released today.
– tejasbubane
May 10 '17 at 11:20
66
66
you can see the effects of these settings in the
.git/config
file– Abizern
Nov 18 '10 at 23:15
you can see the effects of these settings in the
.git/config
file– Abizern
Nov 18 '10 at 23:15
19
19
You can manually edit those config files with
git config --edit
and git config --global --edit
. And in case you missed Abizern’s comment, a repository’s config file is at <repo-root>/.git/config
.– Rory O'Kane
Apr 25 '12 at 20:01
You can manually edit those config files with
git config --edit
and git config --global --edit
. And in case you missed Abizern’s comment, a repository’s config file is at <repo-root>/.git/config
.– Rory O'Kane
Apr 25 '12 at 20:01
12
12
You should also unset GIT_AUTHOR_EMAIL and GIT_COMMITTER_EMAIL (and *_NAME) as they will override the local settings
– ACyclic
Sep 14 '12 at 9:58
You should also unset GIT_AUTHOR_EMAIL and GIT_COMMITTER_EMAIL (and *_NAME) as they will override the local settings
– ACyclic
Sep 14 '12 at 9:58
6
6
Is there a way to do this for all repos in a given folder, rather than for individual repos? See my question here: stackoverflow.com/questions/21307793/…
– scubbo
Jan 23 '14 at 12:13
Is there a way to do this for all repos in a given folder, rather than for individual repos? See my question here: stackoverflow.com/questions/21307793/…
– scubbo
Jan 23 '14 at 12:13
4
4
See this answer below for more upto-date solution with
Git 2.13
released today.– tejasbubane
May 10 '17 at 11:20
See this answer below for more upto-date solution with
Git 2.13
released today.– tejasbubane
May 10 '17 at 11:20
|
show 6 more comments
up vote
207
down vote
Since git 2.13, it is possible to solve this using newly introduced Conditional includes.
An example:
Global config ~/.gitconfig
[user]
name = John Doe
email = john@doe.tld
[includeIf "gitdir:~/work/"]
path = ~/work/.gitconfig
Work specific config ~/work/.gitconfig
[user]
email = john.doe@company.tld
22
This is the best answer now that git 2.13 has been released.
– tejasbubane
May 10 '17 at 11:18
Would this solution work for all subdirectories within the one specified in the include statement, assuming they have no .gitconfig files of their own? I would think so, but so far testing hasn't borne this out.
– Gary
Jun 30 '17 at 19:28
@Gary Yes, according to my experience and the docs: “If the pattern ends with /, ** will be automatically added. For example, the pattern foo/ becomes foo/**. In other words, it matches "foo" and everything inside, recursively.”, “; include for all repositories inside $HOME/to/group [includeIf "gitdir:~/to/group/"]”
– Tomáš Janoušek
Jun 30 '17 at 21:38
This is exactly what I needed. I did have to go to 2.13.1 docs to find out more about theincludeIf
section: git-scm.com/docs/git-config/2.13.1#_conditional_includes
– gligoran
Aug 15 '17 at 9:51
@gligoran There seems to be something wrong with the website. It says there are no changes between 2.13.0 and 2.13.1 version of git-config manpage, yet the 2.13.0 one doesn't mention includeIf. :-(
– Tomáš Janoušek
Aug 15 '17 at 11:26
|
show 2 more comments
up vote
207
down vote
Since git 2.13, it is possible to solve this using newly introduced Conditional includes.
An example:
Global config ~/.gitconfig
[user]
name = John Doe
email = john@doe.tld
[includeIf "gitdir:~/work/"]
path = ~/work/.gitconfig
Work specific config ~/work/.gitconfig
[user]
email = john.doe@company.tld
22
This is the best answer now that git 2.13 has been released.
– tejasbubane
May 10 '17 at 11:18
Would this solution work for all subdirectories within the one specified in the include statement, assuming they have no .gitconfig files of their own? I would think so, but so far testing hasn't borne this out.
– Gary
Jun 30 '17 at 19:28
@Gary Yes, according to my experience and the docs: “If the pattern ends with /, ** will be automatically added. For example, the pattern foo/ becomes foo/**. In other words, it matches "foo" and everything inside, recursively.”, “; include for all repositories inside $HOME/to/group [includeIf "gitdir:~/to/group/"]”
– Tomáš Janoušek
Jun 30 '17 at 21:38
This is exactly what I needed. I did have to go to 2.13.1 docs to find out more about theincludeIf
section: git-scm.com/docs/git-config/2.13.1#_conditional_includes
– gligoran
Aug 15 '17 at 9:51
@gligoran There seems to be something wrong with the website. It says there are no changes between 2.13.0 and 2.13.1 version of git-config manpage, yet the 2.13.0 one doesn't mention includeIf. :-(
– Tomáš Janoušek
Aug 15 '17 at 11:26
|
show 2 more comments
up vote
207
down vote
up vote
207
down vote
Since git 2.13, it is possible to solve this using newly introduced Conditional includes.
An example:
Global config ~/.gitconfig
[user]
name = John Doe
email = john@doe.tld
[includeIf "gitdir:~/work/"]
path = ~/work/.gitconfig
Work specific config ~/work/.gitconfig
[user]
email = john.doe@company.tld
Since git 2.13, it is possible to solve this using newly introduced Conditional includes.
An example:
Global config ~/.gitconfig
[user]
name = John Doe
email = john@doe.tld
[includeIf "gitdir:~/work/"]
path = ~/work/.gitconfig
Work specific config ~/work/.gitconfig
[user]
email = john.doe@company.tld
edited May 17 '17 at 21:21
answered Apr 27 '17 at 9:40
Tomáš Janoušek
2,3601711
2,3601711
22
This is the best answer now that git 2.13 has been released.
– tejasbubane
May 10 '17 at 11:18
Would this solution work for all subdirectories within the one specified in the include statement, assuming they have no .gitconfig files of their own? I would think so, but so far testing hasn't borne this out.
– Gary
Jun 30 '17 at 19:28
@Gary Yes, according to my experience and the docs: “If the pattern ends with /, ** will be automatically added. For example, the pattern foo/ becomes foo/**. In other words, it matches "foo" and everything inside, recursively.”, “; include for all repositories inside $HOME/to/group [includeIf "gitdir:~/to/group/"]”
– Tomáš Janoušek
Jun 30 '17 at 21:38
This is exactly what I needed. I did have to go to 2.13.1 docs to find out more about theincludeIf
section: git-scm.com/docs/git-config/2.13.1#_conditional_includes
– gligoran
Aug 15 '17 at 9:51
@gligoran There seems to be something wrong with the website. It says there are no changes between 2.13.0 and 2.13.1 version of git-config manpage, yet the 2.13.0 one doesn't mention includeIf. :-(
– Tomáš Janoušek
Aug 15 '17 at 11:26
|
show 2 more comments
22
This is the best answer now that git 2.13 has been released.
– tejasbubane
May 10 '17 at 11:18
Would this solution work for all subdirectories within the one specified in the include statement, assuming they have no .gitconfig files of their own? I would think so, but so far testing hasn't borne this out.
– Gary
Jun 30 '17 at 19:28
@Gary Yes, according to my experience and the docs: “If the pattern ends with /, ** will be automatically added. For example, the pattern foo/ becomes foo/**. In other words, it matches "foo" and everything inside, recursively.”, “; include for all repositories inside $HOME/to/group [includeIf "gitdir:~/to/group/"]”
– Tomáš Janoušek
Jun 30 '17 at 21:38
This is exactly what I needed. I did have to go to 2.13.1 docs to find out more about theincludeIf
section: git-scm.com/docs/git-config/2.13.1#_conditional_includes
– gligoran
Aug 15 '17 at 9:51
@gligoran There seems to be something wrong with the website. It says there are no changes between 2.13.0 and 2.13.1 version of git-config manpage, yet the 2.13.0 one doesn't mention includeIf. :-(
– Tomáš Janoušek
Aug 15 '17 at 11:26
22
22
This is the best answer now that git 2.13 has been released.
– tejasbubane
May 10 '17 at 11:18
This is the best answer now that git 2.13 has been released.
– tejasbubane
May 10 '17 at 11:18
Would this solution work for all subdirectories within the one specified in the include statement, assuming they have no .gitconfig files of their own? I would think so, but so far testing hasn't borne this out.
– Gary
Jun 30 '17 at 19:28
Would this solution work for all subdirectories within the one specified in the include statement, assuming they have no .gitconfig files of their own? I would think so, but so far testing hasn't borne this out.
– Gary
Jun 30 '17 at 19:28
@Gary Yes, according to my experience and the docs: “If the pattern ends with /, ** will be automatically added. For example, the pattern foo/ becomes foo/**. In other words, it matches "foo" and everything inside, recursively.”, “; include for all repositories inside $HOME/to/group [includeIf "gitdir:~/to/group/"]”
– Tomáš Janoušek
Jun 30 '17 at 21:38
@Gary Yes, according to my experience and the docs: “If the pattern ends with /, ** will be automatically added. For example, the pattern foo/ becomes foo/**. In other words, it matches "foo" and everything inside, recursively.”, “; include for all repositories inside $HOME/to/group [includeIf "gitdir:~/to/group/"]”
– Tomáš Janoušek
Jun 30 '17 at 21:38
This is exactly what I needed. I did have to go to 2.13.1 docs to find out more about the
includeIf
section: git-scm.com/docs/git-config/2.13.1#_conditional_includes– gligoran
Aug 15 '17 at 9:51
This is exactly what I needed. I did have to go to 2.13.1 docs to find out more about the
includeIf
section: git-scm.com/docs/git-config/2.13.1#_conditional_includes– gligoran
Aug 15 '17 at 9:51
@gligoran There seems to be something wrong with the website. It says there are no changes between 2.13.0 and 2.13.1 version of git-config manpage, yet the 2.13.0 one doesn't mention includeIf. :-(
– Tomáš Janoušek
Aug 15 '17 at 11:26
@gligoran There seems to be something wrong with the website. It says there are no changes between 2.13.0 and 2.13.1 version of git-config manpage, yet the 2.13.0 one doesn't mention includeIf. :-(
– Tomáš Janoušek
Aug 15 '17 at 11:26
|
show 2 more comments
up vote
96
down vote
Or you can add following information in your local .git/config
file
[user]
name = Your Name
email = your.email@gmail.com
18
...which is the manual way to do what the commands recommended by @discomurray do for you.
– user456584
Jul 10 '14 at 21:15
2
I wish I could upvote this 9 more times
– Genia S.
Jul 30 '14 at 3:52
1
is it possible to add multiple lines of this per remote?
– Abel Callejo
Jul 14 '17 at 0:22
sometimes doing things manually is not that bad.
– xxks-kkk
Jun 13 at 14:54
add a comment |
up vote
96
down vote
Or you can add following information in your local .git/config
file
[user]
name = Your Name
email = your.email@gmail.com
18
...which is the manual way to do what the commands recommended by @discomurray do for you.
– user456584
Jul 10 '14 at 21:15
2
I wish I could upvote this 9 more times
– Genia S.
Jul 30 '14 at 3:52
1
is it possible to add multiple lines of this per remote?
– Abel Callejo
Jul 14 '17 at 0:22
sometimes doing things manually is not that bad.
– xxks-kkk
Jun 13 at 14:54
add a comment |
up vote
96
down vote
up vote
96
down vote
Or you can add following information in your local .git/config
file
[user]
name = Your Name
email = your.email@gmail.com
Or you can add following information in your local .git/config
file
[user]
name = Your Name
email = your.email@gmail.com
answered Apr 12 '13 at 11:03
Rahul Prasad
4,58963345
4,58963345
18
...which is the manual way to do what the commands recommended by @discomurray do for you.
– user456584
Jul 10 '14 at 21:15
2
I wish I could upvote this 9 more times
– Genia S.
Jul 30 '14 at 3:52
1
is it possible to add multiple lines of this per remote?
– Abel Callejo
Jul 14 '17 at 0:22
sometimes doing things manually is not that bad.
– xxks-kkk
Jun 13 at 14:54
add a comment |
18
...which is the manual way to do what the commands recommended by @discomurray do for you.
– user456584
Jul 10 '14 at 21:15
2
I wish I could upvote this 9 more times
– Genia S.
Jul 30 '14 at 3:52
1
is it possible to add multiple lines of this per remote?
– Abel Callejo
Jul 14 '17 at 0:22
sometimes doing things manually is not that bad.
– xxks-kkk
Jun 13 at 14:54
18
18
...which is the manual way to do what the commands recommended by @discomurray do for you.
– user456584
Jul 10 '14 at 21:15
...which is the manual way to do what the commands recommended by @discomurray do for you.
– user456584
Jul 10 '14 at 21:15
2
2
I wish I could upvote this 9 more times
– Genia S.
Jul 30 '14 at 3:52
I wish I could upvote this 9 more times
– Genia S.
Jul 30 '14 at 3:52
1
1
is it possible to add multiple lines of this per remote?
– Abel Callejo
Jul 14 '17 at 0:22
is it possible to add multiple lines of this per remote?
– Abel Callejo
Jul 14 '17 at 0:22
sometimes doing things manually is not that bad.
– xxks-kkk
Jun 13 at 14:54
sometimes doing things manually is not that bad.
– xxks-kkk
Jun 13 at 14:54
add a comment |
up vote
33
down vote
After getting some inspiration from Orr Sella's blog post I wrote a pre-commit hook (resides in ~/.git/templates/hooks
) which would set specific usernames and e-mail addresses based on the information inside a local repositorie's ./.git/config
:
You have to place the path to the template directory into your ~/.gitconfig
:
[init]
templatedir = ~/.git/templates
Then each git init
or git clone
will pick up that hook and will apply the user data during the next git commit
. If you want to apply the hook to already exisiting repos then just run a git init
inside the repo in order to reinitialize it.
Here is the hook I came up with (it still needs some polishing - suggestions are welcome).
Save it either as
~/.git/templates/hooks/pre_commit
or
~/.git/templates/hooks/post-checkout
and make sure it is executable: chmod +x ./post-checkout || chmod +x ./pre_commit
#!/usr/bin/env bash
# -------- USER CONFIG
# Patterns to match a repo's "remote.origin.url" - beginning portion of the hostname
git_remotes[0]="Github"
git_remotes[1]="Gitlab"
# Adjust names and e-mail addresses
local_id_0[0]="my_name_0"
local_id_0[1]="my_email_0"
local_id_1[0]="my_name_1"
local_id_1[1]="my_email_1"
local_fallback_id[0]="${local_id_0[0]}"
local_fallback_id[1]="${local_id_0[1]}"
# -------- FUNCTIONS
setIdentity()
{
local current_id local_id
current_id[0]="$(git config --get --local user.name)"
current_id[1]="$(git config --get --local user.email)"
local_id=("$@")
if [[ "${current_id[0]}" == "${local_id[0]}" &&
"${current_id[1]}" == "${local_id[1]}" ]]; then
printf " Local identity is:n"
printf "» User: %sn» Mail: %snn" "${current_id[@]}"
else
printf "» User: %sn» Mail: %snn" "${local_id[@]}"
git config --local user.name "${local_id[0]}"
git config --local user.email "${local_id[1]}"
fi
return 0
}
# -------- IMPLEMENTATION
current_remote_url="$(git config --get --local remote.origin.url)"
if [[ "$current_remote_url" ]]; then
for service in "${git_remotes[@]}"; do
# Disable case sensitivity for regex matching
shopt -s nocasematch
if [[ "$current_remote_url" =~ $service ]]; then
case "$service" in
"${git_remotes[0]}" )
printf "n»» An Intermissionn» %s repository found." "${git_remotes[0]}"
setIdentity "${local_id_0[@]}"
exit 0
;;
"${git_remotes[1]}" )
printf "n»» An Intermissionn» %s repository found." "${git_remotes[1]}"
setIdentity "${local_id_1[@]}"
exit 0
;;
* )
printf "n» pre-commit hook: unknown errorn» Quitting.n"
exit 1
;;
esac
fi
done
else
printf "n»» An Intermissionn» No remote repository set. Using local fallback identity:n"
printf "» User: %sn» Mail: %snn" "${local_fallback_id[@]}"
# Get the user's attention for a second
sleep 1
git config --local user.name "${local_fallback_id[0]}"
git config --local user.email "${local_fallback_id[1]}"
fi
exit 0
EDIT:
So I rewrote the hook as a hook and command in Python. Additionally it's possible to call the script as a Git command (git passport
), too. Also it's possible to define an arbitrary number of IDs inside a configfile (~/.gitpassport
) which are selectable on a prompt. You can find the project at github.com: git-passport - A Git command and hook written in Python to manage multiple Git accounts / user identities.
1
This works neatly. However, it worked better for me by making this a post-checkout (instead of post-commit) hook. My suggestions to improve this answer are, mention that: 1. the snippet needs to be saved as ~/.git/templates/hooks/post-checkout and be given permissionchmod +x post-checkout
, 2.git_remotes
values are beginning portion of the entire hostname, e.g.git@github.com
, 3.local_id
values should be edited by user to respective names and email addresses.
– Shantanu Kumar
Dec 24 '14 at 16:37
@ShantanuKumar Thanks for your comment. I adjusted the answer as you proposed. Maybe I will rewrite the script in Python soon.
– Saucier
Dec 29 '14 at 16:41
Added support for regular expressions and for repositories without remotes. For easy download all merged here. Regexps to distinguish identities for different projects on the same service. And supporting remoteless repos with e.g. a default identity makes sense if yougit init
new projects from an IDE such aseclipse
(which cannot handle interactive pre-commit triggers)
– cfi
Oct 27 '15 at 12:57
add a comment |
up vote
33
down vote
After getting some inspiration from Orr Sella's blog post I wrote a pre-commit hook (resides in ~/.git/templates/hooks
) which would set specific usernames and e-mail addresses based on the information inside a local repositorie's ./.git/config
:
You have to place the path to the template directory into your ~/.gitconfig
:
[init]
templatedir = ~/.git/templates
Then each git init
or git clone
will pick up that hook and will apply the user data during the next git commit
. If you want to apply the hook to already exisiting repos then just run a git init
inside the repo in order to reinitialize it.
Here is the hook I came up with (it still needs some polishing - suggestions are welcome).
Save it either as
~/.git/templates/hooks/pre_commit
or
~/.git/templates/hooks/post-checkout
and make sure it is executable: chmod +x ./post-checkout || chmod +x ./pre_commit
#!/usr/bin/env bash
# -------- USER CONFIG
# Patterns to match a repo's "remote.origin.url" - beginning portion of the hostname
git_remotes[0]="Github"
git_remotes[1]="Gitlab"
# Adjust names and e-mail addresses
local_id_0[0]="my_name_0"
local_id_0[1]="my_email_0"
local_id_1[0]="my_name_1"
local_id_1[1]="my_email_1"
local_fallback_id[0]="${local_id_0[0]}"
local_fallback_id[1]="${local_id_0[1]}"
# -------- FUNCTIONS
setIdentity()
{
local current_id local_id
current_id[0]="$(git config --get --local user.name)"
current_id[1]="$(git config --get --local user.email)"
local_id=("$@")
if [[ "${current_id[0]}" == "${local_id[0]}" &&
"${current_id[1]}" == "${local_id[1]}" ]]; then
printf " Local identity is:n"
printf "» User: %sn» Mail: %snn" "${current_id[@]}"
else
printf "» User: %sn» Mail: %snn" "${local_id[@]}"
git config --local user.name "${local_id[0]}"
git config --local user.email "${local_id[1]}"
fi
return 0
}
# -------- IMPLEMENTATION
current_remote_url="$(git config --get --local remote.origin.url)"
if [[ "$current_remote_url" ]]; then
for service in "${git_remotes[@]}"; do
# Disable case sensitivity for regex matching
shopt -s nocasematch
if [[ "$current_remote_url" =~ $service ]]; then
case "$service" in
"${git_remotes[0]}" )
printf "n»» An Intermissionn» %s repository found." "${git_remotes[0]}"
setIdentity "${local_id_0[@]}"
exit 0
;;
"${git_remotes[1]}" )
printf "n»» An Intermissionn» %s repository found." "${git_remotes[1]}"
setIdentity "${local_id_1[@]}"
exit 0
;;
* )
printf "n» pre-commit hook: unknown errorn» Quitting.n"
exit 1
;;
esac
fi
done
else
printf "n»» An Intermissionn» No remote repository set. Using local fallback identity:n"
printf "» User: %sn» Mail: %snn" "${local_fallback_id[@]}"
# Get the user's attention for a second
sleep 1
git config --local user.name "${local_fallback_id[0]}"
git config --local user.email "${local_fallback_id[1]}"
fi
exit 0
EDIT:
So I rewrote the hook as a hook and command in Python. Additionally it's possible to call the script as a Git command (git passport
), too. Also it's possible to define an arbitrary number of IDs inside a configfile (~/.gitpassport
) which are selectable on a prompt. You can find the project at github.com: git-passport - A Git command and hook written in Python to manage multiple Git accounts / user identities.
1
This works neatly. However, it worked better for me by making this a post-checkout (instead of post-commit) hook. My suggestions to improve this answer are, mention that: 1. the snippet needs to be saved as ~/.git/templates/hooks/post-checkout and be given permissionchmod +x post-checkout
, 2.git_remotes
values are beginning portion of the entire hostname, e.g.git@github.com
, 3.local_id
values should be edited by user to respective names and email addresses.
– Shantanu Kumar
Dec 24 '14 at 16:37
@ShantanuKumar Thanks for your comment. I adjusted the answer as you proposed. Maybe I will rewrite the script in Python soon.
– Saucier
Dec 29 '14 at 16:41
Added support for regular expressions and for repositories without remotes. For easy download all merged here. Regexps to distinguish identities for different projects on the same service. And supporting remoteless repos with e.g. a default identity makes sense if yougit init
new projects from an IDE such aseclipse
(which cannot handle interactive pre-commit triggers)
– cfi
Oct 27 '15 at 12:57
add a comment |
up vote
33
down vote
up vote
33
down vote
After getting some inspiration from Orr Sella's blog post I wrote a pre-commit hook (resides in ~/.git/templates/hooks
) which would set specific usernames and e-mail addresses based on the information inside a local repositorie's ./.git/config
:
You have to place the path to the template directory into your ~/.gitconfig
:
[init]
templatedir = ~/.git/templates
Then each git init
or git clone
will pick up that hook and will apply the user data during the next git commit
. If you want to apply the hook to already exisiting repos then just run a git init
inside the repo in order to reinitialize it.
Here is the hook I came up with (it still needs some polishing - suggestions are welcome).
Save it either as
~/.git/templates/hooks/pre_commit
or
~/.git/templates/hooks/post-checkout
and make sure it is executable: chmod +x ./post-checkout || chmod +x ./pre_commit
#!/usr/bin/env bash
# -------- USER CONFIG
# Patterns to match a repo's "remote.origin.url" - beginning portion of the hostname
git_remotes[0]="Github"
git_remotes[1]="Gitlab"
# Adjust names and e-mail addresses
local_id_0[0]="my_name_0"
local_id_0[1]="my_email_0"
local_id_1[0]="my_name_1"
local_id_1[1]="my_email_1"
local_fallback_id[0]="${local_id_0[0]}"
local_fallback_id[1]="${local_id_0[1]}"
# -------- FUNCTIONS
setIdentity()
{
local current_id local_id
current_id[0]="$(git config --get --local user.name)"
current_id[1]="$(git config --get --local user.email)"
local_id=("$@")
if [[ "${current_id[0]}" == "${local_id[0]}" &&
"${current_id[1]}" == "${local_id[1]}" ]]; then
printf " Local identity is:n"
printf "» User: %sn» Mail: %snn" "${current_id[@]}"
else
printf "» User: %sn» Mail: %snn" "${local_id[@]}"
git config --local user.name "${local_id[0]}"
git config --local user.email "${local_id[1]}"
fi
return 0
}
# -------- IMPLEMENTATION
current_remote_url="$(git config --get --local remote.origin.url)"
if [[ "$current_remote_url" ]]; then
for service in "${git_remotes[@]}"; do
# Disable case sensitivity for regex matching
shopt -s nocasematch
if [[ "$current_remote_url" =~ $service ]]; then
case "$service" in
"${git_remotes[0]}" )
printf "n»» An Intermissionn» %s repository found." "${git_remotes[0]}"
setIdentity "${local_id_0[@]}"
exit 0
;;
"${git_remotes[1]}" )
printf "n»» An Intermissionn» %s repository found." "${git_remotes[1]}"
setIdentity "${local_id_1[@]}"
exit 0
;;
* )
printf "n» pre-commit hook: unknown errorn» Quitting.n"
exit 1
;;
esac
fi
done
else
printf "n»» An Intermissionn» No remote repository set. Using local fallback identity:n"
printf "» User: %sn» Mail: %snn" "${local_fallback_id[@]}"
# Get the user's attention for a second
sleep 1
git config --local user.name "${local_fallback_id[0]}"
git config --local user.email "${local_fallback_id[1]}"
fi
exit 0
EDIT:
So I rewrote the hook as a hook and command in Python. Additionally it's possible to call the script as a Git command (git passport
), too. Also it's possible to define an arbitrary number of IDs inside a configfile (~/.gitpassport
) which are selectable on a prompt. You can find the project at github.com: git-passport - A Git command and hook written in Python to manage multiple Git accounts / user identities.
After getting some inspiration from Orr Sella's blog post I wrote a pre-commit hook (resides in ~/.git/templates/hooks
) which would set specific usernames and e-mail addresses based on the information inside a local repositorie's ./.git/config
:
You have to place the path to the template directory into your ~/.gitconfig
:
[init]
templatedir = ~/.git/templates
Then each git init
or git clone
will pick up that hook and will apply the user data during the next git commit
. If you want to apply the hook to already exisiting repos then just run a git init
inside the repo in order to reinitialize it.
Here is the hook I came up with (it still needs some polishing - suggestions are welcome).
Save it either as
~/.git/templates/hooks/pre_commit
or
~/.git/templates/hooks/post-checkout
and make sure it is executable: chmod +x ./post-checkout || chmod +x ./pre_commit
#!/usr/bin/env bash
# -------- USER CONFIG
# Patterns to match a repo's "remote.origin.url" - beginning portion of the hostname
git_remotes[0]="Github"
git_remotes[1]="Gitlab"
# Adjust names and e-mail addresses
local_id_0[0]="my_name_0"
local_id_0[1]="my_email_0"
local_id_1[0]="my_name_1"
local_id_1[1]="my_email_1"
local_fallback_id[0]="${local_id_0[0]}"
local_fallback_id[1]="${local_id_0[1]}"
# -------- FUNCTIONS
setIdentity()
{
local current_id local_id
current_id[0]="$(git config --get --local user.name)"
current_id[1]="$(git config --get --local user.email)"
local_id=("$@")
if [[ "${current_id[0]}" == "${local_id[0]}" &&
"${current_id[1]}" == "${local_id[1]}" ]]; then
printf " Local identity is:n"
printf "» User: %sn» Mail: %snn" "${current_id[@]}"
else
printf "» User: %sn» Mail: %snn" "${local_id[@]}"
git config --local user.name "${local_id[0]}"
git config --local user.email "${local_id[1]}"
fi
return 0
}
# -------- IMPLEMENTATION
current_remote_url="$(git config --get --local remote.origin.url)"
if [[ "$current_remote_url" ]]; then
for service in "${git_remotes[@]}"; do
# Disable case sensitivity for regex matching
shopt -s nocasematch
if [[ "$current_remote_url" =~ $service ]]; then
case "$service" in
"${git_remotes[0]}" )
printf "n»» An Intermissionn» %s repository found." "${git_remotes[0]}"
setIdentity "${local_id_0[@]}"
exit 0
;;
"${git_remotes[1]}" )
printf "n»» An Intermissionn» %s repository found." "${git_remotes[1]}"
setIdentity "${local_id_1[@]}"
exit 0
;;
* )
printf "n» pre-commit hook: unknown errorn» Quitting.n"
exit 1
;;
esac
fi
done
else
printf "n»» An Intermissionn» No remote repository set. Using local fallback identity:n"
printf "» User: %sn» Mail: %snn" "${local_fallback_id[@]}"
# Get the user's attention for a second
sleep 1
git config --local user.name "${local_fallback_id[0]}"
git config --local user.email "${local_fallback_id[1]}"
fi
exit 0
EDIT:
So I rewrote the hook as a hook and command in Python. Additionally it's possible to call the script as a Git command (git passport
), too. Also it's possible to define an arbitrary number of IDs inside a configfile (~/.gitpassport
) which are selectable on a prompt. You can find the project at github.com: git-passport - A Git command and hook written in Python to manage multiple Git accounts / user identities.
edited Apr 12 '15 at 0:30
answered Apr 16 '14 at 10:37
Saucier
2,5491440
2,5491440
1
This works neatly. However, it worked better for me by making this a post-checkout (instead of post-commit) hook. My suggestions to improve this answer are, mention that: 1. the snippet needs to be saved as ~/.git/templates/hooks/post-checkout and be given permissionchmod +x post-checkout
, 2.git_remotes
values are beginning portion of the entire hostname, e.g.git@github.com
, 3.local_id
values should be edited by user to respective names and email addresses.
– Shantanu Kumar
Dec 24 '14 at 16:37
@ShantanuKumar Thanks for your comment. I adjusted the answer as you proposed. Maybe I will rewrite the script in Python soon.
– Saucier
Dec 29 '14 at 16:41
Added support for regular expressions and for repositories without remotes. For easy download all merged here. Regexps to distinguish identities for different projects on the same service. And supporting remoteless repos with e.g. a default identity makes sense if yougit init
new projects from an IDE such aseclipse
(which cannot handle interactive pre-commit triggers)
– cfi
Oct 27 '15 at 12:57
add a comment |
1
This works neatly. However, it worked better for me by making this a post-checkout (instead of post-commit) hook. My suggestions to improve this answer are, mention that: 1. the snippet needs to be saved as ~/.git/templates/hooks/post-checkout and be given permissionchmod +x post-checkout
, 2.git_remotes
values are beginning portion of the entire hostname, e.g.git@github.com
, 3.local_id
values should be edited by user to respective names and email addresses.
– Shantanu Kumar
Dec 24 '14 at 16:37
@ShantanuKumar Thanks for your comment. I adjusted the answer as you proposed. Maybe I will rewrite the script in Python soon.
– Saucier
Dec 29 '14 at 16:41
Added support for regular expressions and for repositories without remotes. For easy download all merged here. Regexps to distinguish identities for different projects on the same service. And supporting remoteless repos with e.g. a default identity makes sense if yougit init
new projects from an IDE such aseclipse
(which cannot handle interactive pre-commit triggers)
– cfi
Oct 27 '15 at 12:57
1
1
This works neatly. However, it worked better for me by making this a post-checkout (instead of post-commit) hook. My suggestions to improve this answer are, mention that: 1. the snippet needs to be saved as ~/.git/templates/hooks/post-checkout and be given permission
chmod +x post-checkout
, 2. git_remotes
values are beginning portion of the entire hostname, e.g. git@github.com
, 3. local_id
values should be edited by user to respective names and email addresses.– Shantanu Kumar
Dec 24 '14 at 16:37
This works neatly. However, it worked better for me by making this a post-checkout (instead of post-commit) hook. My suggestions to improve this answer are, mention that: 1. the snippet needs to be saved as ~/.git/templates/hooks/post-checkout and be given permission
chmod +x post-checkout
, 2. git_remotes
values are beginning portion of the entire hostname, e.g. git@github.com
, 3. local_id
values should be edited by user to respective names and email addresses.– Shantanu Kumar
Dec 24 '14 at 16:37
@ShantanuKumar Thanks for your comment. I adjusted the answer as you proposed. Maybe I will rewrite the script in Python soon.
– Saucier
Dec 29 '14 at 16:41
@ShantanuKumar Thanks for your comment. I adjusted the answer as you proposed. Maybe I will rewrite the script in Python soon.
– Saucier
Dec 29 '14 at 16:41
Added support for regular expressions and for repositories without remotes. For easy download all merged here. Regexps to distinguish identities for different projects on the same service. And supporting remoteless repos with e.g. a default identity makes sense if you
git init
new projects from an IDE such as eclipse
(which cannot handle interactive pre-commit triggers)– cfi
Oct 27 '15 at 12:57
Added support for regular expressions and for repositories without remotes. For easy download all merged here. Regexps to distinguish identities for different projects on the same service. And supporting remoteless repos with e.g. a default identity makes sense if you
git init
new projects from an IDE such as eclipse
(which cannot handle interactive pre-commit triggers)– cfi
Oct 27 '15 at 12:57
add a comment |
up vote
23
down vote
One command github accounts switch
This solution takes the form of a single git alias. Once executed, the current project user will be attached to another account
Generate ssh keys
ssh-keygen -t rsa -C "rinquin.arnaud@gmail.com" -f '/Users/arnaudrinquin/.ssh/id_rsa'
[...]
ssh-keygen -t rsa -C "arnaud.rinquin@wopata.com" -f '/Users/arnaudrinquin/.ssh/id_rsa_pro'
Link them to your GitHub / Bitbucket accounts
- copy default public key
pbcopy < ~/.ssh/id_rsa.pub
- login to your GitHub acount
- paste the key in the
add SSH key
github page - copy other public key
pbcopy < ~/.ssh/id_rsa_pro.pub
- repeat and adapt steps 2 to 4 for every other account
Step 1. Automatic ssh key switching.
We can configure ssh
to send a use a specific encryption key depending on the host
. The nice thing is that you can have several aliases for the same hostname
.
See this example ~/.ssh/config
file:
# Default GitHub
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa
# Professional github alias
Host github_pro
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_pro
git remote configuration
You can now use these aliases in the git remotes by changing git@github.com
by git@github_pro
.
You can either change your existing projects remotes (using something like git remote origin set-url git@github_pro:foo/bar.git
) or adapt them directly when cloning them.
git clone git@github.com:ArnaudRinquin/atom-zentabs.git
using alias, it become:
git clone git@github_pro:ArnaudRinquin/atom-zentabs.git
Step 2. Changing git user.email
Git config settings can be global or per project. In our case, we want a per project settings. It is very easy to change it:
git config user.email 'arnaud.rinquin@wopata.com'
While this is easy, it takes way to long for the developers we are. We can write a very simple git alias for that.
We are going to add it to the ~/.gitconfig
file.
[user]
name = Arnaud Rinquin
email = rinquin.arnaud@gmail.com
...
[alias]
setpromail = "config user.email 'arnaud.rinquin@wopata.com'"
Then, all we have to do is git setpromail
to have our email changed for this project only.
Step 3. One command switch please?!
Wouldn’t it be nice to switch from default account to a specified one with a single parameter-less command? This is definitely possible. This command will have two steps:
- change current project remotes to the chosen aliases
- change current project user.email config
We already have a one command solution for the second step, but the first one is way harder.
One command remote host change
Here comes the solution in the form of another git alias command to add to your ~/.gitconfig
:
[alias]
changeremotehost = !sh -c "git remote -v | grep '$1.*fetch' | sed s/..fetch.// | sed s/$1/$2/ | xargs git remote set-url"
This allows changing all remotes from one host to another (the alias). See the example:
$ > git remote -v
origin git@github.com:ArnaudRinquin/arnaudrinquin.github.io.git (fetch)
origin git@github.com:ArnaudRinquin/arnaudrinquin.github.io.git (push)
$ > git changeremotehost github.com github_pro
$ > git remote -v
origin git@github_pro:ArnaudRinquin/arnaudrinquin.github.io.git (fetch)
origin git@github_pro:ArnaudRinquin/arnaudrinquin.github.io.git (push)
Combine them all
We now just have to combine the two commands into one, this is quite easy. See how I also integrate bitbucket host switching.
[alias]
changeremotehost = !sh -c "git remote -v | grep '$1.*fetch' | sed s/..fetch.// | sed s/$1/$2/ | xargs git remote set-url"
setpromail = "config user.email 'arnaud.rinquin@wopata.com'"
gopro = !sh -c "git changeremotehost github.com github_pro && git changeremotehost bitbucket.com bitbucket_pro && git setpromail"
Source Link -Tutorial
This is brilliant, thanks. I work with a bunch of repos per email so mysetpromail
alias does aconfig --global
instead (and I have other set aliases for setting different email addresses). It works!
– michel-slm
Jun 8 '16 at 17:08
1
For those 2 different accounts how can I sign with different gpg keys ? I have 2x gpg keys for 2x github accounts and would like to sign differently. "git config --global user.signingkey xxxx"
– hakkican
Apr 6 at 8:09
This answer is awesome. I nearly didn't read it because I thought I found what from the other answer. Definitely deserves more upvotes. PS. It is even better when combined withuseConfigOnly = true
from the other answer.
– Steiny
Jun 14 at 21:13
add a comment |
up vote
23
down vote
One command github accounts switch
This solution takes the form of a single git alias. Once executed, the current project user will be attached to another account
Generate ssh keys
ssh-keygen -t rsa -C "rinquin.arnaud@gmail.com" -f '/Users/arnaudrinquin/.ssh/id_rsa'
[...]
ssh-keygen -t rsa -C "arnaud.rinquin@wopata.com" -f '/Users/arnaudrinquin/.ssh/id_rsa_pro'
Link them to your GitHub / Bitbucket accounts
- copy default public key
pbcopy < ~/.ssh/id_rsa.pub
- login to your GitHub acount
- paste the key in the
add SSH key
github page - copy other public key
pbcopy < ~/.ssh/id_rsa_pro.pub
- repeat and adapt steps 2 to 4 for every other account
Step 1. Automatic ssh key switching.
We can configure ssh
to send a use a specific encryption key depending on the host
. The nice thing is that you can have several aliases for the same hostname
.
See this example ~/.ssh/config
file:
# Default GitHub
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa
# Professional github alias
Host github_pro
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_pro
git remote configuration
You can now use these aliases in the git remotes by changing git@github.com
by git@github_pro
.
You can either change your existing projects remotes (using something like git remote origin set-url git@github_pro:foo/bar.git
) or adapt them directly when cloning them.
git clone git@github.com:ArnaudRinquin/atom-zentabs.git
using alias, it become:
git clone git@github_pro:ArnaudRinquin/atom-zentabs.git
Step 2. Changing git user.email
Git config settings can be global or per project. In our case, we want a per project settings. It is very easy to change it:
git config user.email 'arnaud.rinquin@wopata.com'
While this is easy, it takes way to long for the developers we are. We can write a very simple git alias for that.
We are going to add it to the ~/.gitconfig
file.
[user]
name = Arnaud Rinquin
email = rinquin.arnaud@gmail.com
...
[alias]
setpromail = "config user.email 'arnaud.rinquin@wopata.com'"
Then, all we have to do is git setpromail
to have our email changed for this project only.
Step 3. One command switch please?!
Wouldn’t it be nice to switch from default account to a specified one with a single parameter-less command? This is definitely possible. This command will have two steps:
- change current project remotes to the chosen aliases
- change current project user.email config
We already have a one command solution for the second step, but the first one is way harder.
One command remote host change
Here comes the solution in the form of another git alias command to add to your ~/.gitconfig
:
[alias]
changeremotehost = !sh -c "git remote -v | grep '$1.*fetch' | sed s/..fetch.// | sed s/$1/$2/ | xargs git remote set-url"
This allows changing all remotes from one host to another (the alias). See the example:
$ > git remote -v
origin git@github.com:ArnaudRinquin/arnaudrinquin.github.io.git (fetch)
origin git@github.com:ArnaudRinquin/arnaudrinquin.github.io.git (push)
$ > git changeremotehost github.com github_pro
$ > git remote -v
origin git@github_pro:ArnaudRinquin/arnaudrinquin.github.io.git (fetch)
origin git@github_pro:ArnaudRinquin/arnaudrinquin.github.io.git (push)
Combine them all
We now just have to combine the two commands into one, this is quite easy. See how I also integrate bitbucket host switching.
[alias]
changeremotehost = !sh -c "git remote -v | grep '$1.*fetch' | sed s/..fetch.// | sed s/$1/$2/ | xargs git remote set-url"
setpromail = "config user.email 'arnaud.rinquin@wopata.com'"
gopro = !sh -c "git changeremotehost github.com github_pro && git changeremotehost bitbucket.com bitbucket_pro && git setpromail"
Source Link -Tutorial
This is brilliant, thanks. I work with a bunch of repos per email so mysetpromail
alias does aconfig --global
instead (and I have other set aliases for setting different email addresses). It works!
– michel-slm
Jun 8 '16 at 17:08
1
For those 2 different accounts how can I sign with different gpg keys ? I have 2x gpg keys for 2x github accounts and would like to sign differently. "git config --global user.signingkey xxxx"
– hakkican
Apr 6 at 8:09
This answer is awesome. I nearly didn't read it because I thought I found what from the other answer. Definitely deserves more upvotes. PS. It is even better when combined withuseConfigOnly = true
from the other answer.
– Steiny
Jun 14 at 21:13
add a comment |
up vote
23
down vote
up vote
23
down vote
One command github accounts switch
This solution takes the form of a single git alias. Once executed, the current project user will be attached to another account
Generate ssh keys
ssh-keygen -t rsa -C "rinquin.arnaud@gmail.com" -f '/Users/arnaudrinquin/.ssh/id_rsa'
[...]
ssh-keygen -t rsa -C "arnaud.rinquin@wopata.com" -f '/Users/arnaudrinquin/.ssh/id_rsa_pro'
Link them to your GitHub / Bitbucket accounts
- copy default public key
pbcopy < ~/.ssh/id_rsa.pub
- login to your GitHub acount
- paste the key in the
add SSH key
github page - copy other public key
pbcopy < ~/.ssh/id_rsa_pro.pub
- repeat and adapt steps 2 to 4 for every other account
Step 1. Automatic ssh key switching.
We can configure ssh
to send a use a specific encryption key depending on the host
. The nice thing is that you can have several aliases for the same hostname
.
See this example ~/.ssh/config
file:
# Default GitHub
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa
# Professional github alias
Host github_pro
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_pro
git remote configuration
You can now use these aliases in the git remotes by changing git@github.com
by git@github_pro
.
You can either change your existing projects remotes (using something like git remote origin set-url git@github_pro:foo/bar.git
) or adapt them directly when cloning them.
git clone git@github.com:ArnaudRinquin/atom-zentabs.git
using alias, it become:
git clone git@github_pro:ArnaudRinquin/atom-zentabs.git
Step 2. Changing git user.email
Git config settings can be global or per project. In our case, we want a per project settings. It is very easy to change it:
git config user.email 'arnaud.rinquin@wopata.com'
While this is easy, it takes way to long for the developers we are. We can write a very simple git alias for that.
We are going to add it to the ~/.gitconfig
file.
[user]
name = Arnaud Rinquin
email = rinquin.arnaud@gmail.com
...
[alias]
setpromail = "config user.email 'arnaud.rinquin@wopata.com'"
Then, all we have to do is git setpromail
to have our email changed for this project only.
Step 3. One command switch please?!
Wouldn’t it be nice to switch from default account to a specified one with a single parameter-less command? This is definitely possible. This command will have two steps:
- change current project remotes to the chosen aliases
- change current project user.email config
We already have a one command solution for the second step, but the first one is way harder.
One command remote host change
Here comes the solution in the form of another git alias command to add to your ~/.gitconfig
:
[alias]
changeremotehost = !sh -c "git remote -v | grep '$1.*fetch' | sed s/..fetch.// | sed s/$1/$2/ | xargs git remote set-url"
This allows changing all remotes from one host to another (the alias). See the example:
$ > git remote -v
origin git@github.com:ArnaudRinquin/arnaudrinquin.github.io.git (fetch)
origin git@github.com:ArnaudRinquin/arnaudrinquin.github.io.git (push)
$ > git changeremotehost github.com github_pro
$ > git remote -v
origin git@github_pro:ArnaudRinquin/arnaudrinquin.github.io.git (fetch)
origin git@github_pro:ArnaudRinquin/arnaudrinquin.github.io.git (push)
Combine them all
We now just have to combine the two commands into one, this is quite easy. See how I also integrate bitbucket host switching.
[alias]
changeremotehost = !sh -c "git remote -v | grep '$1.*fetch' | sed s/..fetch.// | sed s/$1/$2/ | xargs git remote set-url"
setpromail = "config user.email 'arnaud.rinquin@wopata.com'"
gopro = !sh -c "git changeremotehost github.com github_pro && git changeremotehost bitbucket.com bitbucket_pro && git setpromail"
Source Link -Tutorial
One command github accounts switch
This solution takes the form of a single git alias. Once executed, the current project user will be attached to another account
Generate ssh keys
ssh-keygen -t rsa -C "rinquin.arnaud@gmail.com" -f '/Users/arnaudrinquin/.ssh/id_rsa'
[...]
ssh-keygen -t rsa -C "arnaud.rinquin@wopata.com" -f '/Users/arnaudrinquin/.ssh/id_rsa_pro'
Link them to your GitHub / Bitbucket accounts
- copy default public key
pbcopy < ~/.ssh/id_rsa.pub
- login to your GitHub acount
- paste the key in the
add SSH key
github page - copy other public key
pbcopy < ~/.ssh/id_rsa_pro.pub
- repeat and adapt steps 2 to 4 for every other account
Step 1. Automatic ssh key switching.
We can configure ssh
to send a use a specific encryption key depending on the host
. The nice thing is that you can have several aliases for the same hostname
.
See this example ~/.ssh/config
file:
# Default GitHub
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa
# Professional github alias
Host github_pro
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_pro
git remote configuration
You can now use these aliases in the git remotes by changing git@github.com
by git@github_pro
.
You can either change your existing projects remotes (using something like git remote origin set-url git@github_pro:foo/bar.git
) or adapt them directly when cloning them.
git clone git@github.com:ArnaudRinquin/atom-zentabs.git
using alias, it become:
git clone git@github_pro:ArnaudRinquin/atom-zentabs.git
Step 2. Changing git user.email
Git config settings can be global or per project. In our case, we want a per project settings. It is very easy to change it:
git config user.email 'arnaud.rinquin@wopata.com'
While this is easy, it takes way to long for the developers we are. We can write a very simple git alias for that.
We are going to add it to the ~/.gitconfig
file.
[user]
name = Arnaud Rinquin
email = rinquin.arnaud@gmail.com
...
[alias]
setpromail = "config user.email 'arnaud.rinquin@wopata.com'"
Then, all we have to do is git setpromail
to have our email changed for this project only.
Step 3. One command switch please?!
Wouldn’t it be nice to switch from default account to a specified one with a single parameter-less command? This is definitely possible. This command will have two steps:
- change current project remotes to the chosen aliases
- change current project user.email config
We already have a one command solution for the second step, but the first one is way harder.
One command remote host change
Here comes the solution in the form of another git alias command to add to your ~/.gitconfig
:
[alias]
changeremotehost = !sh -c "git remote -v | grep '$1.*fetch' | sed s/..fetch.// | sed s/$1/$2/ | xargs git remote set-url"
This allows changing all remotes from one host to another (the alias). See the example:
$ > git remote -v
origin git@github.com:ArnaudRinquin/arnaudrinquin.github.io.git (fetch)
origin git@github.com:ArnaudRinquin/arnaudrinquin.github.io.git (push)
$ > git changeremotehost github.com github_pro
$ > git remote -v
origin git@github_pro:ArnaudRinquin/arnaudrinquin.github.io.git (fetch)
origin git@github_pro:ArnaudRinquin/arnaudrinquin.github.io.git (push)
Combine them all
We now just have to combine the two commands into one, this is quite easy. See how I also integrate bitbucket host switching.
[alias]
changeremotehost = !sh -c "git remote -v | grep '$1.*fetch' | sed s/..fetch.// | sed s/$1/$2/ | xargs git remote set-url"
setpromail = "config user.email 'arnaud.rinquin@wopata.com'"
gopro = !sh -c "git changeremotehost github.com github_pro && git changeremotehost bitbucket.com bitbucket_pro && git setpromail"
Source Link -Tutorial
answered Oct 12 '15 at 10:37
Kaleem Ullah
3,12012733
3,12012733
This is brilliant, thanks. I work with a bunch of repos per email so mysetpromail
alias does aconfig --global
instead (and I have other set aliases for setting different email addresses). It works!
– michel-slm
Jun 8 '16 at 17:08
1
For those 2 different accounts how can I sign with different gpg keys ? I have 2x gpg keys for 2x github accounts and would like to sign differently. "git config --global user.signingkey xxxx"
– hakkican
Apr 6 at 8:09
This answer is awesome. I nearly didn't read it because I thought I found what from the other answer. Definitely deserves more upvotes. PS. It is even better when combined withuseConfigOnly = true
from the other answer.
– Steiny
Jun 14 at 21:13
add a comment |
This is brilliant, thanks. I work with a bunch of repos per email so mysetpromail
alias does aconfig --global
instead (and I have other set aliases for setting different email addresses). It works!
– michel-slm
Jun 8 '16 at 17:08
1
For those 2 different accounts how can I sign with different gpg keys ? I have 2x gpg keys for 2x github accounts and would like to sign differently. "git config --global user.signingkey xxxx"
– hakkican
Apr 6 at 8:09
This answer is awesome. I nearly didn't read it because I thought I found what from the other answer. Definitely deserves more upvotes. PS. It is even better when combined withuseConfigOnly = true
from the other answer.
– Steiny
Jun 14 at 21:13
This is brilliant, thanks. I work with a bunch of repos per email so my
setpromail
alias does a config --global
instead (and I have other set aliases for setting different email addresses). It works!– michel-slm
Jun 8 '16 at 17:08
This is brilliant, thanks. I work with a bunch of repos per email so my
setpromail
alias does a config --global
instead (and I have other set aliases for setting different email addresses). It works!– michel-slm
Jun 8 '16 at 17:08
1
1
For those 2 different accounts how can I sign with different gpg keys ? I have 2x gpg keys for 2x github accounts and would like to sign differently. "git config --global user.signingkey xxxx"
– hakkican
Apr 6 at 8:09
For those 2 different accounts how can I sign with different gpg keys ? I have 2x gpg keys for 2x github accounts and would like to sign differently. "git config --global user.signingkey xxxx"
– hakkican
Apr 6 at 8:09
This answer is awesome. I nearly didn't read it because I thought I found what from the other answer. Definitely deserves more upvotes. PS. It is even better when combined with
useConfigOnly = true
from the other answer.– Steiny
Jun 14 at 21:13
This answer is awesome. I nearly didn't read it because I thought I found what from the other answer. Definitely deserves more upvotes. PS. It is even better when combined with
useConfigOnly = true
from the other answer.– Steiny
Jun 14 at 21:13
add a comment |
up vote
23
down vote
If you do not want to have a default email address (email address links to a github user), you can configure that you want to be asked. How you can do that depends on the version of git you use, see below.
The (intended) drawback is that you have to configure your email address (and your name) once for every repository. So, you cannot forget to do it.
Version < 2.7.0
[user]
name = Your name
email = "(none)"
in your global configuration ~/.gitconfig
as stated in a comment by Dan Aloni in Orr Sella's blog post. When trying to do the first commit in a repository, git fails with the nice message:
*** Please tell me who you are.
Run
git config --global user.email "you@example.com"
git config --global user.name "Your Name"
to set your account's default identity.
Omit --global to set the identity only in this repository.
fatal: unable to auto-detect email address (got '(none)')
The name is taken from the global config when the email address is set locally (the message is not perfectly accurate).
2.7.0 ≤ Version < 2.8.0
The behaviour in versions < 2.7.0 was not intended and fixed with 2.7.0. You can still use a pre-commit hook as described in Orr Sella's blog post. This solution works also for other versions, but the other solutions not for this version.
Version ≥ 2.8.0
Dan Aloni added an option to achieve that behaviour (see release notes). Use it with:
[user]
useConfigOnly = true
To make it work you may not give a name or email address in the global config. Then, at the first commit, you get an error message
fatal: user.useConfigOnly set but no name given
So the message is not very instructive, but since you set the option explicitly, you should know what to do. In contrast to the solution of versions < 2.7.0, you always have to set both name and email manually.
I use this method for a few months, it worked great. I have multiple email addresses (personal, work) and I do not want to have a "default" one configured in global gitconfig. With special value "(none)" git will prompt me to provide a valid address every time I spin up a new repo, instead of guessing one based on username and hostname (which is annoying and I need to --amend it). However, recently with upgraded version of Git (2.7.0, maybe earlier), I find out that special value "(none)" does not trigger fatal errors any more. Instead it will just use "John Doe <(none)>" as is ...
– Zhuoyun Wei
Feb 2 '16 at 13:11
@wzyboy: Oh, you are right. I usedgit bisect
to find that commit 19ce497c... introduced this behaviour. However, independent of the version (2.5 - 2.7) I can useemail =
(with no argument) in the config and it shows the same behaviour asemail = "(none)"
in old versions. Can you confirm this? If so, I'll edit my answer. I am just sceptical as it seems so obvious and I did not use it before.
– John
Feb 3 '16 at 17:17
I triedemail =
in 2.7.0, Git still guesses email address based on username and hostname. I now usepre-commit
approach in Sella's blog. I also notified the Dan Aloni who came up with the"(none)"
idea in Sella's post and he filed up a patch to formally implement this as a feature: permalink.gmane.org/gmane.comp.version-control.git/285301
– Zhuoyun Wei
Feb 4 '16 at 3:25
3
since git-2.8: The "user.useConfigOnly" configuration variable can be used to force the user to always set user.email & user.name github.com/git/git/blob/master/Documentation/RelNotes/2.8.0.txt
– rofrol
Mar 29 '16 at 13:33
@rofrol Thanks for the info. I updated my answer accordingly.
– John
Mar 29 '16 at 16:12
add a comment |
up vote
23
down vote
If you do not want to have a default email address (email address links to a github user), you can configure that you want to be asked. How you can do that depends on the version of git you use, see below.
The (intended) drawback is that you have to configure your email address (and your name) once for every repository. So, you cannot forget to do it.
Version < 2.7.0
[user]
name = Your name
email = "(none)"
in your global configuration ~/.gitconfig
as stated in a comment by Dan Aloni in Orr Sella's blog post. When trying to do the first commit in a repository, git fails with the nice message:
*** Please tell me who you are.
Run
git config --global user.email "you@example.com"
git config --global user.name "Your Name"
to set your account's default identity.
Omit --global to set the identity only in this repository.
fatal: unable to auto-detect email address (got '(none)')
The name is taken from the global config when the email address is set locally (the message is not perfectly accurate).
2.7.0 ≤ Version < 2.8.0
The behaviour in versions < 2.7.0 was not intended and fixed with 2.7.0. You can still use a pre-commit hook as described in Orr Sella's blog post. This solution works also for other versions, but the other solutions not for this version.
Version ≥ 2.8.0
Dan Aloni added an option to achieve that behaviour (see release notes). Use it with:
[user]
useConfigOnly = true
To make it work you may not give a name or email address in the global config. Then, at the first commit, you get an error message
fatal: user.useConfigOnly set but no name given
So the message is not very instructive, but since you set the option explicitly, you should know what to do. In contrast to the solution of versions < 2.7.0, you always have to set both name and email manually.
I use this method for a few months, it worked great. I have multiple email addresses (personal, work) and I do not want to have a "default" one configured in global gitconfig. With special value "(none)" git will prompt me to provide a valid address every time I spin up a new repo, instead of guessing one based on username and hostname (which is annoying and I need to --amend it). However, recently with upgraded version of Git (2.7.0, maybe earlier), I find out that special value "(none)" does not trigger fatal errors any more. Instead it will just use "John Doe <(none)>" as is ...
– Zhuoyun Wei
Feb 2 '16 at 13:11
@wzyboy: Oh, you are right. I usedgit bisect
to find that commit 19ce497c... introduced this behaviour. However, independent of the version (2.5 - 2.7) I can useemail =
(with no argument) in the config and it shows the same behaviour asemail = "(none)"
in old versions. Can you confirm this? If so, I'll edit my answer. I am just sceptical as it seems so obvious and I did not use it before.
– John
Feb 3 '16 at 17:17
I triedemail =
in 2.7.0, Git still guesses email address based on username and hostname. I now usepre-commit
approach in Sella's blog. I also notified the Dan Aloni who came up with the"(none)"
idea in Sella's post and he filed up a patch to formally implement this as a feature: permalink.gmane.org/gmane.comp.version-control.git/285301
– Zhuoyun Wei
Feb 4 '16 at 3:25
3
since git-2.8: The "user.useConfigOnly" configuration variable can be used to force the user to always set user.email & user.name github.com/git/git/blob/master/Documentation/RelNotes/2.8.0.txt
– rofrol
Mar 29 '16 at 13:33
@rofrol Thanks for the info. I updated my answer accordingly.
– John
Mar 29 '16 at 16:12
add a comment |
up vote
23
down vote
up vote
23
down vote
If you do not want to have a default email address (email address links to a github user), you can configure that you want to be asked. How you can do that depends on the version of git you use, see below.
The (intended) drawback is that you have to configure your email address (and your name) once for every repository. So, you cannot forget to do it.
Version < 2.7.0
[user]
name = Your name
email = "(none)"
in your global configuration ~/.gitconfig
as stated in a comment by Dan Aloni in Orr Sella's blog post. When trying to do the first commit in a repository, git fails with the nice message:
*** Please tell me who you are.
Run
git config --global user.email "you@example.com"
git config --global user.name "Your Name"
to set your account's default identity.
Omit --global to set the identity only in this repository.
fatal: unable to auto-detect email address (got '(none)')
The name is taken from the global config when the email address is set locally (the message is not perfectly accurate).
2.7.0 ≤ Version < 2.8.0
The behaviour in versions < 2.7.0 was not intended and fixed with 2.7.0. You can still use a pre-commit hook as described in Orr Sella's blog post. This solution works also for other versions, but the other solutions not for this version.
Version ≥ 2.8.0
Dan Aloni added an option to achieve that behaviour (see release notes). Use it with:
[user]
useConfigOnly = true
To make it work you may not give a name or email address in the global config. Then, at the first commit, you get an error message
fatal: user.useConfigOnly set but no name given
So the message is not very instructive, but since you set the option explicitly, you should know what to do. In contrast to the solution of versions < 2.7.0, you always have to set both name and email manually.
If you do not want to have a default email address (email address links to a github user), you can configure that you want to be asked. How you can do that depends on the version of git you use, see below.
The (intended) drawback is that you have to configure your email address (and your name) once for every repository. So, you cannot forget to do it.
Version < 2.7.0
[user]
name = Your name
email = "(none)"
in your global configuration ~/.gitconfig
as stated in a comment by Dan Aloni in Orr Sella's blog post. When trying to do the first commit in a repository, git fails with the nice message:
*** Please tell me who you are.
Run
git config --global user.email "you@example.com"
git config --global user.name "Your Name"
to set your account's default identity.
Omit --global to set the identity only in this repository.
fatal: unable to auto-detect email address (got '(none)')
The name is taken from the global config when the email address is set locally (the message is not perfectly accurate).
2.7.0 ≤ Version < 2.8.0
The behaviour in versions < 2.7.0 was not intended and fixed with 2.7.0. You can still use a pre-commit hook as described in Orr Sella's blog post. This solution works also for other versions, but the other solutions not for this version.
Version ≥ 2.8.0
Dan Aloni added an option to achieve that behaviour (see release notes). Use it with:
[user]
useConfigOnly = true
To make it work you may not give a name or email address in the global config. Then, at the first commit, you get an error message
fatal: user.useConfigOnly set but no name given
So the message is not very instructive, but since you set the option explicitly, you should know what to do. In contrast to the solution of versions < 2.7.0, you always have to set both name and email manually.
edited Mar 29 '16 at 16:09
answered Aug 20 '15 at 17:03
John
559512
559512
I use this method for a few months, it worked great. I have multiple email addresses (personal, work) and I do not want to have a "default" one configured in global gitconfig. With special value "(none)" git will prompt me to provide a valid address every time I spin up a new repo, instead of guessing one based on username and hostname (which is annoying and I need to --amend it). However, recently with upgraded version of Git (2.7.0, maybe earlier), I find out that special value "(none)" does not trigger fatal errors any more. Instead it will just use "John Doe <(none)>" as is ...
– Zhuoyun Wei
Feb 2 '16 at 13:11
@wzyboy: Oh, you are right. I usedgit bisect
to find that commit 19ce497c... introduced this behaviour. However, independent of the version (2.5 - 2.7) I can useemail =
(with no argument) in the config and it shows the same behaviour asemail = "(none)"
in old versions. Can you confirm this? If so, I'll edit my answer. I am just sceptical as it seems so obvious and I did not use it before.
– John
Feb 3 '16 at 17:17
I triedemail =
in 2.7.0, Git still guesses email address based on username and hostname. I now usepre-commit
approach in Sella's blog. I also notified the Dan Aloni who came up with the"(none)"
idea in Sella's post and he filed up a patch to formally implement this as a feature: permalink.gmane.org/gmane.comp.version-control.git/285301
– Zhuoyun Wei
Feb 4 '16 at 3:25
3
since git-2.8: The "user.useConfigOnly" configuration variable can be used to force the user to always set user.email & user.name github.com/git/git/blob/master/Documentation/RelNotes/2.8.0.txt
– rofrol
Mar 29 '16 at 13:33
@rofrol Thanks for the info. I updated my answer accordingly.
– John
Mar 29 '16 at 16:12
add a comment |
I use this method for a few months, it worked great. I have multiple email addresses (personal, work) and I do not want to have a "default" one configured in global gitconfig. With special value "(none)" git will prompt me to provide a valid address every time I spin up a new repo, instead of guessing one based on username and hostname (which is annoying and I need to --amend it). However, recently with upgraded version of Git (2.7.0, maybe earlier), I find out that special value "(none)" does not trigger fatal errors any more. Instead it will just use "John Doe <(none)>" as is ...
– Zhuoyun Wei
Feb 2 '16 at 13:11
@wzyboy: Oh, you are right. I usedgit bisect
to find that commit 19ce497c... introduced this behaviour. However, independent of the version (2.5 - 2.7) I can useemail =
(with no argument) in the config and it shows the same behaviour asemail = "(none)"
in old versions. Can you confirm this? If so, I'll edit my answer. I am just sceptical as it seems so obvious and I did not use it before.
– John
Feb 3 '16 at 17:17
I triedemail =
in 2.7.0, Git still guesses email address based on username and hostname. I now usepre-commit
approach in Sella's blog. I also notified the Dan Aloni who came up with the"(none)"
idea in Sella's post and he filed up a patch to formally implement this as a feature: permalink.gmane.org/gmane.comp.version-control.git/285301
– Zhuoyun Wei
Feb 4 '16 at 3:25
3
since git-2.8: The "user.useConfigOnly" configuration variable can be used to force the user to always set user.email & user.name github.com/git/git/blob/master/Documentation/RelNotes/2.8.0.txt
– rofrol
Mar 29 '16 at 13:33
@rofrol Thanks for the info. I updated my answer accordingly.
– John
Mar 29 '16 at 16:12
I use this method for a few months, it worked great. I have multiple email addresses (personal, work) and I do not want to have a "default" one configured in global gitconfig. With special value "(none)" git will prompt me to provide a valid address every time I spin up a new repo, instead of guessing one based on username and hostname (which is annoying and I need to --amend it). However, recently with upgraded version of Git (2.7.0, maybe earlier), I find out that special value "(none)" does not trigger fatal errors any more. Instead it will just use "John Doe <(none)>" as is ...
– Zhuoyun Wei
Feb 2 '16 at 13:11
I use this method for a few months, it worked great. I have multiple email addresses (personal, work) and I do not want to have a "default" one configured in global gitconfig. With special value "(none)" git will prompt me to provide a valid address every time I spin up a new repo, instead of guessing one based on username and hostname (which is annoying and I need to --amend it). However, recently with upgraded version of Git (2.7.0, maybe earlier), I find out that special value "(none)" does not trigger fatal errors any more. Instead it will just use "John Doe <(none)>" as is ...
– Zhuoyun Wei
Feb 2 '16 at 13:11
@wzyboy: Oh, you are right. I used
git bisect
to find that commit 19ce497c... introduced this behaviour. However, independent of the version (2.5 - 2.7) I can use email =
(with no argument) in the config and it shows the same behaviour as email = "(none)"
in old versions. Can you confirm this? If so, I'll edit my answer. I am just sceptical as it seems so obvious and I did not use it before.– John
Feb 3 '16 at 17:17
@wzyboy: Oh, you are right. I used
git bisect
to find that commit 19ce497c... introduced this behaviour. However, independent of the version (2.5 - 2.7) I can use email =
(with no argument) in the config and it shows the same behaviour as email = "(none)"
in old versions. Can you confirm this? If so, I'll edit my answer. I am just sceptical as it seems so obvious and I did not use it before.– John
Feb 3 '16 at 17:17
I tried
email =
in 2.7.0, Git still guesses email address based on username and hostname. I now use pre-commit
approach in Sella's blog. I also notified the Dan Aloni who came up with the "(none)"
idea in Sella's post and he filed up a patch to formally implement this as a feature: permalink.gmane.org/gmane.comp.version-control.git/285301– Zhuoyun Wei
Feb 4 '16 at 3:25
I tried
email =
in 2.7.0, Git still guesses email address based on username and hostname. I now use pre-commit
approach in Sella's blog. I also notified the Dan Aloni who came up with the "(none)"
idea in Sella's post and he filed up a patch to formally implement this as a feature: permalink.gmane.org/gmane.comp.version-control.git/285301– Zhuoyun Wei
Feb 4 '16 at 3:25
3
3
since git-2.8: The "user.useConfigOnly" configuration variable can be used to force the user to always set user.email & user.name github.com/git/git/blob/master/Documentation/RelNotes/2.8.0.txt
– rofrol
Mar 29 '16 at 13:33
since git-2.8: The "user.useConfigOnly" configuration variable can be used to force the user to always set user.email & user.name github.com/git/git/blob/master/Documentation/RelNotes/2.8.0.txt
– rofrol
Mar 29 '16 at 13:33
@rofrol Thanks for the info. I updated my answer accordingly.
– John
Mar 29 '16 at 16:12
@rofrol Thanks for the info. I updated my answer accordingly.
– John
Mar 29 '16 at 16:12
add a comment |
up vote
16
down vote
Another option to get git
to work with multiple names / emails is by aliasing git
and using the -c
flag to override the global and repository-specific config.
For example, by defining an alias:
alias git='/usr/bin/git -c user.name="Your name" -c user.email="name@example.com"'
To see whether it works, simply type git config user.email
:
$ git config user.email
name@example.com
Instead of an alias, you could also put a custom git
executable within your $PATH
.
#!/bin/sh
/usr/bin/git -c user.name="Your name" -c user.email="name@example.com" "$@"
An advantage of these method over a repository-specific .git/config
is that it applies to every git
repository when the custom git
program is active. In this way, you can easily switch between users/names without modifying any (shared) configuration.
add a comment |
up vote
16
down vote
Another option to get git
to work with multiple names / emails is by aliasing git
and using the -c
flag to override the global and repository-specific config.
For example, by defining an alias:
alias git='/usr/bin/git -c user.name="Your name" -c user.email="name@example.com"'
To see whether it works, simply type git config user.email
:
$ git config user.email
name@example.com
Instead of an alias, you could also put a custom git
executable within your $PATH
.
#!/bin/sh
/usr/bin/git -c user.name="Your name" -c user.email="name@example.com" "$@"
An advantage of these method over a repository-specific .git/config
is that it applies to every git
repository when the custom git
program is active. In this way, you can easily switch between users/names without modifying any (shared) configuration.
add a comment |
up vote
16
down vote
up vote
16
down vote
Another option to get git
to work with multiple names / emails is by aliasing git
and using the -c
flag to override the global and repository-specific config.
For example, by defining an alias:
alias git='/usr/bin/git -c user.name="Your name" -c user.email="name@example.com"'
To see whether it works, simply type git config user.email
:
$ git config user.email
name@example.com
Instead of an alias, you could also put a custom git
executable within your $PATH
.
#!/bin/sh
/usr/bin/git -c user.name="Your name" -c user.email="name@example.com" "$@"
An advantage of these method over a repository-specific .git/config
is that it applies to every git
repository when the custom git
program is active. In this way, you can easily switch between users/names without modifying any (shared) configuration.
Another option to get git
to work with multiple names / emails is by aliasing git
and using the -c
flag to override the global and repository-specific config.
For example, by defining an alias:
alias git='/usr/bin/git -c user.name="Your name" -c user.email="name@example.com"'
To see whether it works, simply type git config user.email
:
$ git config user.email
name@example.com
Instead of an alias, you could also put a custom git
executable within your $PATH
.
#!/bin/sh
/usr/bin/git -c user.name="Your name" -c user.email="name@example.com" "$@"
An advantage of these method over a repository-specific .git/config
is that it applies to every git
repository when the custom git
program is active. In this way, you can easily switch between users/names without modifying any (shared) configuration.
answered Oct 2 '14 at 21:41
Rob W
265k49629564
265k49629564
add a comment |
add a comment |
up vote
8
down vote
There is a simple solution that seems to work well for avoiding mistakes.
Simply remove the [user]
section from your ~/.gitconfig
, which will prevent you from making any commits without setting user.name
for each repository.
In your ~/.bashrc
, add some simple aliases for the user and email:
alias ggmail='git config user.name "My Name";git config user.email me@gmail.com'
alias gwork='git config user.name "My Name";git config user.email me@work.job'
add a comment |
up vote
8
down vote
There is a simple solution that seems to work well for avoiding mistakes.
Simply remove the [user]
section from your ~/.gitconfig
, which will prevent you from making any commits without setting user.name
for each repository.
In your ~/.bashrc
, add some simple aliases for the user and email:
alias ggmail='git config user.name "My Name";git config user.email me@gmail.com'
alias gwork='git config user.name "My Name";git config user.email me@work.job'
add a comment |
up vote
8
down vote
up vote
8
down vote
There is a simple solution that seems to work well for avoiding mistakes.
Simply remove the [user]
section from your ~/.gitconfig
, which will prevent you from making any commits without setting user.name
for each repository.
In your ~/.bashrc
, add some simple aliases for the user and email:
alias ggmail='git config user.name "My Name";git config user.email me@gmail.com'
alias gwork='git config user.name "My Name";git config user.email me@work.job'
There is a simple solution that seems to work well for avoiding mistakes.
Simply remove the [user]
section from your ~/.gitconfig
, which will prevent you from making any commits without setting user.name
for each repository.
In your ~/.bashrc
, add some simple aliases for the user and email:
alias ggmail='git config user.name "My Name";git config user.email me@gmail.com'
alias gwork='git config user.name "My Name";git config user.email me@work.job'
answered Mar 9 '16 at 15:56
Zantier
422415
422415
add a comment |
add a comment |
up vote
8
down vote
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.gitconfig
has my work name and email. Both files are at ~
path.
So my personal name/email applies by default. For c:/work/
dir, my work name/email is applied. For c:/work/github/
dir, my personal name/email is applied. This works as the last setting gets applied.
# ~/.gitconfig
[include]
path = user.gitconfig
[includeIf "gitdir/i:c:/work/"]
path = work-user.gitconfig
[includeIf "gitdir/i:c:/work/github/"]
path = user.gitconfig
gitdir
is case-sensitive and gitdir/i
is case-insensitive.
"gitdir/i:github/"
would apply the conditional include for any directory with github
in its path.
While your answer is appreciated and good Tomáš Janoušek gave the same 20 days before you. Please consider deleting this answer.
– Hedge
Sep 15 '17 at 17:03
3
@Hedge Yes, I upvoted his answer, but it took me a while to configure on windows and that's wheregitdir/i
helped me out (which his answer does not mention).
– hIpPy
Sep 15 '17 at 21:44
add a comment |
up vote
8
down vote
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.gitconfig
has my work name and email. Both files are at ~
path.
So my personal name/email applies by default. For c:/work/
dir, my work name/email is applied. For c:/work/github/
dir, my personal name/email is applied. This works as the last setting gets applied.
# ~/.gitconfig
[include]
path = user.gitconfig
[includeIf "gitdir/i:c:/work/"]
path = work-user.gitconfig
[includeIf "gitdir/i:c:/work/github/"]
path = user.gitconfig
gitdir
is case-sensitive and gitdir/i
is case-insensitive.
"gitdir/i:github/"
would apply the conditional include for any directory with github
in its path.
While your answer is appreciated and good Tomáš Janoušek gave the same 20 days before you. Please consider deleting this answer.
– Hedge
Sep 15 '17 at 17:03
3
@Hedge Yes, I upvoted his answer, but it took me a while to configure on windows and that's wheregitdir/i
helped me out (which his answer does not mention).
– hIpPy
Sep 15 '17 at 21:44
add a comment |
up vote
8
down vote
up vote
8
down vote
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.gitconfig
has my work name and email. Both files are at ~
path.
So my personal name/email applies by default. For c:/work/
dir, my work name/email is applied. For c:/work/github/
dir, my personal name/email is applied. This works as the last setting gets applied.
# ~/.gitconfig
[include]
path = user.gitconfig
[includeIf "gitdir/i:c:/work/"]
path = work-user.gitconfig
[includeIf "gitdir/i:c:/work/github/"]
path = user.gitconfig
gitdir
is case-sensitive and gitdir/i
is case-insensitive.
"gitdir/i:github/"
would apply the conditional include for any directory with github
in its path.
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.gitconfig
has my work name and email. Both files are at ~
path.
So my personal name/email applies by default. For c:/work/
dir, my work name/email is applied. For c:/work/github/
dir, my personal name/email is applied. This works as the last setting gets applied.
# ~/.gitconfig
[include]
path = user.gitconfig
[includeIf "gitdir/i:c:/work/"]
path = work-user.gitconfig
[includeIf "gitdir/i:c:/work/github/"]
path = user.gitconfig
gitdir
is case-sensitive and gitdir/i
is case-insensitive.
"gitdir/i:github/"
would apply the conditional include for any directory with github
in its path.
answered May 18 '17 at 0:13
hIpPy
1,77352948
1,77352948
While your answer is appreciated and good Tomáš Janoušek gave the same 20 days before you. Please consider deleting this answer.
– Hedge
Sep 15 '17 at 17:03
3
@Hedge Yes, I upvoted his answer, but it took me a while to configure on windows and that's wheregitdir/i
helped me out (which his answer does not mention).
– hIpPy
Sep 15 '17 at 21:44
add a comment |
While your answer is appreciated and good Tomáš Janoušek gave the same 20 days before you. Please consider deleting this answer.
– Hedge
Sep 15 '17 at 17:03
3
@Hedge Yes, I upvoted his answer, but it took me a while to configure on windows and that's wheregitdir/i
helped me out (which his answer does not mention).
– hIpPy
Sep 15 '17 at 21:44
While your answer is appreciated and good Tomáš Janoušek gave the same 20 days before you. Please consider deleting this answer.
– Hedge
Sep 15 '17 at 17:03
While your answer is appreciated and good Tomáš Janoušek gave the same 20 days before you. Please consider deleting this answer.
– Hedge
Sep 15 '17 at 17:03
3
3
@Hedge Yes, I upvoted his answer, but it took me a while to configure on windows and that's where
gitdir/i
helped me out (which his answer does not mention).– hIpPy
Sep 15 '17 at 21:44
@Hedge Yes, I upvoted his answer, but it took me a while to configure on windows and that's where
gitdir/i
helped me out (which his answer does not mention).– hIpPy
Sep 15 '17 at 21:44
add a comment |
up vote
7
down vote
git aliases (and sections in git configs) to the rescue!
add an alias (from command line):
git config --global alias.identity '! git config user.name $(git config user.$1.name); git config user.email $(git config user.$1.email); :'
then, set, for example
git config --global user.github.name "your github username"
git config --global user.github.email your@github.email
and in a new or cloned repo you can run this command:
git identity github
This solution isn't automatic, but unsetting user and email in your global ~/.gitconfig would force git to remind you to set them manually in each new or cloned repo.
git config --global --unset user.name
git config --global --unset user.email
I like your solution; however, when I unset my global it just makes the commit with my computer's host name rather than having git remind me to set them in any given repo :(
– ENG618
Jun 20 '17 at 14:59
add a comment |
up vote
7
down vote
git aliases (and sections in git configs) to the rescue!
add an alias (from command line):
git config --global alias.identity '! git config user.name $(git config user.$1.name); git config user.email $(git config user.$1.email); :'
then, set, for example
git config --global user.github.name "your github username"
git config --global user.github.email your@github.email
and in a new or cloned repo you can run this command:
git identity github
This solution isn't automatic, but unsetting user and email in your global ~/.gitconfig would force git to remind you to set them manually in each new or cloned repo.
git config --global --unset user.name
git config --global --unset user.email
I like your solution; however, when I unset my global it just makes the commit with my computer's host name rather than having git remind me to set them in any given repo :(
– ENG618
Jun 20 '17 at 14:59
add a comment |
up vote
7
down vote
up vote
7
down vote
git aliases (and sections in git configs) to the rescue!
add an alias (from command line):
git config --global alias.identity '! git config user.name $(git config user.$1.name); git config user.email $(git config user.$1.email); :'
then, set, for example
git config --global user.github.name "your github username"
git config --global user.github.email your@github.email
and in a new or cloned repo you can run this command:
git identity github
This solution isn't automatic, but unsetting user and email in your global ~/.gitconfig would force git to remind you to set them manually in each new or cloned repo.
git config --global --unset user.name
git config --global --unset user.email
git aliases (and sections in git configs) to the rescue!
add an alias (from command line):
git config --global alias.identity '! git config user.name $(git config user.$1.name); git config user.email $(git config user.$1.email); :'
then, set, for example
git config --global user.github.name "your github username"
git config --global user.github.email your@github.email
and in a new or cloned repo you can run this command:
git identity github
This solution isn't automatic, but unsetting user and email in your global ~/.gitconfig would force git to remind you to set them manually in each new or cloned repo.
git config --global --unset user.name
git config --global --unset user.email
answered Mar 24 '17 at 21:01
codesnik
10115
10115
I like your solution; however, when I unset my global it just makes the commit with my computer's host name rather than having git remind me to set them in any given repo :(
– ENG618
Jun 20 '17 at 14:59
add a comment |
I like your solution; however, when I unset my global it just makes the commit with my computer's host name rather than having git remind me to set them in any given repo :(
– ENG618
Jun 20 '17 at 14:59
I like your solution; however, when I unset my global it just makes the commit with my computer's host name rather than having git remind me to set them in any given repo :(
– ENG618
Jun 20 '17 at 14:59
I like your solution; however, when I unset my global it just makes the commit with my computer's host name rather than having git remind me to set them in any given repo :(
– ENG618
Jun 20 '17 at 14:59
add a comment |
up vote
6
down vote
This answer is partially inspired by the post by @Saucier, but I was looking for an automated way to set user.name
and user.email
on a per repo basis, based on the remote, that was a little more light weight than the git-passport package that he developed. Also h/t to @John for the useConfigOnly setting. Here is my solution:
.gitconfig
changes:
[github]
name = <github username>
email = <github email>
[gitlab]
name = <gitlab username>
email = <gitlab email>
[init]
templatedir = ~/.git-templates
[user]
useConfigOnly = true
post-checkout hook which should be saved to the following path: ~/.git-templates/hooks/post-checkout
:
#!/usr/bin/env bash
# make regex matching below case insensitive
shopt -s nocasematch
# values in the services array should have a corresponding section in
# .gitconfig where the 'name' and 'email' for that service are specified
remote_url="$( git config --get --local remote.origin.url )"
services=(
'github'
'gitlab'
)
set_local_user_config() {
local service="${1}"
local config="${2}"
local service_config="$( git config --get ${service}.${config} )"
local local_config="$( git config --get --local user.${config} )"
if [[ "${local_config}" != "${service_config}" ]]; then
git config --local "user.${config}" "${service_config}"
echo "repo 'user.${config}' has been set to '${service_config}'"
fi
}
# if remote_url doesn't contain the any of the values in the services
# array the user name and email will remain unset and the
# user.useConfigOnly = true setting in .gitconfig will prompt for those
# credentials and prevent commits until they are defined
for s in "${services[@]}"; do
if [[ "${remote_url}" =~ "${s}" ]]; then
set_local_user_config "${s}" 'name'
set_local_user_config "${s}" 'email'
break
fi
done
I use different credentials for github and gitlab, but those references in the code above could be replaced or augmented with any service that you use. In order to have the post-checkout hook automatically set the user name and email locally for a repo after a checkout make sure the service name appears in the remote url, add it to the services array in the post-checkout
script and create a section for it in your .gitconfig
that contains your user name and email for that service.
If none of the service names appear in the remote url or the repo doesn't have a remote the user name and email will not be set locally. In these cases the user.useConfigOnly
setting will be in play which will not allow you to make commits until the user name and email are set at the repo level, and will prompt the user to configure that information.
1
Great solution. Do not forget tochmod 755
the hook script. Otherwise, it will be copied but never executed.
– onekiloparsec
Apr 11 '17 at 15:41
add a comment |
up vote
6
down vote
This answer is partially inspired by the post by @Saucier, but I was looking for an automated way to set user.name
and user.email
on a per repo basis, based on the remote, that was a little more light weight than the git-passport package that he developed. Also h/t to @John for the useConfigOnly setting. Here is my solution:
.gitconfig
changes:
[github]
name = <github username>
email = <github email>
[gitlab]
name = <gitlab username>
email = <gitlab email>
[init]
templatedir = ~/.git-templates
[user]
useConfigOnly = true
post-checkout hook which should be saved to the following path: ~/.git-templates/hooks/post-checkout
:
#!/usr/bin/env bash
# make regex matching below case insensitive
shopt -s nocasematch
# values in the services array should have a corresponding section in
# .gitconfig where the 'name' and 'email' for that service are specified
remote_url="$( git config --get --local remote.origin.url )"
services=(
'github'
'gitlab'
)
set_local_user_config() {
local service="${1}"
local config="${2}"
local service_config="$( git config --get ${service}.${config} )"
local local_config="$( git config --get --local user.${config} )"
if [[ "${local_config}" != "${service_config}" ]]; then
git config --local "user.${config}" "${service_config}"
echo "repo 'user.${config}' has been set to '${service_config}'"
fi
}
# if remote_url doesn't contain the any of the values in the services
# array the user name and email will remain unset and the
# user.useConfigOnly = true setting in .gitconfig will prompt for those
# credentials and prevent commits until they are defined
for s in "${services[@]}"; do
if [[ "${remote_url}" =~ "${s}" ]]; then
set_local_user_config "${s}" 'name'
set_local_user_config "${s}" 'email'
break
fi
done
I use different credentials for github and gitlab, but those references in the code above could be replaced or augmented with any service that you use. In order to have the post-checkout hook automatically set the user name and email locally for a repo after a checkout make sure the service name appears in the remote url, add it to the services array in the post-checkout
script and create a section for it in your .gitconfig
that contains your user name and email for that service.
If none of the service names appear in the remote url or the repo doesn't have a remote the user name and email will not be set locally. In these cases the user.useConfigOnly
setting will be in play which will not allow you to make commits until the user name and email are set at the repo level, and will prompt the user to configure that information.
1
Great solution. Do not forget tochmod 755
the hook script. Otherwise, it will be copied but never executed.
– onekiloparsec
Apr 11 '17 at 15:41
add a comment |
up vote
6
down vote
up vote
6
down vote
This answer is partially inspired by the post by @Saucier, but I was looking for an automated way to set user.name
and user.email
on a per repo basis, based on the remote, that was a little more light weight than the git-passport package that he developed. Also h/t to @John for the useConfigOnly setting. Here is my solution:
.gitconfig
changes:
[github]
name = <github username>
email = <github email>
[gitlab]
name = <gitlab username>
email = <gitlab email>
[init]
templatedir = ~/.git-templates
[user]
useConfigOnly = true
post-checkout hook which should be saved to the following path: ~/.git-templates/hooks/post-checkout
:
#!/usr/bin/env bash
# make regex matching below case insensitive
shopt -s nocasematch
# values in the services array should have a corresponding section in
# .gitconfig where the 'name' and 'email' for that service are specified
remote_url="$( git config --get --local remote.origin.url )"
services=(
'github'
'gitlab'
)
set_local_user_config() {
local service="${1}"
local config="${2}"
local service_config="$( git config --get ${service}.${config} )"
local local_config="$( git config --get --local user.${config} )"
if [[ "${local_config}" != "${service_config}" ]]; then
git config --local "user.${config}" "${service_config}"
echo "repo 'user.${config}' has been set to '${service_config}'"
fi
}
# if remote_url doesn't contain the any of the values in the services
# array the user name and email will remain unset and the
# user.useConfigOnly = true setting in .gitconfig will prompt for those
# credentials and prevent commits until they are defined
for s in "${services[@]}"; do
if [[ "${remote_url}" =~ "${s}" ]]; then
set_local_user_config "${s}" 'name'
set_local_user_config "${s}" 'email'
break
fi
done
I use different credentials for github and gitlab, but those references in the code above could be replaced or augmented with any service that you use. In order to have the post-checkout hook automatically set the user name and email locally for a repo after a checkout make sure the service name appears in the remote url, add it to the services array in the post-checkout
script and create a section for it in your .gitconfig
that contains your user name and email for that service.
If none of the service names appear in the remote url or the repo doesn't have a remote the user name and email will not be set locally. In these cases the user.useConfigOnly
setting will be in play which will not allow you to make commits until the user name and email are set at the repo level, and will prompt the user to configure that information.
This answer is partially inspired by the post by @Saucier, but I was looking for an automated way to set user.name
and user.email
on a per repo basis, based on the remote, that was a little more light weight than the git-passport package that he developed. Also h/t to @John for the useConfigOnly setting. Here is my solution:
.gitconfig
changes:
[github]
name = <github username>
email = <github email>
[gitlab]
name = <gitlab username>
email = <gitlab email>
[init]
templatedir = ~/.git-templates
[user]
useConfigOnly = true
post-checkout hook which should be saved to the following path: ~/.git-templates/hooks/post-checkout
:
#!/usr/bin/env bash
# make regex matching below case insensitive
shopt -s nocasematch
# values in the services array should have a corresponding section in
# .gitconfig where the 'name' and 'email' for that service are specified
remote_url="$( git config --get --local remote.origin.url )"
services=(
'github'
'gitlab'
)
set_local_user_config() {
local service="${1}"
local config="${2}"
local service_config="$( git config --get ${service}.${config} )"
local local_config="$( git config --get --local user.${config} )"
if [[ "${local_config}" != "${service_config}" ]]; then
git config --local "user.${config}" "${service_config}"
echo "repo 'user.${config}' has been set to '${service_config}'"
fi
}
# if remote_url doesn't contain the any of the values in the services
# array the user name and email will remain unset and the
# user.useConfigOnly = true setting in .gitconfig will prompt for those
# credentials and prevent commits until they are defined
for s in "${services[@]}"; do
if [[ "${remote_url}" =~ "${s}" ]]; then
set_local_user_config "${s}" 'name'
set_local_user_config "${s}" 'email'
break
fi
done
I use different credentials for github and gitlab, but those references in the code above could be replaced or augmented with any service that you use. In order to have the post-checkout hook automatically set the user name and email locally for a repo after a checkout make sure the service name appears in the remote url, add it to the services array in the post-checkout
script and create a section for it in your .gitconfig
that contains your user name and email for that service.
If none of the service names appear in the remote url or the repo doesn't have a remote the user name and email will not be set locally. In these cases the user.useConfigOnly
setting will be in play which will not allow you to make commits until the user name and email are set at the repo level, and will prompt the user to configure that information.
edited Feb 21 '17 at 1:35
answered Feb 20 '17 at 21:12
Grant Humphries
65811015
65811015
1
Great solution. Do not forget tochmod 755
the hook script. Otherwise, it will be copied but never executed.
– onekiloparsec
Apr 11 '17 at 15:41
add a comment |
1
Great solution. Do not forget tochmod 755
the hook script. Otherwise, it will be copied but never executed.
– onekiloparsec
Apr 11 '17 at 15:41
1
1
Great solution. Do not forget to
chmod 755
the hook script. Otherwise, it will be copied but never executed.– onekiloparsec
Apr 11 '17 at 15:41
Great solution. Do not forget to
chmod 755
the hook script. Otherwise, it will be copied but never executed.– onekiloparsec
Apr 11 '17 at 15:41
add a comment |
up vote
4
down vote
GIT_AUTHOR_EMAIL
+ local .bashrc
.bashrc_local
: don't track this file, put it only on your work computer:
export GIT_AUTHOR_EMAIL='me@work.com'
export GIT_COMMITTER_EMAIL="$GIT_AUTHOR_EMAIL"
.bashrc
: track this file, make it the same on both work and home computers:
F="$HOME/.bashrc_local"
if [ -r "$F" ]; then
. "$F"
fi
I'm using https://github.com/technicalpickles/homesick to sync my dotfiles.
If only gitconfig would accept environment variables: Shell variable expansion in git config
add a comment |
up vote
4
down vote
GIT_AUTHOR_EMAIL
+ local .bashrc
.bashrc_local
: don't track this file, put it only on your work computer:
export GIT_AUTHOR_EMAIL='me@work.com'
export GIT_COMMITTER_EMAIL="$GIT_AUTHOR_EMAIL"
.bashrc
: track this file, make it the same on both work and home computers:
F="$HOME/.bashrc_local"
if [ -r "$F" ]; then
. "$F"
fi
I'm using https://github.com/technicalpickles/homesick to sync my dotfiles.
If only gitconfig would accept environment variables: Shell variable expansion in git config
add a comment |
up vote
4
down vote
up vote
4
down vote
GIT_AUTHOR_EMAIL
+ local .bashrc
.bashrc_local
: don't track this file, put it only on your work computer:
export GIT_AUTHOR_EMAIL='me@work.com'
export GIT_COMMITTER_EMAIL="$GIT_AUTHOR_EMAIL"
.bashrc
: track this file, make it the same on both work and home computers:
F="$HOME/.bashrc_local"
if [ -r "$F" ]; then
. "$F"
fi
I'm using https://github.com/technicalpickles/homesick to sync my dotfiles.
If only gitconfig would accept environment variables: Shell variable expansion in git config
GIT_AUTHOR_EMAIL
+ local .bashrc
.bashrc_local
: don't track this file, put it only on your work computer:
export GIT_AUTHOR_EMAIL='me@work.com'
export GIT_COMMITTER_EMAIL="$GIT_AUTHOR_EMAIL"
.bashrc
: track this file, make it the same on both work and home computers:
F="$HOME/.bashrc_local"
if [ -r "$F" ]; then
. "$F"
fi
I'm using https://github.com/technicalpickles/homesick to sync my dotfiles.
If only gitconfig would accept environment variables: Shell variable expansion in git config
edited May 23 '17 at 12:10
Community♦
11
11
answered Jul 8 '16 at 19:22
Ciro Santilli 新疆改造中心 六四事件 法轮功
130k27509439
130k27509439
add a comment |
add a comment |
up vote
3
down vote
Windows Environment
Additional this can be modified from Git Extensions --> Settings --> Global Settings
, if you have it installed in your systems.
gitextensions-latest-release
Right Click on a folder/directory in Windows Environment to access these settings.
Update : How to switch/maintain multiple settings in Version 2.49
How does this address multiple users, instead of just editing them?
– D_N
Feb 14 at 21:44
@D_N Updated with new screenshot with options to switch settings.
– Abhijeet
Feb 15 at 10:15
Open local repo folder and select git-config from navigation menu then click the local repo tab, the contents will be applied to $local_repo_path/.git/config as above answer.
– maxwu
May 12 at 12:37
add a comment |
up vote
3
down vote
Windows Environment
Additional this can be modified from Git Extensions --> Settings --> Global Settings
, if you have it installed in your systems.
gitextensions-latest-release
Right Click on a folder/directory in Windows Environment to access these settings.
Update : How to switch/maintain multiple settings in Version 2.49
How does this address multiple users, instead of just editing them?
– D_N
Feb 14 at 21:44
@D_N Updated with new screenshot with options to switch settings.
– Abhijeet
Feb 15 at 10:15
Open local repo folder and select git-config from navigation menu then click the local repo tab, the contents will be applied to $local_repo_path/.git/config as above answer.
– maxwu
May 12 at 12:37
add a comment |
up vote
3
down vote
up vote
3
down vote
Windows Environment
Additional this can be modified from Git Extensions --> Settings --> Global Settings
, if you have it installed in your systems.
gitextensions-latest-release
Right Click on a folder/directory in Windows Environment to access these settings.
Update : How to switch/maintain multiple settings in Version 2.49
Windows Environment
Additional this can be modified from Git Extensions --> Settings --> Global Settings
, if you have it installed in your systems.
gitextensions-latest-release
Right Click on a folder/directory in Windows Environment to access these settings.
Update : How to switch/maintain multiple settings in Version 2.49
edited Feb 15 at 10:09
answered Jul 23 '14 at 9:34
Abhijeet
4,75914462
4,75914462
How does this address multiple users, instead of just editing them?
– D_N
Feb 14 at 21:44
@D_N Updated with new screenshot with options to switch settings.
– Abhijeet
Feb 15 at 10:15
Open local repo folder and select git-config from navigation menu then click the local repo tab, the contents will be applied to $local_repo_path/.git/config as above answer.
– maxwu
May 12 at 12:37
add a comment |
How does this address multiple users, instead of just editing them?
– D_N
Feb 14 at 21:44
@D_N Updated with new screenshot with options to switch settings.
– Abhijeet
Feb 15 at 10:15
Open local repo folder and select git-config from navigation menu then click the local repo tab, the contents will be applied to $local_repo_path/.git/config as above answer.
– maxwu
May 12 at 12:37
How does this address multiple users, instead of just editing them?
– D_N
Feb 14 at 21:44
How does this address multiple users, instead of just editing them?
– D_N
Feb 14 at 21:44
@D_N Updated with new screenshot with options to switch settings.
– Abhijeet
Feb 15 at 10:15
@D_N Updated with new screenshot with options to switch settings.
– Abhijeet
Feb 15 at 10:15
Open local repo folder and select git-config from navigation menu then click the local repo tab, the contents will be applied to $local_repo_path/.git/config as above answer.
– maxwu
May 12 at 12:37
Open local repo folder and select git-config from navigation menu then click the local repo tab, the contents will be applied to $local_repo_path/.git/config as above answer.
– maxwu
May 12 at 12:37
add a comment |
up vote
2
down vote
May be it is simple hack, but it is useful. Just generate 2 ssh keys like below.
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/GowthamSai/.ssh/id_rsa): work
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in damsn.
Your public key has been saved in damsn.pub.
The key fingerprint is:
SHA256:CrsKDJWVVek5GTCqmq8/8RnwvAo1G6UOmQFbzddcoAY GowthamSai@Gowtham-MacBook-Air.local
The key's randomart image is:
+---[RSA 4096]----+
|. .oEo+=o+. |
|.o o+o.o= |
|o o o.o. + |
| =.+ . = |
|= *+. S. |
|o*.++o . |
|=.oo.+. |
| +. +. |
|.o=+. |
+----[SHA256]-----+
Same way create one more for personal. So, you have 2 ssh keys, work and company. Copy work.pub, work, personal.pub, personal to ~/.ssh/ Directory.
Then create shell script with the following lines and name it as crev.sh (Company Reverse) with following content.
cp ~/.ssh/work ~/.ssh/id_rsa
cp ~/.ssh/work.pub ~/.ssh/id_rsa.pub
Same way, create one more called prev.sh (Personal Reverse) with the following content.
cp ~/.ssh/personal ~/.ssh/id_rsa
cp ~/.ssh/personal.pub ~/.ssh/id_rsa.pub
in ~/.bashrc add aliases for those scripts like below
alias crev="sh ~/.ssh/crev.sh"
alias prev="sh ~/.ssh/prev.sh"
source ~/.bashrc
When ever you wanna use company, just do crev, and if you wanna use personal do prev :-p.
Add those ssh keys to your github accounts. Make sure, you don't have id_rsa generated perviously, because those scripts will overwrite id_rsa. If you have already generated id_rsa, use that for one of the account. Copy them as personal and skip generation of personal keys.
add a comment |
up vote
2
down vote
May be it is simple hack, but it is useful. Just generate 2 ssh keys like below.
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/GowthamSai/.ssh/id_rsa): work
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in damsn.
Your public key has been saved in damsn.pub.
The key fingerprint is:
SHA256:CrsKDJWVVek5GTCqmq8/8RnwvAo1G6UOmQFbzddcoAY GowthamSai@Gowtham-MacBook-Air.local
The key's randomart image is:
+---[RSA 4096]----+
|. .oEo+=o+. |
|.o o+o.o= |
|o o o.o. + |
| =.+ . = |
|= *+. S. |
|o*.++o . |
|=.oo.+. |
| +. +. |
|.o=+. |
+----[SHA256]-----+
Same way create one more for personal. So, you have 2 ssh keys, work and company. Copy work.pub, work, personal.pub, personal to ~/.ssh/ Directory.
Then create shell script with the following lines and name it as crev.sh (Company Reverse) with following content.
cp ~/.ssh/work ~/.ssh/id_rsa
cp ~/.ssh/work.pub ~/.ssh/id_rsa.pub
Same way, create one more called prev.sh (Personal Reverse) with the following content.
cp ~/.ssh/personal ~/.ssh/id_rsa
cp ~/.ssh/personal.pub ~/.ssh/id_rsa.pub
in ~/.bashrc add aliases for those scripts like below
alias crev="sh ~/.ssh/crev.sh"
alias prev="sh ~/.ssh/prev.sh"
source ~/.bashrc
When ever you wanna use company, just do crev, and if you wanna use personal do prev :-p.
Add those ssh keys to your github accounts. Make sure, you don't have id_rsa generated perviously, because those scripts will overwrite id_rsa. If you have already generated id_rsa, use that for one of the account. Copy them as personal and skip generation of personal keys.
add a comment |
up vote
2
down vote
up vote
2
down vote
May be it is simple hack, but it is useful. Just generate 2 ssh keys like below.
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/GowthamSai/.ssh/id_rsa): work
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in damsn.
Your public key has been saved in damsn.pub.
The key fingerprint is:
SHA256:CrsKDJWVVek5GTCqmq8/8RnwvAo1G6UOmQFbzddcoAY GowthamSai@Gowtham-MacBook-Air.local
The key's randomart image is:
+---[RSA 4096]----+
|. .oEo+=o+. |
|.o o+o.o= |
|o o o.o. + |
| =.+ . = |
|= *+. S. |
|o*.++o . |
|=.oo.+. |
| +. +. |
|.o=+. |
+----[SHA256]-----+
Same way create one more for personal. So, you have 2 ssh keys, work and company. Copy work.pub, work, personal.pub, personal to ~/.ssh/ Directory.
Then create shell script with the following lines and name it as crev.sh (Company Reverse) with following content.
cp ~/.ssh/work ~/.ssh/id_rsa
cp ~/.ssh/work.pub ~/.ssh/id_rsa.pub
Same way, create one more called prev.sh (Personal Reverse) with the following content.
cp ~/.ssh/personal ~/.ssh/id_rsa
cp ~/.ssh/personal.pub ~/.ssh/id_rsa.pub
in ~/.bashrc add aliases for those scripts like below
alias crev="sh ~/.ssh/crev.sh"
alias prev="sh ~/.ssh/prev.sh"
source ~/.bashrc
When ever you wanna use company, just do crev, and if you wanna use personal do prev :-p.
Add those ssh keys to your github accounts. Make sure, you don't have id_rsa generated perviously, because those scripts will overwrite id_rsa. If you have already generated id_rsa, use that for one of the account. Copy them as personal and skip generation of personal keys.
May be it is simple hack, but it is useful. Just generate 2 ssh keys like below.
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/GowthamSai/.ssh/id_rsa): work
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in damsn.
Your public key has been saved in damsn.pub.
The key fingerprint is:
SHA256:CrsKDJWVVek5GTCqmq8/8RnwvAo1G6UOmQFbzddcoAY GowthamSai@Gowtham-MacBook-Air.local
The key's randomart image is:
+---[RSA 4096]----+
|. .oEo+=o+. |
|.o o+o.o= |
|o o o.o. + |
| =.+ . = |
|= *+. S. |
|o*.++o . |
|=.oo.+. |
| +. +. |
|.o=+. |
+----[SHA256]-----+
Same way create one more for personal. So, you have 2 ssh keys, work and company. Copy work.pub, work, personal.pub, personal to ~/.ssh/ Directory.
Then create shell script with the following lines and name it as crev.sh (Company Reverse) with following content.
cp ~/.ssh/work ~/.ssh/id_rsa
cp ~/.ssh/work.pub ~/.ssh/id_rsa.pub
Same way, create one more called prev.sh (Personal Reverse) with the following content.
cp ~/.ssh/personal ~/.ssh/id_rsa
cp ~/.ssh/personal.pub ~/.ssh/id_rsa.pub
in ~/.bashrc add aliases for those scripts like below
alias crev="sh ~/.ssh/crev.sh"
alias prev="sh ~/.ssh/prev.sh"
source ~/.bashrc
When ever you wanna use company, just do crev, and if you wanna use personal do prev :-p.
Add those ssh keys to your github accounts. Make sure, you don't have id_rsa generated perviously, because those scripts will overwrite id_rsa. If you have already generated id_rsa, use that for one of the account. Copy them as personal and skip generation of personal keys.
answered Feb 26 '17 at 4:43
7H3 IN5ID3R
420420
420420
add a comment |
add a comment |
up vote
1
down vote
I made a bash function that handle that. Here is the Github repo.
For record:
# Look for closest .gitconfig file in parent directories
# This file will be used as main .gitconfig file.
function __recursive_gitconfig_git {
gitconfig_file=$(__recursive_gitconfig_closest)
if [ "$gitconfig_file" != '' ]; then
home="$(dirname $gitconfig_file)/"
HOME=$home /usr/bin/git "$@"
else
/usr/bin/git "$@"
fi
}
# Look for closest .gitconfig file in parents directories
function __recursive_gitconfig_closest {
slashes=${PWD//[^/]/}
directory="$PWD"
for (( n=${#slashes}; n>0; --n ))
do
test -e "$directory/.gitconfig" && echo "$directory/.gitconfig" && return
directory="$directory/.."
done
}
alias git='__recursive_gitconfig_git'
add a comment |
up vote
1
down vote
I made a bash function that handle that. Here is the Github repo.
For record:
# Look for closest .gitconfig file in parent directories
# This file will be used as main .gitconfig file.
function __recursive_gitconfig_git {
gitconfig_file=$(__recursive_gitconfig_closest)
if [ "$gitconfig_file" != '' ]; then
home="$(dirname $gitconfig_file)/"
HOME=$home /usr/bin/git "$@"
else
/usr/bin/git "$@"
fi
}
# Look for closest .gitconfig file in parents directories
function __recursive_gitconfig_closest {
slashes=${PWD//[^/]/}
directory="$PWD"
for (( n=${#slashes}; n>0; --n ))
do
test -e "$directory/.gitconfig" && echo "$directory/.gitconfig" && return
directory="$directory/.."
done
}
alias git='__recursive_gitconfig_git'
add a comment |
up vote
1
down vote
up vote
1
down vote
I made a bash function that handle that. Here is the Github repo.
For record:
# Look for closest .gitconfig file in parent directories
# This file will be used as main .gitconfig file.
function __recursive_gitconfig_git {
gitconfig_file=$(__recursive_gitconfig_closest)
if [ "$gitconfig_file" != '' ]; then
home="$(dirname $gitconfig_file)/"
HOME=$home /usr/bin/git "$@"
else
/usr/bin/git "$@"
fi
}
# Look for closest .gitconfig file in parents directories
function __recursive_gitconfig_closest {
slashes=${PWD//[^/]/}
directory="$PWD"
for (( n=${#slashes}; n>0; --n ))
do
test -e "$directory/.gitconfig" && echo "$directory/.gitconfig" && return
directory="$directory/.."
done
}
alias git='__recursive_gitconfig_git'
I made a bash function that handle that. Here is the Github repo.
For record:
# Look for closest .gitconfig file in parent directories
# This file will be used as main .gitconfig file.
function __recursive_gitconfig_git {
gitconfig_file=$(__recursive_gitconfig_closest)
if [ "$gitconfig_file" != '' ]; then
home="$(dirname $gitconfig_file)/"
HOME=$home /usr/bin/git "$@"
else
/usr/bin/git "$@"
fi
}
# Look for closest .gitconfig file in parents directories
function __recursive_gitconfig_closest {
slashes=${PWD//[^/]/}
directory="$PWD"
for (( n=${#slashes}; n>0; --n ))
do
test -e "$directory/.gitconfig" && echo "$directory/.gitconfig" && return
directory="$directory/.."
done
}
alias git='__recursive_gitconfig_git'
answered Feb 7 at 16:48
Arount
4,450925
4,450925
add a comment |
add a comment |
up vote
0
down vote
Something like Rob W's answer, but allowing different a different ssh key, and works with older git versions (which don't have e.g. a core.sshCommand config).
I created the file ~/bin/git_poweruser
, with executable permission, and in the PATH:
#!/bin/bash
TMPDIR=$(mktemp -d)
trap 'rm -rf "$TMPDIR"' EXIT
cat > $TMPDIR/ssh << 'EOF'
#!/bin/bash
ssh -i $HOME/.ssh/poweruserprivatekey $@
EOF
chmod +x $TMPDIR/ssh
export GIT_SSH=$TMPDIR/ssh
git -c user.name="Power User name" -c user.email="power@user.email" $@
Whenever I want to commit or push something as "Power User", I use git_poweruser
instead of git
. It should work on any directory, and does not require changes in .gitconfig
or .ssh/config
, at least not in mine.
add a comment |
up vote
0
down vote
Something like Rob W's answer, but allowing different a different ssh key, and works with older git versions (which don't have e.g. a core.sshCommand config).
I created the file ~/bin/git_poweruser
, with executable permission, and in the PATH:
#!/bin/bash
TMPDIR=$(mktemp -d)
trap 'rm -rf "$TMPDIR"' EXIT
cat > $TMPDIR/ssh << 'EOF'
#!/bin/bash
ssh -i $HOME/.ssh/poweruserprivatekey $@
EOF
chmod +x $TMPDIR/ssh
export GIT_SSH=$TMPDIR/ssh
git -c user.name="Power User name" -c user.email="power@user.email" $@
Whenever I want to commit or push something as "Power User", I use git_poweruser
instead of git
. It should work on any directory, and does not require changes in .gitconfig
or .ssh/config
, at least not in mine.
add a comment |
up vote
0
down vote
up vote
0
down vote
Something like Rob W's answer, but allowing different a different ssh key, and works with older git versions (which don't have e.g. a core.sshCommand config).
I created the file ~/bin/git_poweruser
, with executable permission, and in the PATH:
#!/bin/bash
TMPDIR=$(mktemp -d)
trap 'rm -rf "$TMPDIR"' EXIT
cat > $TMPDIR/ssh << 'EOF'
#!/bin/bash
ssh -i $HOME/.ssh/poweruserprivatekey $@
EOF
chmod +x $TMPDIR/ssh
export GIT_SSH=$TMPDIR/ssh
git -c user.name="Power User name" -c user.email="power@user.email" $@
Whenever I want to commit or push something as "Power User", I use git_poweruser
instead of git
. It should work on any directory, and does not require changes in .gitconfig
or .ssh/config
, at least not in mine.
Something like Rob W's answer, but allowing different a different ssh key, and works with older git versions (which don't have e.g. a core.sshCommand config).
I created the file ~/bin/git_poweruser
, with executable permission, and in the PATH:
#!/bin/bash
TMPDIR=$(mktemp -d)
trap 'rm -rf "$TMPDIR"' EXIT
cat > $TMPDIR/ssh << 'EOF'
#!/bin/bash
ssh -i $HOME/.ssh/poweruserprivatekey $@
EOF
chmod +x $TMPDIR/ssh
export GIT_SSH=$TMPDIR/ssh
git -c user.name="Power User name" -c user.email="power@user.email" $@
Whenever I want to commit or push something as "Power User", I use git_poweruser
instead of git
. It should work on any directory, and does not require changes in .gitconfig
or .ssh/config
, at least not in mine.
answered May 27 '17 at 7:43
Jellby
7411229
7411229
add a comment |
add a comment |
up vote
0
down vote
Here is what I just found after following the steps in many answers here
How to set up Multiple SSH Keys settings for different github account
You might want to start checking your currently saved keys
$ ssh-add -l
If you decide to delete all cached keys before (optional, carefull about this)
$ ssh-add -D
Then you can create a ssh pub/priv key linked to each email/account that you wish/need to use
$ cd ~/.ssh
$ ssh-keygen -t rsa -C "work@company.com" <-- save it as "id_rsa_work"
$ ssh-keygen -t rsa -C "pers@email.com" <-- save it as "id_rsa_pers"
After performing this commands you will have the following files created
~/.ssh/id_rsa_work
~/.ssh/id_rsa_work.pub
~/.ssh/id_rsa_pers
~/.ssh/id_rsa_pers.pub
Make sure authentication agent is running
$ eval `ssh-agent -s`
Add the generated keys as following (from the ~/.ssh folder)
$ ssh-add id_rsa_work
$ ssh-add id_rsa_pers
Now you can check your saved keys again
$ ssh-add -l
Now you need to add the generated public keys to your github/bickbuket server Acces Keys
Clone each of the repos to different folders
Go to the folder where user work will be used and do this
$ git config user.name "Working Hard"
$ git config user.email "work@company.com"
Just to see what this does check the contents of the ".git/config"
Go to the folder where user activehacker will be used and do this
$ git config user.name "Personal Account"
$ git config user.email "pers@email.com"
Just to see what this does check the contents of the ".git/config"
After all this you will be able to commit your personal and work code by switch folders
If you have any problems let me know mgg.isco@gmail.com
add a comment |
up vote
0
down vote
Here is what I just found after following the steps in many answers here
How to set up Multiple SSH Keys settings for different github account
You might want to start checking your currently saved keys
$ ssh-add -l
If you decide to delete all cached keys before (optional, carefull about this)
$ ssh-add -D
Then you can create a ssh pub/priv key linked to each email/account that you wish/need to use
$ cd ~/.ssh
$ ssh-keygen -t rsa -C "work@company.com" <-- save it as "id_rsa_work"
$ ssh-keygen -t rsa -C "pers@email.com" <-- save it as "id_rsa_pers"
After performing this commands you will have the following files created
~/.ssh/id_rsa_work
~/.ssh/id_rsa_work.pub
~/.ssh/id_rsa_pers
~/.ssh/id_rsa_pers.pub
Make sure authentication agent is running
$ eval `ssh-agent -s`
Add the generated keys as following (from the ~/.ssh folder)
$ ssh-add id_rsa_work
$ ssh-add id_rsa_pers
Now you can check your saved keys again
$ ssh-add -l
Now you need to add the generated public keys to your github/bickbuket server Acces Keys
Clone each of the repos to different folders
Go to the folder where user work will be used and do this
$ git config user.name "Working Hard"
$ git config user.email "work@company.com"
Just to see what this does check the contents of the ".git/config"
Go to the folder where user activehacker will be used and do this
$ git config user.name "Personal Account"
$ git config user.email "pers@email.com"
Just to see what this does check the contents of the ".git/config"
After all this you will be able to commit your personal and work code by switch folders
If you have any problems let me know mgg.isco@gmail.com
add a comment |
up vote
0
down vote
up vote
0
down vote
Here is what I just found after following the steps in many answers here
How to set up Multiple SSH Keys settings for different github account
You might want to start checking your currently saved keys
$ ssh-add -l
If you decide to delete all cached keys before (optional, carefull about this)
$ ssh-add -D
Then you can create a ssh pub/priv key linked to each email/account that you wish/need to use
$ cd ~/.ssh
$ ssh-keygen -t rsa -C "work@company.com" <-- save it as "id_rsa_work"
$ ssh-keygen -t rsa -C "pers@email.com" <-- save it as "id_rsa_pers"
After performing this commands you will have the following files created
~/.ssh/id_rsa_work
~/.ssh/id_rsa_work.pub
~/.ssh/id_rsa_pers
~/.ssh/id_rsa_pers.pub
Make sure authentication agent is running
$ eval `ssh-agent -s`
Add the generated keys as following (from the ~/.ssh folder)
$ ssh-add id_rsa_work
$ ssh-add id_rsa_pers
Now you can check your saved keys again
$ ssh-add -l
Now you need to add the generated public keys to your github/bickbuket server Acces Keys
Clone each of the repos to different folders
Go to the folder where user work will be used and do this
$ git config user.name "Working Hard"
$ git config user.email "work@company.com"
Just to see what this does check the contents of the ".git/config"
Go to the folder where user activehacker will be used and do this
$ git config user.name "Personal Account"
$ git config user.email "pers@email.com"
Just to see what this does check the contents of the ".git/config"
After all this you will be able to commit your personal and work code by switch folders
If you have any problems let me know mgg.isco@gmail.com
Here is what I just found after following the steps in many answers here
How to set up Multiple SSH Keys settings for different github account
You might want to start checking your currently saved keys
$ ssh-add -l
If you decide to delete all cached keys before (optional, carefull about this)
$ ssh-add -D
Then you can create a ssh pub/priv key linked to each email/account that you wish/need to use
$ cd ~/.ssh
$ ssh-keygen -t rsa -C "work@company.com" <-- save it as "id_rsa_work"
$ ssh-keygen -t rsa -C "pers@email.com" <-- save it as "id_rsa_pers"
After performing this commands you will have the following files created
~/.ssh/id_rsa_work
~/.ssh/id_rsa_work.pub
~/.ssh/id_rsa_pers
~/.ssh/id_rsa_pers.pub
Make sure authentication agent is running
$ eval `ssh-agent -s`
Add the generated keys as following (from the ~/.ssh folder)
$ ssh-add id_rsa_work
$ ssh-add id_rsa_pers
Now you can check your saved keys again
$ ssh-add -l
Now you need to add the generated public keys to your github/bickbuket server Acces Keys
Clone each of the repos to different folders
Go to the folder where user work will be used and do this
$ git config user.name "Working Hard"
$ git config user.email "work@company.com"
Just to see what this does check the contents of the ".git/config"
Go to the folder where user activehacker will be used and do this
$ git config user.name "Personal Account"
$ git config user.email "pers@email.com"
Just to see what this does check the contents of the ".git/config"
After all this you will be able to commit your personal and work code by switch folders
If you have any problems let me know mgg.isco@gmail.com
edited Jun 25 at 17:33
answered Jun 25 at 16:46
Mauricio Gracia Gutierrez
5,42733657
5,42733657
add a comment |
add a comment |
up vote
0
down vote
Although most questions sort of answered the OP, I just had to go through this myself and without even googling I was able to find the quickest and simplest solution. Here's simple steps:
- copy existing
.gitconfg
from your other repo - paste into your newly added repo
- change values in
.gitconfig
file, such as name, email and username
[user]
name = John
email = john@email.net
username = john133
- add filename to
.gitignore
list, to make sure you don't commit.gitconfig
file to your work repo
add a comment |
up vote
0
down vote
Although most questions sort of answered the OP, I just had to go through this myself and without even googling I was able to find the quickest and simplest solution. Here's simple steps:
- copy existing
.gitconfg
from your other repo - paste into your newly added repo
- change values in
.gitconfig
file, such as name, email and username
[user]
name = John
email = john@email.net
username = john133
- add filename to
.gitignore
list, to make sure you don't commit.gitconfig
file to your work repo
add a comment |
up vote
0
down vote
up vote
0
down vote
Although most questions sort of answered the OP, I just had to go through this myself and without even googling I was able to find the quickest and simplest solution. Here's simple steps:
- copy existing
.gitconfg
from your other repo - paste into your newly added repo
- change values in
.gitconfig
file, such as name, email and username
[user]
name = John
email = john@email.net
username = john133
- add filename to
.gitignore
list, to make sure you don't commit.gitconfig
file to your work repo
Although most questions sort of answered the OP, I just had to go through this myself and without even googling I was able to find the quickest and simplest solution. Here's simple steps:
- copy existing
.gitconfg
from your other repo - paste into your newly added repo
- change values in
.gitconfig
file, such as name, email and username
[user]
name = John
email = john@email.net
username = john133
- add filename to
.gitignore
list, to make sure you don't commit.gitconfig
file to your work repo
answered Aug 10 at 19:55
Tatarin
809523
809523
add a comment |
add a comment |
up vote
0
down vote
Just add this to your ~/.bash_profile to switch between default keys for github.com
# Git SSH keys swap
alias work_git="ssh-add -D && ssh-add -K ~/.ssh/id_rsa_work"
alias personal_git="ssh-add -D && ssh-add -K ~/.ssh/id_rsa"
add a comment |
up vote
0
down vote
Just add this to your ~/.bash_profile to switch between default keys for github.com
# Git SSH keys swap
alias work_git="ssh-add -D && ssh-add -K ~/.ssh/id_rsa_work"
alias personal_git="ssh-add -D && ssh-add -K ~/.ssh/id_rsa"
add a comment |
up vote
0
down vote
up vote
0
down vote
Just add this to your ~/.bash_profile to switch between default keys for github.com
# Git SSH keys swap
alias work_git="ssh-add -D && ssh-add -K ~/.ssh/id_rsa_work"
alias personal_git="ssh-add -D && ssh-add -K ~/.ssh/id_rsa"
Just add this to your ~/.bash_profile to switch between default keys for github.com
# Git SSH keys swap
alias work_git="ssh-add -D && ssh-add -K ~/.ssh/id_rsa_work"
alias personal_git="ssh-add -D && ssh-add -K ~/.ssh/id_rsa"
answered Nov 10 at 20:43
Jason DeBolt
1
1
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f4220416%2fcan-i-specify-multiple-users-for-myself-in-gitconfig%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
See git-config#FILES.
– Josh Lee
Nov 18 '10 at 22:48
I'm surprised that your company server would do that - it would have to CHANGE the sha of your commit for that to work. If you make a commit to a local checkout, what username do you see?
– Alex Brown
Nov 18 '10 at 22:59
@Alex: Presumably the notification hook is trying to match the author information (email, perhaps) with some other list/database, maybe in order to canonicalize it, maybe looking for some other information. Obviously not the best approach for this particular case, though!
– Cascabel
Nov 18 '10 at 23:48
1
@Alex: Forgot the important bit there - it could easily just be a name on the email notification, not actually changing anything on the commit.
– Cascabel
Nov 19 '10 at 0:49
1
You can use a git-hook to automate this recurring work: github.com/DrVanScott/git-clone-init
– Henning
Apr 22 '17 at 17:40