让现有的 Git 分支跟踪远程分支? [英] Make an existing Git branch track a remote branch?

查看:42
本文介绍了让现有的 Git 分支跟踪远程分支?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何创建一个跟踪远程分支的新分支,但是如何使现有分支跟踪远程分支?

I know how to make a new branch that tracks remote branches, but how do I make an existing branch track a remote branch?

我知道我可以只编辑 .git/config 文件,但似乎应该有更简单的方法.

I know I can just edit the .git/config file, but it seems there should be an easier way.

推荐答案

给定一个分支 foo 和一个远程 upstream:

Given a branch foo and a remote upstream:

从 Git 1.8.0 开始:

git branch -u upstream/foo

或者,如果本地分支 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

从 Git 1.7.0 开始(1.8.0 之前):

git branch --set-upstream foo upstream/foo

注意事项:

  • 以上所有命令都会导致本地分支foo从远程upstream跟踪远程分支foo.
  • 旧的 (1.7.x) 语法已被弃用,取而代之的是新的 (1.8+) 语法.新语法旨在更直观、更容易记住.
  • 在针对尚未获取的新创建的远程节点运行时,定义上游分支将失败.在这种情况下,请预先运行 git fetch upstream.
  • 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.
  • Defining an upstream branch will fail when run against newly-created remotes that have not already been fetched. In that case, run git fetch upstream beforehand.

另见:为什么我需要一直做`--set-upstream`?

这篇关于让现有的 Git 分支跟踪远程分支?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆