在github上同步一个分支(fork)

在同步之前,需要创建一个远程点指向上游仓库(repo).如果你已经派生了一个原始仓库,可以按照如下方法做。

    $ git remote -v  
    # List the current remotes (列出当前远程仓库)  
    # origin  https://github.com/user/repo.git (fetch)  
    # origin  https://github.com/user/repo.git (push)  
    $ git remote add upstream https://github.com/otheruser/repo.git  
    # Set a new remote (设置一个新的远程仓库)  
    $ git remote -v  
    # Verify new remote (验证新的原唱仓库)  
    # origin    https://github.com/user/repo.git (fetch)  
    # origin    https://github.com/user/repo.git (push)  
    # upstream  https://github.com/otheruser/repo.git (fetch)  
    # upstream  https://github.com/otheruser/repo.git (push)  

同步

同步上游仓库到你的仓库需要执行两步:首先你需要从远程拉去,之后你需要合并你希望的分支到你的本地副本分支。

拉取

从远程仓库拉取将取回其分支以及各自的提交。它们将存储在你本地仓库的指定分之下。

    $ git fetch upstream
    # Grab the upstream remote's branches
    # remote: Counting objects: 75, done.
    # remote: Compressing objects: 100% (53/53), done.
    # remote: Total 62 (delta 27), reused 44 (delta 9)
    # Unpacking objects: 100% (62/62), done.
    # From https://github.com/otheruser/repo
    #  * [new branch]      master     -> upstream/master

现在我们把上游master保存到了本地仓库,upstream/master

    $ git branch -va
    # List all local and remote-tracking branches
    # * master                  a422352 My local commit
    #   remotes/origin/HEAD     -> origin/master
    #   remotes/origin/master   a422352 My local commit
    #   remotes/upstream/master 5fdff0f Some upstream commit

合并

现在我们已经拉取了上游仓库,我们将要合并其变更到我们的本地分支。这将使该分支与上游同步,而不会失去我们的本地更改。

    $ git checkout master
    # Check out our local master branch
    # Switched to branch 'master'

    $ git merge upstream/master
    # Merge upstream's master into our own
    # Updating a422352..5fdff0f
    # Fast-forward
    #  README                    |    9 -------
    #  README.md                 |    7 ++++++
    #  2 files changed, 7 insertions(+), 9 deletions(-)
    #  delete mode 100644 README
    #  create mode 100644 README.md

如果您的本地分支没有任何独特的提交,Git会改为执行“fast-forward”。

    $ git merge upstream/master
    # Updating 34e91da..16c56ad
    # Fast-forward
    #  README.md                 |    5 +++--
    #  1 file changed, 3 insertions(+), 2 deletions(-)

最后将本地变更推送到远程服务器即可。

参考资料



blog comments powered by Disqus

Published

2016-02-18

Categories


Tags