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

发布于 2024-07-13 10:44:12 字数 122 浏览 7 评论 0原文

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

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

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/config file, but it seems there should be an easier way.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(23

忘你却要生生世世 2024-07-20 10:44:12

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

从 Git 1.8.0 开始:

git branch -u upstream/foo

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

git branch -u upstream/foo foo

或者,如果您喜欢输入更长的命令,这些相当于上面两个:

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

注释:< /strong>

  • 所有上述命令都会导致本地分支 foo 跟踪来自远程 upstream 的远程分支 foo
  • 旧的 (1.7.x) 语法已被弃用,取而代之的是新的 (1.8+) 语法。 新语法旨在更直观、更容易记住。
  • 当针对尚未获取的新创建的远程运行时,定义上游分支将失败。 在这种情况下,请提前运行 git fetch uploader。

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

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 (before 1.8.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.
  • 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.

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

岁月蹉跎了容颜 2024-07-20 10:44:12

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

如果您还没有“远程”,请设置“远程”

git remote add origin ssh://...

现在配置 master 以了解跟踪:

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

并推送:

git push origin master

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

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

git remote add origin ssh://...

Now configure master to know to track:

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

And push:

git push origin master
缱绻入梦 2024-07-20 10:44:12

我这样做是使用 -u 选项进行推送的副作用,如

$ git push -u origin branch-name

等效的长选项是 --set-upstream 中所示。

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

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

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

假设您签出了本地 foo 分支,并希望它使用与其上游同名的分支。 让这一切发生

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

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

$ git push -u origin branch-name

The equivalent long option is --set-upstream.

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

git branch --set-upstream is 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 to be what the user meant. The option is deprecated; use the new --set-upstream-to (with a short-and-sweet -u) option instead.

Say you have a local foo branch checked out and want it to use a branch by the same name as its upstream. Make this happen with

$ git branch --set-upstream-to=origin/foo
蓝颜夕 2024-07-20 10:44:12

对于 Git 版本 1.8.0 及更高版本:

实际上要使接受的答案起作用:

git remote add upstream <remote-url>
git fetch upstream
git branch -f --track qa upstream/qa
# OR Git version 1.8.0 and higher:
git branch --set-upstream-to=upstream/qa
# Gitversions lower than 1.8.0
git branch --set-upstream qa upstream/qa

For Git versions 1.8.0 and higher:

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 version 1.8.0 and higher:
git branch --set-upstream-to=upstream/qa
# Gitversions lower than 1.8.0
git branch --set-upstream qa upstream/qa
╰沐子 2024-07-20 10:44:12

您可能会发现 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

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

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
预谋 2024-07-20 10:44:12

1-使用以下命令更新本地元数据: git fetch --all

在此处输入图像描述

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

在此处输入图像描述

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

git checkoutbranchName

示例:

在此处输入图像描述

4-使用以下命令将本地分支链接到远程分支:

gitbranch --set-upstream-to nameOfRemoteBranch

注意:nameOfRemoteBranch:从步骤 2 的输出复制“gitbranch - r "

使用示例:

在此处输入图像描述

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

enter image description here

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

enter image description here

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

git checkout branchName

example :

enter image description here

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

git branch --set-upstream-to nameOfRemoteBranch

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

Example of use:

enter image description here

沉溺在你眼里的海 2024-07-20 10:44:12

我相信早在 Git 1.5.x 中,您就可以让本地分支 $BRANCH 跟踪远程分支 origin/$BRANCH,如下所示。

鉴于 $BRANCHorigin/$BRANCH 存在,并且您当前尚未签出 $BRANCH(如果有,请移开), do:

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

这将重新创建 $BRANCH 作为跟踪分支。 尽管 $BRANCH 已经存在,-f 仍会强制创建。 如果通常的默认值已到位(即 git-config 参数 branch.autosetupmerge 为 true),则 --track 是可选的。

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

git push origin $BRANCH

后跟上一个命令以升级将本地分支转换为跟踪分支。

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

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

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

This recreates $BRANCH as a tracking branch. The -f forces the creation despite $BRANCH existing already. --track is optional if the usual defaults are in place (that is, the git-config parameter branch.autosetupmerge is true).

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

git push origin $BRANCH

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

绻影浮沉 2024-07-20 10:44:12

确保你跑步:

git config push.default tracking

以便能够无忧无虑地推动

Make sure you run :

git config push.default tracking

to be able to push trouble free

随梦而飞# 2024-07-20 10:44:12

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

如果你不想手动修改文件(而且这并不难),你可以随时使用 git config 来完成它......但同样,这只是要编辑无论如何,.git/config 文件。

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

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

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 config to do it...but again, that's just going to edit the .git/config file, anyway.

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

揽清风入怀 2024-07-20 10:44:12

简而言之,

git branch --set-upstream yourLocalBranchName origin/develop

这将使您的 yourLocalBranchName 跟踪名为 develop 的远程分支。

In very short

git branch --set-upstream yourLocalBranchName origin/develop

This will make your yourLocalBranchName track the remote branch called develop.

鲜血染红嫁衣 2024-07-20 10:44:12

对于 1.6.x,可以使用 git_remote_branch 工具来完成:

grb track foo upstream

这将导致 Git 生成 foo 跟踪 upstream/foo

For 1.6.x, it can be done using the git_remote_branch tool:

grb track foo upstream

That will cause Git to make foo track upstream/foo.

如此安好 2024-07-20 10:44:12

我使用以下命令(假设您的本地分支名称为“branch-name-local”,远程分支名称为“branch-name-remote”):

$ git branch --set-upstream-to=origin/branch-name-remote branch-name-local

如果本地和远程分支名称相同,则只需执行以下操作:

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

I use the following command (Suppose your local branch name is "branch-name-local" and remote branch name is "branch-name-remote"):

$ git branch --set-upstream-to=origin/branch-name-remote branch-name-local

If both local and remote branches have the same name, then just do the following:

$ git branch --set-upstream-to=origin/branch-name branch-name
一江春梦 2024-07-20 10:44:12

为了创建新分支,我们可以使用以下命令

 git checkout --track -b example origin/example 

For the already created branch to create link between remote then from that branch use below command

 git branch -u origin/remote-branch-name

For creating new branch, we could use following command

 git checkout --track -b example origin/example 

For the already created branch to create link between remote then from that branch use below command

 git branch -u origin/remote-branch-name

流星番茄 2024-07-20 10:44:12

使用“--track”选项

  • git pull之后:

    git checkout --track

  • 或者:

    git fetch && git checkout <分支名称>

Use '--track' Option

  • After a git pull :

    git checkout --track <remote-branch-name>

  • Or:

    git fetch && git checkout <branch-name>

别闹i 2024-07-20 10:44:12

在这里,使用 githubgit version 2.1.4,只需执行以下操作:

$ git clone [email protected]:user/repo.git

遥控器来自 itelsef,即使没有本地链接:

$ git remote show origin

* remote origin
  Fetch URL: [email protected]:user/repo.git
  Push  URL: [email protected]:user/repo.git
  HEAD branch: master
  Remote branches:
    develop tracked         <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    master  tracked
  Local branch configured for 'git pull':
    master merges with remote master
  Local ref configured for 'git push':
    master pushes to master (up to date)

但当然,仍然没有本地分支:

$ git branch
* master                  <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

请参阅? 现在,如果您只需签出 develp,它就会自动发挥作用:

$ git checkout develop
Branch develop set up to track remote branch develop from origin.
Switched to a new branch 'develop'

太简单了!


摘要。只需运行这 2 个命令:

$ git clone [email protected]:user/repo.git
$ git checkout develop

Here, using github and git version 2.1.4, just do:

$ git clone [email protected]:user/repo.git

And remotes come by itelsef, even if not linked locally:

$ git remote show origin

* remote origin
  Fetch URL: [email protected]:user/repo.git
  Push  URL: [email protected]:user/repo.git
  HEAD branch: master
  Remote branches:
    develop tracked         <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    master  tracked
  Local branch configured for 'git pull':
    master merges with remote master
  Local ref configured for 'git push':
    master pushes to master (up to date)

But of course, still no local branch:

$ git branch
* master                  <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

See? Now if you just checkout develp, it will do the magic automatically:

$ git checkout develop
Branch develop set up to track remote branch develop from origin.
Switched to a new branch 'develop'

So easy!


Summary. Just run this 2 commands:

$ git clone [email protected]:user/repo.git
$ git checkout develop
绮烟 2024-07-20 10:44:12

对于 git 版本 2.25.1,请使用以下命令:

git push --set-upstream origin <local_branch_name>

For git version 2.25.1, use the command:

git push --set-upstream origin <local_branch_name>
审判长 2024-07-20 10:44:12

如果运行后出现“错误:请求的上游分支 'origin/foo' 不存在”

git branch -u origin/foo

  1. 确保 origin 确实有 foo 分支。

  2. 确保remote.origin.fetch变量设置为+refs/heads/*:refs/remotes/origin/*

$ git config remote.origin.fetch '+refs/heads/*:refs/remotes/origin/*'
$ git config --get remote.origin.fetch
+refs/heads/*:refs/remotes/origin/*
  1. 运行 git fetch -v< /代码>。 您应该看到 git 从 origin/foo 更新:
$ git fetch -v
From github.com:davidhcefx/test
 * [new branch]      foo             -> origin/foo
 = [up to date]      master          -> origin/master
  1. 成功后,git Branch -avv 将显示方括号,指示跟踪的远程分支:
$ git branch -u origin/foo
branch 'foo' set up to track 'origin/foo'.
$ git branch -avv
* foo                            92c5ada [origin/foo] Initial commit
  master                         92c5ada [origin/master] Initial commit

In case you got "error: the requested upstream branch 'origin/foo' does not exist" after running:

git branch -u origin/foo

  1. Make sure origin does have a foo branch.

  2. Make sure the remote.origin.fetch variable is set to +refs/heads/*:refs/remotes/origin/*:

$ git config remote.origin.fetch '+refs/heads/*:refs/remotes/origin/*'
$ git config --get remote.origin.fetch
+refs/heads/*:refs/remotes/origin/*
  1. Run git fetch -v. You should see git updating from origin/foo:
$ git fetch -v
From github.com:davidhcefx/test
 * [new branch]      foo             -> origin/foo
 = [up to date]      master          -> origin/master
  1. Upon success, git branch -avv will show square brackets, indicating tracked remote branches:
$ git branch -u origin/foo
branch 'foo' set up to track 'origin/foo'.
$ git branch -avv
* foo                            92c5ada [origin/foo] Initial commit
  master                         92c5ada [origin/master] Initial commit
难以启齿的温柔 2024-07-20 10:44:12

这不是这个问题的直接答案,但我想在这里为那些在尝试配置上游分支时可能遇到与我相同问题的人留下一条注释。

警惕push.default

对于较旧的 git 版本,默认值是匹配,如果您有以下情况,这将导致非常不良的行为:

本地分支“master”跟踪到 origin/master

远程分支“上游”跟踪到upstream/master

如果你在“upstream”分支上尝试“git push”,并且 push.default 匹配,git 会自动尝试将本地分支“master”合并到“upstream/master”,从而导致整个分支崩溃。很多混乱。

这提供了更理智的行为:

git config --global push.default upper

This isn't a direct answer to this question, but I wanted to leave a note here for anyone who may be having the same issue as me when trying to configure an upstream branch.

Be wary of push.default.

With older git versions, the default was matching, which would cause very undesirable behaviour if you have, for example:

Local branch "master" tracking to origin/master

Remote branch "upstream" tracking to upstream/master

If you tried to "git push" when on the "upstream" branch, with push.default matching git would automatically try to merge the local branch "master" into "upstream/master", causing a whole lot of chaos.

This gives more sane behaviour:

git config --global push.default upstream

Smile简单爱 2024-07-20 10:44:12

以某种相关的方式,我试图将远程跟踪分支添加到现有分支,但无法访问我想要添加该远程跟踪分支的系统上的远程存储库(因为我经常导出此副本)通过sneakernet回购到另一个有权推送到该远程的系统)。 我发现没有办法强制在本地添加尚未获取的远程分支(因此本地不知道该分支存在于远程上,我会收到错误:请求的上游分支“origin/remotebranchname”不存在)。

最后,我通过在 .git/refs/remotes/origin/remotebranchname 添加新的头文件,然后复制引用(目测是最快,虽然很蹩脚;-)从可以访问原始存储库的系统到工作站(使用我在其中添加远程分支的本地存储库)。

完成后,我就可以使用 gitbranch --set-upstream-to=origin/remotebranchname

In a somewhat related way I was trying to add a remote tracking branch to an existing branch, but did not have access to that remote repository on the system where I wanted to add that remote tracking branch on (because I frequently export a copy of this repo via sneakernet to another system that has the access to push to that remote). I found that there was no way to force adding a remote branch on the local that hadn't been fetched yet (so local did not know that the branch existed on the remote and I would get the error: the requested upstream branch 'origin/remotebranchname' does not exist).

In the end I managed to add the new, previously unknown remote branch (without fetching) by adding a new head file at .git/refs/remotes/origin/remotebranchname and then copying the ref (eyeballing was quickest, lame as it was ;-) from the system with access to the origin repo to the workstation (with the local repo where I was adding the remote branch on).

Once that was done, I could then use git branch --set-upstream-to=origin/remotebranchname

弥枳 2024-07-20 10:44:12

或者简单地通过:

如果您还没有在分支中,则切换到分支:

[za]$ git checkout branch_name

运行

[za]$ git branch --set-upstream origin branch_name
Branch origin set up to track local branch brnach_name by rebasing.

并准备好:

 [za]$ git push origin branch_name

您可以alawys查看配置文件,以查看运行时跟踪的内容:

 [za]$ git config -e

知道这一点也很高兴,它显示哪些分支被跟踪,哪些分支不被跟踪。 :

  [za]$ git remote show origin 

or simply by :

switch to the branch if you are not in it already:

[za]$ git checkout branch_name

run

[za]$ git branch --set-upstream origin branch_name
Branch origin set up to track local branch brnach_name by rebasing.

and you ready to :

 [za]$ git push origin branch_name

You can alawys take a look at the config file to see what is tracking what by running:

 [za]$ git config -e

It's also nice to know this, it shows which branches are tracked and which ones are not. :

  [za]$ git remote show origin 
时光瘦了 2024-07-20 10:44:12

对于像我这样只想将本地分支名称与远程分支名称同步的人,这里有一个方便的命令:

git branch -u origin/$(git rev-parse --abbrev-ref HEAD)

For anyone who, like me, just wants to sync up your local branch name with the remote branch name, here's a handy command:

git branch -u origin/$(git rev-parse --abbrev-ref HEAD)
假装爱人 2024-07-20 10:44:12

为了避免记住每次收到消息时需要执行的操作:

请指定您要合并的分支。 请参阅 git-pull(1)
了解详情。
......

您可以使用以下脚本,将您所在的当前分支设置为起源为上游

就我而言,我几乎从不 > 将除原点之外的其他内容设置为默认上游。 另外,我几乎总是为本地和远程分支保留相同的分支名称。 所以以下内容适合我:

#!/bin/bash
# scriptname: git-branch-set-originupstream
current_branch="$(git branch | grep -oP '(?<=^\* )(.*)
)"
upstream="origin/$current_branch"
git branch -u "$upstream"

To avoid remembering what you need to do each time you get the message:

Please specify which branch you want to merge with. See git-pull(1)
for details.
.....

You can use the following script which sets origin as upstream for the current branch you are in.

In my case I almost never set something else than origin as the default upstream. Also I almost always keep the same branch name for local and remote branch. So the following fits me:

#!/bin/bash
# scriptname: git-branch-set-originupstream
current_branch="$(git branch | grep -oP '(?<=^\* )(.*)
)"
upstream="origin/$current_branch"
git branch -u "$upstream"
盛夏已如深秋| 2024-07-20 10:44:12

这也行

git branch --set-upstream-to=/< remote>/< branch> < localbranch>

This would work too

git branch --set-upstream-to=/< remote>/< branch> < localbranch>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文