How to set up and switch between GitHub/Bitbucket accounts when committing your code
Most of the developers out there work on personal projects and on work related projects simultaneously. And most likely multiple accounts are used for making code commits. Which brings us to this topic.
In this article, I will show how to set up and switch between profiles for each repository.
Prerequisites
- Bitbucket or GitHub accounts
- Installed a GPG tool such as gpgtools (https://gpgtools.org/)
Step 1
Generate a new SSH Key using below command and the example sequence is as below.
$~ ssh-keygen -t ed25519 -C "myworkemail@work.com"Generating public/private ed25519 key pair.
Enter file in which to save the key (/Users/yourname/.ssh/id_ed25519): bibucket-work
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in bibucket-work.
Your public key has been saved in bibucket-work.pub.
The key fingerprint is:
SHA256:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/XXX
myworkemail@work.com
The key's randomart image is:
+--[ED25519 256]--+
| ..ooo==. |
| .=*o*+o |
| .ooo+.E |
| . @.*.o |
| = B B . |
| + B + |
| . o = |
| |
| |
+----[SHA256]-----+
The keygen command will create 2 files. A private key file and the public key file. The public key file is what you should share or upload to platforms like GitHub, Bitbucket or pantheon etc in order to authenticate your machine with the destination.
Step 2
In a mac device, these keys are usually housed under ~/.ssh
path.
So for security purposes, I will move the two newly created key files into this path.
mv bibucket-work ~/.sshmv bibucket-work.pub ~/.ssh
Step 3
Now we can log in to our Bitbucket account and head to https://bitbucket.org/account/settings/ssh-keys/ so that we can add the public key.
Click on Add key
Let's copy and paste our bitbucket-work.pub
key content into the text field using the below command.
cat ~/.ssh/bitbucket-work.pub | pbcopy
Step 4
Now let’s create a new host record for our key in the ~/.ssh/config
file.
You can have as many host records as you like, and this will be a key element that will help us to switch between accounts and tell the repository config to use a specific key file.
Type in vi ~/.ssh/config
from your terminal and add below code.
You can provide any hostname as long as it is ad hears to the syntax of ssh config files.
Host myBitbucketWorkKey
HostName bitbucket.org
UseKeychain yes
AddKeysToAgent yes
User <YOUR BITBUCKET USERNAME>
IdentityFile ~/.ssh/bitbucket-work
for GitHub
Host myGitHubWorkKey
HostName github.com
UseKeychain yes
AddKeysToAgent yes
User <YOUR BITBUCKET USERNAME>
IdentityFile ~/.ssh/github-work
Step 5
Now we create an example repository on Bitbucket, clone it locally.
git clone git@bitbucket.org:<YOUR BITBUCKET USERNAME>/test.git
cd test
cat .git/config
You should see something like this;
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
ignorecase = true
precomposeunicode = true
[remote "origin"]
url = git@bitbucket.org:<YOUR BITBUCKET USERNAME>/test.git
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
remote = origin
merge = refs/heads/master
rebase = true
Step 6
Now to configure the repository git config file.
To add username and user email values into .git/config
file you can run below commands
git config user.name "<YOUR NAME>”git config user.email “myworkemail@work.com"
or
vi .git/config
and paste in below
[user]
name = <YOUR NAME>
email = myworkemail@work.com
Now let's add the RSA/DSA identity files to the ssh agent.
Check if the ssh-agent is running
$ ssh-agentSSH_AUTH_SOCK=/var/folders/54/XXXXXXXXXXXXXX0000gn/T//ssh-XXXXXXXXXXX/agent.35351; export SSH_AUTH_SOCK;
SSH_AGENT_PID=35352; export SSH_AGENT_PID;
echo Agent pid 35352;
If you get a response like above means the ssh-agent is running.
Now let's add the identity file, which is the SSH Key file we created in step 1.
ssh-add ~/.ssh/github-work
Now you are all set! You can add, edit or delete files as normal and continue your dev work, then simply follow the git commit and push workflows.
Whenever you are changing the repositories make sure you add the correct key via ssh-add command.
You can verify if you’re using the correct account by following command.
For Bitbucket
$ ssh -T <YOUR USERNAME>@bitbucket.comThe authenticity of host 'bitbucket.com (18.205.93.4)' can't be established.
RSA key fingerprint is SHA256:XXXXXXXXXXXXXXXXXXXXXXX.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'bitbucket.com,18.205.93.4' (RSA) to the list of known hosts.
logged in as <YOUR USERNAME>
For GitHub
$ ssh -T <YOUR USERNAME>@github.comHi <YOUR USERNAME>! You've successfully authenticated, but GitHub does not provide shell access.
Step 7
This is an extra step and a nice to have. But some repositories require your commits to be signed and verified in order for you to contribute to it.
From your GitHub account, navigate to https://github.com/settings/keys
You will notice a section for GPG keys.
Step 7.1
In order to add a GPG key, you need a key generator.
You can refer to this article to see the steps to generate the keys via terminal.
But for this example I use GPG Keychain application.
You can create a new key there.
Now simply export it and copy the content to your GitHub GPG Keys section. Although there is no option to add GPG Keys to a Bitbucket account, you can still sign your commits.
Step 7.2
Verify the key has been created.
$ gpg --list-secret-keys --keyid-format=long/Users/yourname/.gnupg/pubring.gpg
-------------------------------------
sec rsa4096/<SECURE KEY CODE> 2021-07-29 [SC]
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
uid [ultimate] <KEY LABEL>
<myworkemail@work.com>
ssb rsa4096/XXXXXXXXXXXX 2021-07-29 [E]
Tell the git config file that gpgsign is required for each commit
git config commit.gpgsign true
or add below line directly to .git/config
[commit]
gpgsign = true
Finally, set the sign key code
git config user.signingkey <SECURE KEY CODE>
Now your repository .git/config
will look like this
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
ignorecase = true
precomposeunicode = true
[remote "origin"]
url = git@bitbucket.org:<YOUR BITBUCKET USERNAME>/test.git
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
remote = origin
merge = refs/heads/master
rebase = true
[user]
name = <YOUR NAME>
email = myworkemail@work.com
signingkey = <SECURE KEY CODE>
[commit]
gpgsign = true
Now you are all set. Follow the same steps again in order to add a new account. Make sure you follow the commands in this article and do not use
--global
flag when executing git commands as it will apply globally. That will defeat the purpose of setting up repository specific configurations.
Resources
- SSH Config file structure and patterns
https://linuxize.com/post/using-the-ssh-config-file/#ssh-config-file-structure-and-patterns
- Generating SSH Key files
- Signing with your GPG key
Extras
- A nifty tool to switch git user info
https://github.com/joealba/gitswitch
- SSH commands
https://linux.101hacks.com/unix/ssh-add/
- Setting up repository remote
https://docs.github.com/en/get-started/getting-started-with-git/managing-remote-repositories
- Troubleshooting gpg failed to sign error
https://stackoverflow.com/questions/41052538/git-error-gpg-failed-to-sign-data
- Troubleshooting permission denied publickey error