Git远程03:分支的upstream - higoge/blog GitHub Wiki

00 一个分支的upstream,其实就是与远程分支做关联,告诉git,默认此分支推送及拉取的远程分支信息。 #### upstream的设置

  • 基本设置
$ git branch --set-upstream-to=origin/dev

git branch -u origin/dev
  • 在推送的同时,同时设置upstream
$ git push -u origin master

命令的含义是,推送master分支到远程origin仓库master分支,并且建立本地分支master的upstream为origin/master。(关于git push更详细的解释,请参考第04节)

  • 不切换分支直接设置其他分支的upstream
$ git br -u origin/br01-remote br01

设置本地分支br01的upstream为origin/br01-remote。 或者push的时候直接设置。

$ git push -u origin br03:br03

取消upstream

  • 取消当前分支的upstream
$ git branch --unset-upstream
  • 取消其他分支的upstream
$ git branch --unset-upstream [分支名]

查看upstream

查看upstream信息,主要是查看仓库目录下.git/config文件。

$ cat .git/config
[core]
        repositoryformatversion = 0
        filemode = true
        bare = false
        logallrefupdates = true
[remote "origin"]
        url = git@github0123:jeremy0123/fetch.git
        fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
        remote = origin
        merge = refs/heads/master
[branch "br01"]
        remote = origin
        merge = refs/heads/br01-remote
[branch "br03"]
        remote = origin
        merge = refs/heads/br03

其中[branch "分支名"]下的信息就是upstream信息,remote项表示upstream的远程仓库名,merge项表示远程跟踪分支名。 另外,config中[remote "远程仓库名"]下的url和fetch需要注意下,这些信息可以和第02节的clone信息对应起来。

也可以通过remote show查看。

$ git remote show origin
* remote origin
  Fetch URL: git@github0123:jeremy0123/fetch.git
  Push  URL: git@github0123:jeremy0123/fetch.git
  HEAD branch: master
  Remote branches:
    br01-remote tracked
    br03        tracked
    master      tracked
  Local branches configured for 'git pull':
    br01   merges with remote br01-remote
    br03   merges with remote br03
    master merges with remote master
  Local refs configured for 'git push':
    br03   pushes to br03   (up to date)
    master pushes to master (up to date)

Remote branches表示远程仓库的分支,git pull表示upstream跟踪分支。