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. HDU_5533_Dancing Stars on Me

    Dancing Stars on Me Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Ot ...

  2. c# winfrom实时获取斗鱼房间弹幕

    效果图如下: 通过webBrowser获取,时钟控件刷新弹幕,正则匹配数据,用第二个webBrowser显示弹幕内容.老话,并没完善.请自行完善.有个dll是用来屏蔽webBrowser的声音的,可能 ...

  3. Accuracy, Precision, Resolution & Sensitivity

    Instrument manufacturers usually supply specifications for their equipment that define its accuracy, ...

  4. vue-router路由懒加载

    正常配置 import Vue from 'vue' import Router from 'vue-router' import Login from '@/components/pages/log ...

  5. 拖拽js和jq写法

    第一种原生js写法 window.onload=function () { var oDrag=document.getElementById('drag'); oDrag.onmousedown=f ...

  6. 原!linux 监控 jar定时任务 挂了重启 脚本

    #!/bin/bash time=$(date "+%Y-%m-%d %H:%M:%S") echo "monitor start at: ${time}" P ...

  7. Django logging模块

    一.Django logging配置 1.在setting.py中配置 # 日志文件存放路径 BASE_LOG_DIR = os.path.join(BASE_DIR, "log" ...

  8. Matlab GUI memo

    有一段时间没写博客,一周4篇文章都坚持不下来,不知道写哪个方面的内容,写研究相关就怕论文查重查到,其他方面也没太多时间去学.还是花时间多学点其他方面.废话到此,很早就做过matlab gui相关,现在 ...

  9. jquery插件开发快速入门

    1.添加jQuery对象方法添加jQuery对象方法:jQuery.prototype.myMethod. 在jQuery源码中有一句:jQuery.fn = jQuery.prototype,也就是 ...

  10. 常用的系统架构 web服务器之iis,apache,tomcat三者之间的比较

    常用的系统架构是: Linux + Apache + PHP + MySQL Linux + Apache + Java (WebSphere) + Oracle Windows Server 200 ...