Make an existing Git branch track a remote branch?

Given a branch foo and a remote upstream:

As of Git 1.8.0:

git branch -u upstream/foo

Or, if local branch foo is not the current branch:

git branch -u upstream/foo foo

Or, if you like to type longer commands, these are equivalent to the above two:

git branch --set-upstream-to=upstream/foo

git branch --set-upstream-to=upstream/foo foo

As of Git 1.7.0:

git branch --set-upstream foo upstream/foo

Notes:

All of the above commands will cause local branch foo to track remote branch foo from remote upstream. The old (1.7.x) syntax is deprecated in favor of the new (1.8+) syntax. The new syntax is intended to be more intuitive and easier to remember.

官方文档:git-branch(1) Manual Page

-u <upstream>

--set-upstream-to=<upstream>

Set up <branchname>'s tracking information

so <upstream> is considered <branchname>'s upstream branch.

If no <branchname> is specified, then it defaults to the current branch.

假定,需要把foo分支,和远端的foo分支进行映射,远端名称为upstream

如果当foo分支不是当前分支,使用如下命令

git branch -u upstream/foo foo

如果foo分支是当前分支

git branch -u upstream/foo

==============================================================================

在checkout分支的时候,就进行映射

he ProGit book has a very good explanation:

Tracking Branches

Checking out a local branch from a remote branch automatically creates what is called a tracking branch. Tracking branches are local branches that have a direct relationship to a remote branch. If you’re on a tracking branch and type git push, Git automatically knows which server and branch to push to. Also, running git pull while on one of these branches fetches all the remote references and then automatically merges in the corresponding remote branch.

When you clone a repository, it generally automatically creates a master branch that tracks origin/master. That’s why git push and git pull work out of the box with no other arguments. However, you can set up other tracking branches if you wish — ones that don’t track branches on origin and don’t track the master branch. The simple case is the example you just saw, running git checkout -b [branch] [remotename]/[branch]. If you have Git version 1.6.2 or later, you can also use the --track shorthand:

$ git checkout --track origin/serverfix
Branch serverfix set up to track remote branch refs/remotes/origin/serverfix.
Switched to a new branch "serverfix"

To set up a local branch with a different name than the remote branch, you can easily use the first version with a different local branch name:

$ git checkout -b sf origin/serverfix
Branch sf set up to track remote branch refs/remotes/origin/serverfix.
Switched to a new branch "sf"

Now, your local branch sf will automatically push to and pull from origin/serverfix.

查看本地分支和远端分支的映射情况git branch -vv

查看映射

Administrator@LuJunTao MINGW64 /d/SourceCode/GameSourceCode/Hearthstone-Deck-Tracker (chucklu_zhCN)
$ git branch -vv
chucklu_master 8739b35 [chucklu/master] v0.10.16 (mainwindow topmost fix)
* chucklu_zhCN d5dbcc8 1.修改默认语言为中文zhCN
master 8739b35 [epix37/master] v0.10.16 (mainwindow topmost fix)
ui-translation cade481 [epix37/ui-translation] a few more resx entries

发现chucklu_zhCN分支没有和远端建立映射关系

添加映射

Administrator@LuJunTao MINGW64 /d/SourceCode/GameSourceCode/Hearthstone-Deck-Tracker (chucklu_zhCN)
$ git branch -u chucklu/chucklu_zhCN
Branch chucklu_zhCN set up to track remote branch chucklu_zhCN from chucklu.

再次查看映射

Administrator@LuJunTao MINGW64 /d/SourceCode/GameSourceCode/Hearthstone-Deck-Tracker (chucklu_zhCN)
$ git branch -vv
chucklu_master 8739b35 [chucklu/master] v0.10.16 (mainwindow topmost fix)
* chucklu_zhCN d5dbcc8 [chucklu/chucklu_zhCN: ahead 1] 1.修改默认语言为中文zhCN     //可以发现这个已经映射成功了
master 8739b35 [epix37/master] v0.10.16 (mainwindow topmost fix)
ui-translation cade481 [epix37/ui-translation] a few more resx entries

remove the remote tracking branch

https://stackoverflow.com/questions/3046436/how-do-you-stop-tracking-a-remote-branch-in-git

To remove the upstream for the current branch do:

$ git branch --unset-upstream

This is available for Git v.1.8.0 or newer.

git将本地已经存在的分支和一个指定的远端分支建立映射关系的更多相关文章

  1. git 在本地备份与指定不需要管理文件

    git 在本地备份 备份文件夹操作 在本地备份文件夹克隆一个不带工作区的仓库: 哑协议: git clone --bare <workspace>/.git yourwork.git gi ...

  2. 【git】本地git bash连接远程库github

    重要参考: https://www.liaoxuefeng.com/wiki/896043488029600 https://segmentfault.com/a/1190000003728094 正 ...

  3. git查看本地和创建分支、上传分支、提交代码到分支、删除分支等,git分支、git查看本地和创建分支以及上传分支到服务器

    以下是git命令行里边的命令操作 ##进入项目目录下 giscafer@Faronsince2016 /G/002_project $ cd Comments ##查看远程分支有哪些 giscafer ...

  4. 关于git新建本地分支与远程分支关联问题

    背景 新建本地分支并推送到远端后,当前分支没有与远端分支关联,每次推送都需要填写一堆信息. 操作 git branch --set-upstream-to=origin/20160928 切换到本地分 ...

  5. git推送本地分支到远端 以及删除远端分支的 命令

    git推送本地分支到远端 当前处于master分支,尝试用了git push origin warning: push.default is unset; its implicit value is ...

  6. Git新建本地分支与远程分支关联问题:git branch --set-upstream

    Git新建本地分支与远程分支关联问题:git branch --set-upstream git在本地新建分支, push到remote服务器上之后,再次pull下来的时候,如果不做处理会报以下提示: ...

  7. git 删除本地分支和远程分支、本地代码回滚和远程代码库回滚

    [git 删除本地分支] git branch -D br [git 删除远程分支] git push origin :br  (origin 后面有空格) git代码库回滚: 指的是将代码库某分支退 ...

  8. git删除本地分支

    远端master分支有更新需要拉取至本地,但是代码有些地方做了修改导致了小冲突,但是这些修改又是无关紧要的,于是就打算直接删除掉本地分支再重新拉取master分支,过程如下: //查看本地分支 git ...

  9. git在本地向远程仓库创建分支

    在本地的仓库种,如果想给upstream创建新分支并关联,需要执行 git push -u/--set-upstream 远程仓库名 远程分支名

随机推荐

  1. UVA12470—Tribonacci (类似斐波那契,简单题)

    题目链接:https://vjudge.net/problem/UVA-12470 题目意思:我们都知道斐波那契数列F[i]=F[i-1]+F[i-2],现在我们要算这样的一个式子T[i]=T[i-1 ...

  2. 经常会碰到css的bug

    1.a标签做为空的时候,只做链接的时候,ie是无法点击链接. a{background:url(about:blank);} ;filter:alpha(opacity=0);} 2.给导航做下拉菜单 ...

  3. python基础-第七篇-7.4异常处理

    异常基础: 异常处理首先要捕获异常,不让程序中断,也不让错误信息直接呈现出来,然后就是你该怎么处理异常,以什么方式显示 try: pass except Exception,ex: pass 在需要用 ...

  4. IIS7.5 错误代码0x8007007e HTTP 错误 500.19

    目录 问题案例 解决问题 参考资料 问题案例 今天在win2008+IIS7.5的环境中部署WCF服务后,一直出现无法打开的页面.具体错误信息如下: HTTP 错误 500.19 - Internal ...

  5. JavaWeb-Servlet开发2

    ---恢复内容开始--- ServletConfig 配置Servlet初始化参数 在Servlet的配置文件web.xml中,可以使用一个或多个<init-param>标签为servle ...

  6. docker部署oracle

    oracle部署 创建oracle用户 [root@stpass-15 ~]# useradd oracle[root@stpass-15 oracle]# cd oracle [root@stpas ...

  7. 005-maven坐标和依赖

    1.何为Maven坐标 groupId.artifactId.version.packaging.classifier 中央仓库:http://repol.maven.org/maven22.坐标详解 ...

  8. linux的虚拟机时间和物理机时间同步

    1.查看和设置物理机硬件时间 查看硬件时间# hwclock --show或# clock --show 设置硬件时间# hwclock --set --date=”01/14/19 10:19″(月 ...

  9. DataGird 相关

    DataGird控件          DataGirdView 控件    DataGird类  他们之间是什么关系??????? DataGridView 控件是替换 DataGrid 控件的新控 ...

  10. atheros无线驱动之:数据接收流程

    1:数据处理函数tasklet,workqueue在之前的初始化代码中的函数__ath_attach()中,有如下的代码: #ifndef ATH_SUPPORT_HTC#ifdef ADF_SUPP ...