Git远程03:分支的upstream

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

upstream的设置

基本设置

1
$ git branch --set-upstream-to=origin/dev


1
git branch -u origin/dev

此命令的含义是,是指当前分支的upstream为origin远程仓库的dev分支。

在推送的同时,同时设置upstream

1
$ git push -u origin master

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

不切换分支直接设置其他分支的upstream

1
$ git br -u origin/br01-remote br01

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

1
$ git push -u origin br03:br03

取消upstream

取消当前分支的upstream

1
$ git branch --unset-upstream

取消其他分支的upstream

1
$ git branch --unset-upstream [分支名]

查看upstream

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
$ 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信息对应起来。

也可以通过git remote show查看。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
$ 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跟踪分支。

如果本文对你有所帮助,请小额赞助
~~ EOF ~~