如何从GitHub克隆项目但使用Bitbucket作为远程仓库

4

我想从GitHub克隆一个开源项目,但我想使用Bitbucket作为我的远程存储库主机,因为它的私有repo是免费的。

我不打算fork / pull request,但当原始项目得到更新时,我想将更新拉到我的本地存储库并合并。

我认为最终我需要两个远程存储库。(origin和upstream?)

设置这样的环境的正确/安全方法是什么?


2
我认为你已经描述了一个合适的解决方案。您将需要两个远程,一个指向gitub,另一个指向bitbucket。 originupstream这些名称在这种设置中非常典型。第一次推送分支时,请确保使用-u设置正确的远程跟踪分支。 - larsks
谢谢,很高兴知道我走在正确的方向上。我只知道git的基本命令,从未尝试过这个级别的操作。我阅读了许多博客文章和文章,但没有找到完全相同情况的内容。 - jun_k
1个回答

7
我刚刚在GitHub上测试了一个项目:google-calendar-backup
首先,我登录我的Bitbucket账户并创建了一个名为google-calendar-backup的私有Git仓库。
然后,我按照以下步骤进行:
(注意:我使用的是Windows系统,所以在你的机器上路径可能会不同)
  1. Clone original repo from GitHub

    git clone https://github.com/christianspecht/google-calendar-backup C:\LocalDir
    
  2. This will create a remote "origin" in the local repo which points to GitHub.
    Since you want to work mainly with Bitbucket, Bitbucket should be the "main" remote origin, so we'll rename the existing one.

    cd c:\localdir
    git remote rename origin upstream
    
  3. Go to local dir and add the Bitbucket repo as a second remote:

    git remote add origin https://bitbucket.org/christianspecht/google-calendar-backup
    
  4. (optional) Show both remotes:

    git remote -v
    

    On my machine, I get this result:

    origin  https://bitbucket.org/christianspecht/google-calendar-backup (fetch)
    origin  https://bitbucket.org/christianspecht/google-calendar-backup (push)
    upstream        https://github.com/christianspecht/google-calendar-backup (fetch)
    upstream        https://github.com/christianspecht/google-calendar-backup (push)
    
  5. Push all branches to Bitbucket once

    git push -u --all origin
    

    Result:

    Username for 'https://bitbucket.org': christianspecht
    Password for 'https://christianspecht@bitbucket.org':
    Counting objects: 30, done.
    Delta compression using up to 4 threads.
    Compressing objects: 100% (30/30), done.
    Writing objects: 100% (30/30), 4.41 KiB | 0 bytes/s, done.
    Total 30 (delta 12), reused 0 (delta 0)
    To https://bitbucket.org/christianspecht/google-calendar-backup
     * [new branch]      master -> master
    Branch master set up to track remote branch master from origin.
    

    (Credit to larsks for -u - I didn't know this, I'm still learning Git myself)


就是这样。现在你可以进行修改并将其推送到 origin (Bitbucket)。
有时候,你需要从 upstream (GitHub) 拉取以获取原始项目的新更改。


1
谢谢,它完美地运行了。我还需要在工作中设置另一台计算机以相同的方式。但是由于我已经将代码推送到Bitbucket存储库,所以我考虑克隆这个而不是Github上的原始版本,然后向Github存储库添加一个上游分支。我明天会尝试。 - jun_k

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接