1

I'm currently using Bitbucket for both work and personal projects. The problem is that I have two different Bitbucket users I log in as to access and commit to the repos I use for work and the repos I use for my personal projects.

I use one SSH key pair for the work repos and another SSH key pair for my personal repos. However, when I run the following command, I always see that I'm logged into Bitbucket under my work user:

ssh -vT git@bitbucket.com

As a result, I cannot push or pull from my personal Bitbucket repos from the command line. How can I set up my repos so that it automatically always pushes and pulls work repos as my work Bitbucket user and personal repos as my personal Bitbucket user? Thank you.

HartleySan
  • 7,404
  • 14
  • 66
  • 119
  • Different gitconfig files? – matt Oct 20 '20 at 03:26
  • matt, could you please be more specific? Each repo has its own `gitconfig` file, but still, when I try to push or pull from my personal repos, I get `Forbidden` errors. I'm setting the remote and everything. – HartleySan Oct 20 '20 at 03:31
  • Basically, from the local repo, I ran `git init`. Then, I ran `git remote add origin git@bitbucket.org:personal-bitbucket-user-name/personal-bitbucket-repo-name.git`, and then `git push -u origin master`, but it always fails on the last one. The SSH public key is already in BB for my personal accoun too. – HartleySan Oct 20 '20 at 03:34
  • https://stackoverflow.com/search?q=%5Bbitbucket%5D+%5Bgit%5D+multiple+accounts – phd Oct 20 '20 at 08:04

1 Answers1

2

The way to do this is with the combination of the unqiue $HOME/.ssh/config file and the reponame/.git/config file found in a given git repo.

Your $HOME/.ssh/config file would look like the following.

username@hostname:~$ cat ~/.ssh/config
Host bbmine
        HostName bitbucket.org
        User git
        IdentityFile /home/username/.ssh/id_rsamine

Host bbwork
        HostName bitbucket.org
        User git
        IdentityFile /home/usermname/.ssh/id_rsawork

In the file reponame/.git/config in your git repo you would place a remote section like the following:

[remote "bbmineremote"]
        url = bbmine:bitbucketusername/reponame.git
        fetch = +refs/heads/*:refs/remotes/bitbucket/*

You could then push and pull branches like

git push bbmineremote master

or

git pull bbmineremote master

You can confirm that you have set this up correctly by running the following ssh command

username@hostname:~$ ssh bbmine
PTY allocation request failed on channel 0
logged in as bitbucketusername

You can use git or hg to connect to Bitbucket. Shell access is disabled
Connection to bitbucket.org closed.

These Host sections in an .ssh/config file are kind of like aliases for anything that uses ssh, like git.

OregonTrail
  • 8,594
  • 7
  • 43
  • 58
  • 1
    OregonTrail, thanks a lot for your answer. I ended up using a combination of this answer and the other SO thread linked to above (https://stackoverflow.com/search?q=%5Bbitbucket%5D+%5Bgit%5D+multiple+accounts) simply because I didn't want to have to type the alias every time I get a git push or pull. – HartleySan Oct 20 '20 at 13:10
  • 1
    `git push bbmine master` isn't referring to the ssh host, it's referring to the git remote, if you set the remote as a tracking branch for any given branch that you're working on, then you can run `git pull` and `git push` as you normally would. – OregonTrail Oct 20 '20 at 16:01
  • Gotcha! Thanks for that follow-up. I get it. – HartleySan Oct 20 '20 at 17:09
  • 1
    I updated the answer to clarify that – OregonTrail Oct 21 '20 at 18:11
  • 1
    See https://stackoverflow.com/questions/520650/make-an-existing-git-branch-track-a-remote-branch for how to make a branch track a specific remote – OregonTrail Oct 21 '20 at 18:18