> For the complete documentation index, see [llms.txt](https://pshizhsysu.gitbook.io/git/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://pshizhsysu.gitbook.io/git/yuan-cheng-cang-ku/guan-4e8e60-set-upstream.md).

# 关于upstream

假设learngit是一个本地git仓库，那么当我们在learngit目录下执行git目录时，实际上此时是在某个分支下的（比如master分支）。那么当我们执行如下命令时：

```
$ git push
```

git会把当前分支的内容默认同步到origin远程仓库的同名分支。同理`git pull`命令会从origin的同名分支拉取代码。

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

## 基本设置

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

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

## 在push时设置upstream

```
$ git push -u origin master
```

命令的含义是，推送当前分支到远程origin仓库master分支，并且建立本地当前分支的upstream为origin/master

## 取消upstream

```
$ git branch --unset-upstream
```

该命令的意思是，取消本地仓库当前分支的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项表示远程跟踪分支名

## Reference

\[1] <https://higoge.github.io/2015/07/06/git-remote03/>
