How to use Bitbucket and GitHub at the same time for one project?

前端 未结 3 431
旧时难觅i
旧时难觅i 2020-12-12 08:28

I have one repository which I want to push into Bitbucket and GitHub. It is vital for my repository to be hosted by both.

Is there a way to do this in Git?

相关标签:
3条回答
  • 2020-12-12 08:55

    You can use multiple remote repositories with git. But you'll have to push separately into 2 of your remotes I believe.

    For example, if your project currently points to github, you can rename your current remote repository to github:

    $ git remote rename origin github
    

    You can then add another remote repository, say bitbucket:

    $ git remote add bitbucket git@bitbucket.org:your_user/your_repo.git
    

    Now in order to push changes to corresponding branch on github or bitbucket you can do this:

    $ git push github HEAD
    $ git push bitbucket HEAD
    

    Same rule applies to pulling: you need to specify which remote you want to pull from:

    $ git pull github your_branch
    $ git pull bitbucket your_branch
    
    0 讨论(0)
  • 2020-12-12 09:01

    A few EASY solutions.

    Multiple remotes pushed (and fetched) independently

    This is the easiest to get your head around, but the most effort to maintain.

    We start out by adding our new remote:

    $ cd myproject 
    $ git remote add bitbucket ssh://git@bitbucket.org/user/myproject.git 
    $ git push bitbucket master
    

    Straight forward no? Except of course every time we commit any changes, we need to push to both our original “origin” and our new remote “bitbucket”:

    $ git push origin master
    $ git push bitbucket master
    

    Not a massive overhead, but I’m sure it will grate over time. Or you can create an `alias gpob="git push origin master && git push bitbucket master".

    Single remote with multiple URLs pushed (and fetched) consecutively

    With this method, we are going to add an additional URL to our existing remote “origin”:

    $ cd myproject
    $ git remote set-url --add origin ssh://git@bitbucket.org/user/myproject.git
    $ git push origin master
    Everything up-to-date
    Everything up-to-date
    

    Much less effort!

    Of course silver lining has a cloud, and in this case, it is that while we can push to multiple URLs simultaneously, we can only fetch from the original “origin” (you can change this, but that is out of scope for this post).

    Finally, to see which remote will be fetched from:

    $ git remote -v show
    

    I blogged about it as well.

    0 讨论(0)
  • 2020-12-12 09:15

    Yes, you can do that. You don't have to push twice but just once to push to both remote repositories. I had the same issue before so wrote how to do it here. Git: Push to / Pull from Both Github and Bitbucket

    0 讨论(0)
提交回复
热议问题