Reputation: 417
I have two github accounts, 1 for personal and other one for work.
If I git clone repo from my work github account, is there a way automatically set git config user.name
and git config user.email
for work config?
Upvotes: 0
Views: 67
Reputation: 212228
You can put some logic in a post-checkout hook, which will be run after a clone. For example:
#!/bin/sh
die() { test -n "$*" && echo "$@" >&2; exit 1; }
if test "$1" = 0000000000000000000000000000000000000000; then
url=$(git ls-remote 2>&1 > /dev/null | awk '{print $2; exit}')
test -n "$url" || die Cannot find url of remote
case $url in
*unique_string*)
git config user.email [email protected]
git config user.name Hello World
;;
esac
fi
In this case, pick some string from your work url that you can use to recognize the repo. In order to get this hook to run after a clone, you'll need to configure git to use it, and that's probably easiest to do by using a template dir. That is, put the above script in $HOME/.config/git-templates/hooks/post-checkout
(or wherever you would like, and don't forget to chmod +x) and run git config --global init.templatedir '~/.config/git-templates'
You can probably avoid the ls-remote
cruft and just reference origin
, but that will fail if you've used --origin
in clone to use a non-default name. There's probably (undoubtedly) a cleaner way to get the remote url than filtering the output of ls-remote, but that's a different question. I'm sure a better solution will be found in the comments in the near future!
Upvotes: 2
Reputation: 1470
you can achieve this by writing the custom script which will automatically run and configure git config when you switch to your project directory.
just like rvmrc file which configures gemset
for writing script you can refer below link: Refer this https://willi.am/blog/2015/02/27/dynamically-configure-your-git-email/
Upvotes: 1