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 "[email protected]" -f '/Users/arnaudrinquin/.ssh/id_rsa'
[...]
ssh-keygen -t rsa -C "[email protected]" -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 [email protected]
by git@github_pro
.
You can either change your existing projects remotes (using something like git remote set-url origin git@github_pro:foo/bar.git
) or adapt them directly when cloning them.
git clone [email protected]: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 '[email protected]'
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 = [email protected]
...
[alias]
setpromail = "config user.email '[email protected]'"
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 [email protected]:ArnaudRinquin/arnaudrinquin.github.io.git (fetch)
origin [email protected]: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 '[email protected]'"
gopro = !sh -c \"git changeremotehost github.com github_pro && git changeremotehost bitbucket.com bitbucket_pro && git setpromail\"
Source Link -Github
Source Link -Tutorial