让现有的 Git 分支跟踪远程分支?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/520650/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 06:08:59  来源:igfitidea点击:

Make an existing Git branch track a remote branch?

gitbranchgit-branch

提问by Pat Notz

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

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

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

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

回答by Dan Moulding

Given a branch fooand a remote upstream:

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

As of Git 1.8.0:

从 Git 1.8.0 开始:

git branch -u upstream/foo

Or, if local branch foois not the current branch:

或者,如果本地分支foo不是当前分支:

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 1.7.0 开始:

git branch --set-upstream foo upstream/foo

Notes:

笔记:

  • All of the above commands will cause local branch footo track remote branch foofrom 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 upstreambeforehand.
  • 以上所有命令都会导致本地foo分支foo从 remote跟踪远程分支upstream
  • 旧的 (1.7.x) 语法已被弃用,取而代之的是新的 (1.8+) 语法。新语法旨在更直观、更容易记住。
  • 当针对尚未获取的新创建的远程运行时,定义上游分支将失败。在这种情况下,请git fetch upstream提前运行。


See also: Why do I need to do `--set-upstream` all the time?

另请参阅:为什么我需要一直执行`--set-upstream`?

回答by Paul Hedderly

You can do the following (assuming you are checked out on master and want to push to a remote branch master):

您可以执行以下操作(假设您已在 master 上签出并想要推送到远程分支 master):

Set up the 'remote' if you don't have it already

如果您还没有“遥控器”,请设置它

git remote add origin ssh://...

Now configure master to know to track:

现在配置 master 知道跟踪:

git config branch.master.remote origin
git config branch.master.merge refs/heads/master

And push:

并推:

git push origin master

回答by Greg Bacon

I do this as a side-effect of pushing with the -uoption as in

我这样做是作为推动-u选项的副作用,如

$ git push -u origin branch-name

The equivalent long option is --set-upstream.

等效的 long 选项是--set-upstream.

The git-branchcommand also understands --set-upstream, but its use can be confusing. Version 1.8.0modifies the interface.

git-branch命令也理解--set-upstream,但它的使用可能会令人困惑。1.8.0 版修改了界面。

git branch --set-upstreamis deprecated and may be removed in a relatively distant future. git branch [-u|--set-upstream-to]has been introduced with a saner order of arguments.

It was tempting to say git branch --set-upstream origin/master, but that tells Git to arrange the local branch "origin/master" to integrate with the currently checked out branch, which is highly unlikely what the user meant. The option is deprecated; use the new --set-upstream-to(with a short-and-sweet -u) option instead.

git branch --set-upstream已弃用,可能会在相对遥远的将来被删除。 git branch [-u|--set-upstream-to]引入了更合理的参数顺序。

很想说git branch --set-upstream origin/master,但这告诉 Git 安排本地分支“origin/master”与当前签出的分支集成,这极不可能是用户的意思。该选项已弃用;改用新的--set-upstream-to(带有短而甜的-u)选项。

Say you have a local foobranch and want it to treat the branch by the same name as its upstream. Make this happen with

假设您有一个本地foo分支,并希望它以与其上游相同的名称处理该分支。使这发生

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

or just

要不就

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

回答by James Mead

You might find the git_remote_branchtool useful. It offers simple commands for creating, publishing, deleting, tracking & renaming remote branches. One nice feature is that you can ask a grbcommand to explain what git commands it would execute.

您可能会发现该git_remote_branch工具很有用。它提供了用于创建、发布、删除、跟踪和重命名远程分支的简单命令。一个很好的功能是你可以要求一个grb命令来解释它将执行哪些 git 命令。

grb explain create my_branch github
# git_remote_branch version 0.3.0

# List of operations to do to create a new remote branch and track it locally:
git push github master:refs/heads/my_branch
git fetch github
git branch --track my_branch github/my_branch
git checkout my_branch

回答by Hedgehog

Actually for the accepted answer to work:

实际上对于接受的答案起作用:

git remote add upstream <remote-url>
git fetch upstream
git branch -f --track qa upstream/qa
# OR:
git branch --set-upstream qa upstream/qa

回答by wu-lee

I believe that in as early as Git 1.5.x you could make a local branch $BRANCHtrack a remote branch origin/$BRANCH, like this.

我相信早在 Git 1.5.x 中,您就可以像这样让本地分支$BRANCH跟踪远程分支origin/$BRANCH

Given that $BRANCHand origin/$BRANCHexist, and you've not currently checked out $BRANCH(switch away if you have), do:

鉴于存在$BRANCHorigin/$BRANCH存在,并且您目前尚未结帐$BRANCH(如果有,请切换),请执行以下操作:

git branch -f --track $BRANCH origin/$BRANCH

This recreates $BRANCHas a tracking branch. The -fforces the creation despite $BRANCHexisting already. --trackis optional if the usual defaults are in place (that is, the git-config parameter branch.autosetupmergeis true).

这将重新创建$BRANCH为跟踪分支。该-f部队的创建,尽管$BRANCH现有的已经。--track如果有通常的默认值(即 git-config 参数branch.autosetupmerge为真),则是可选的。

Note, if origin/$BRANCHdoesn't exist yet, you can create it by pushing your local $BRANCHinto the remote repository with:

请注意,如果origin/$BRANCH尚不存在,您可以通过将本地推$BRANCH送到远程存储库来创建它:

git push origin $BRANCH

Followed by the previous command to promote the local branch into a tracking branch.

紧接着前面的命令将本地分支提升为跟踪分支。

回答by Monsif EL AISSOUSSI

1- update your local meta-data using : git fetch --all

1- 使用git fetch --all更新您的本地元数据

enter image description here

在此处输入图片说明

2- show your remote and local branches using : git branch -a, see the following Screenshot

2- 使用git branch -a显示您的远程和本地分支 ,请参阅以下屏幕截图

enter image description here

在此处输入图片说明

3- switch to target branch , that you want to linked with the remote: using

3-切换到要与远程链接的目标分支:使用

git checkout branchName

git checkout 分支名称

example :

例子 :

enter image description here

在此处输入图片说明

4- Link your local branch to a remote branch using:

4- 使用以下方法将您的本地分支链接到远程分支:

git branch --set-upstream-to nameOfRemoteBranch

git branch --set-upstream-to nameOfRemoteBranch

N.B : nameOfRemoteBranch: to copy from the output of step 2 " git branch -r "

注意:nameOfRemoteBranch:从第 2 步“git branch -r”的输出中复制

Example of use:

使用示例:

enter image description here

在此处输入图片说明

回答by romanlv

Make sure you run :

确保你运行:

git config push.default tracking

to be able to push trouble free

能够顺利推进

回答by mipadi

Editing .git/configis probably the easiest and fastest way. That's what the Git commands for handling remote branches are doing, anyway.

编辑.git/config可能是最简单、最快捷的方式。无论如何,这就是用于处理远程分支的 Git 命令所做的。

If you don't want to muck with the file by hand (and it's not that hard to do), you can always use git configto do it...but again, that's just going to edit the .git/configfile, anyway.

如果您不想手动处理文件(这并不难),您可以随时使用git config它来做...但同样.git/config,无论如何,这只是编辑文件。

There are, of course, ways to automatically track a remote branch when using git checkout(by passing the --trackflag, for example), but these commands work with newbranches, not existing ones.

当然,有一些方法可以在使用时自动跟踪远程分支git checkout--track例如,通过传递标志),但这些命令适用于分支,而不是现有分支。

回答by MadNik

In very short

简而言之

git branch --set-upstream yourLocalBranchName origin/develop

This will make your yourLocalBranchNametrack the remote branch called develop.

这将使您的yourLocalBranchName轨道成为名为develop.