DEV Community

vikrampawar
vikrampawar

Posted on

Same Repo On Both Bitbucket And Github

I used bitbucket as my main remote hub for some years now. The main reason was that we use bitbucket at work, so I was more familiar with it. The other reason of course was that I could host my private repositories there. Now Github also allows hosting of private repositories, so I wanted to add Github as another remote to my existing git repositories. Here is what I did.

First I checked what is the remote by using the below command.

# Shows remote
git remote -v show

### output
origin  https://Vikrampawar@bitbucket.org/Vikrampawar/javaprojects.git (fetch)
origin  https://Vikrampawar@bitbucket.org/Vikrampawar/javaprojects.git (push)

Then I looked at the git config in a text editor.

The config is in the file .git/config. It has an entry as below

[remote "origin"]
    url = https://Vikrampawar@bitbucket.org/Vikrampawar/javaprojects.git
    fetch = +refs/heads/*:refs/remotes/origin/*

To set Github as another remote, I ran the below command.

git remote set-url origin --add git@github.com:vikrampawar/javaprojects.git

### output
origin  https://Vikrampawar@bitbucket.org/Vikrampawar/javaprojects.git (fetch)
origin  https://Vikrampawar@bitbucket.org/Vikrampawar/javaprojects.git (push)
origin  git@github.com:vikrampawar/javaprojects.git (push)

This added the remote to the config

[remote "origin"]
    url = https://Vikrampawar@bitbucket.org/Vikrampawar/javaprojects.git
    fetch = +refs/heads/*:refs/remotes/origin/*
    url = git@github.com:vikrampawar/javaprojects.git   

I had issues. So, I first created an empty project called 'javaprojects' on Github, and then I also changed the protocol to https instead of git.

git remote -v show
### output
origin  https://Vikrampawar@bitbucket.org/Vikrampawar/javaprojects.git (fetch)
origin  https://Vikrampawar@bitbucket.org/Vikrampawar/javaprojects.git (push)
origin  https://github.com/vikrampawar/javaprojects.git (push)

When I did a push, it worked. I have the repo on Github as well now.

This only works one way now. I can push changes to Github, but I can't pull from it. I'll do that setup later.

Top comments (3)

Collapse
 
_garybell profile image
Gary Bell

Whenever I've needed to use different remotes, I've set them up to be completely different remote names. For instance if I had the origin on GitLab as git@gitlab.com:garybell/myproject.git, but also wanted to host it on GitHub, I'd run the following:
git remote add github git@github.com:garybell/myproject.git
Following any commit messages, I can then run:
git push origin branch && git push github branch, and the changes are pushed to both.

Collapse
 
vikrampawar profile image
vikrampawar

That’s a good option as well. If I remember correctly β€˜origin’ is just a branch like any other. It is the main branch by convention and there is nothing special about it.

Collapse
 
_garybell profile image
Gary Bell

Origin is just the name of the remote, yes. Much like master is the branch. I've just found that naming them to they are useful when using more than just "origin" helps