让现有的 Git 分支跟踪远程分支?
我知道如何创建一个跟踪远程分支的新分支,但是如何使现有分支跟踪远程分支?
我知道我可以只编辑 .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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(23)
给定一个分支
foo
和一个远程upstream
:从 Git 1.8.0 开始:
或者,如果是本地分支
foo
不是当前分支:或者,如果您喜欢输入更长的命令,这些相当于上面两个:
从 Git 1.7.0(1.8.0 之前)开始:
注释:< /strong>
foo
跟踪来自远程upstream
的远程分支foo
。另请参阅:为什么我需要一直执行`--set-upstream`?
Given a branch
foo
and a remoteupstream
:As of Git 1.8.0:
Or, if local branch
foo
is not the current branch:Or, if you like to type longer commands, these are equivalent to the above two:
As of Git 1.7.0 (before 1.8.0):
Notes:
foo
to track remote branchfoo
from remoteupstream
.git fetch upstream
beforehand.See also: Why do I need to do `--set-upstream` all the time?
您可以执行以下操作(假设您已在 master 上签出并想要推送到远程分支 master):
如果您还没有“远程”,请设置“远程”
现在配置 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
Now configure master to know to track:
And push:
我这样做是使用
-u
选项进行推送的副作用,如等效的长选项是
--set-upstream
中所示。git-branch
命令也理解--set-upstream
,但它的使用可能会令人困惑。 版本 1.8.0 修改了界面。假设您签出了本地
foo
分支,并希望它使用与其上游同名的分支。 让这一切发生I do this as a side-effect of pushing with the
-u
option as inThe 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.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 版本 1.8.0 及更高版本:
实际上要使接受的答案起作用:
For Git versions 1.8.0 and higher:
Actually for the accepted answer to work:
您可能会发现
git_remote_branch
工具很有用。 它提供了用于创建、发布、删除、跟踪和创建的简单命令。 重命名远程分支。 一个不错的功能是您可以询问 grb 命令来解释它将执行哪些 git 命令。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 agrb
command to explain what git commands it would execute.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
2- show your remote and local branches using : git branch -a
, see the following Screenshot
3- switch to target branch , that you want to linked with the remote: using
git checkout branchName
example :
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:
我相信早在 Git 1.5.x 中,您就可以让本地分支
$BRANCH
跟踪远程分支origin/$BRANCH
,如下所示。鉴于
$BRANCH
和origin/$BRANCH
存在,并且您当前尚未签出$BRANCH
(如果有,请移开), do:这将重新创建
$BRANCH
作为跟踪分支。 尽管$BRANCH
已经存在,-f
仍会强制创建。 如果通常的默认值已到位(即 git-config 参数branch.autosetupmerge
为 true),则--track
是可选的。请注意,如果
origin/$BRANCH
尚不存在,您可以通过将本地$BRANCH
推送到远程存储库来创建它:后跟上一个命令以升级将本地分支转换为跟踪分支。
I believe that in as early as Git 1.5.x you could make a local branch
$BRANCH
track a remote branchorigin/$BRANCH
, like this.Given that
$BRANCH
andorigin/$BRANCH
exist, and you've not currently checked out$BRANCH
(switch away if you have), do: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 parameterbranch.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:Followed by the previous command to promote the local branch into a tracking branch.
确保你跑步:
以便能够无忧无虑地推动
Make sure you run :
to be able to push trouble free
编辑
.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.简而言之,
这将使您的
yourLocalBranchName
跟踪名为develop
的远程分支。In very short
This will make your
yourLocalBranchName
track the remote branch calleddevelop
.对于 1.6.x,可以使用 git_remote_branch 工具来完成:
这将导致 Git 生成
foo
跟踪upstream/foo
。For 1.6.x, it can be done using the git_remote_branch tool:
That will cause Git to make
foo
trackupstream/foo
.我使用以下命令(假设您的本地分支名称为“branch-name-local”,远程分支名称为“branch-name-remote”):
如果本地和远程分支名称相同,则只需执行以下操作:
I use the following command (Suppose your local branch name is "branch-name-local" and remote branch name is "branch-name-remote"):
If both local and remote branches have the same name, then just do the following:
为了创建新分支,我们可以使用以下命令
For the already created branch to create link between remote then from that branch use below command
For creating new branch, we could use following command
For the already created branch to create link between remote then from that branch use below command
使用“--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>
在这里,使用
github
和git version 2.1.4
,只需执行以下操作:遥控器来自 itelsef,即使没有本地链接:
但当然,仍然没有本地分支:
请参阅? 现在,如果您只需签出 develp,它就会自动发挥作用:
太简单了!
摘要。只需运行这 2 个命令:
Here, using
github
andgit version 2.1.4
, just do:And remotes come by itelsef, even if not linked locally:
But of course, still no local branch:
See? Now if you just checkout develp, it will do the magic automatically:
So easy!
Summary. Just run this 2 commands:
对于 git 版本
2.25.1
,请使用以下命令:For git version
2.25.1
, use the command:如果运行后出现“错误:请求的上游分支 'origin/foo' 不存在”:
确保
origin
确实有foo
分支。确保
remote.origin.fetch
变量设置为+refs/heads/*:refs/remotes/origin/*
:git fetch -v< /代码>。 您应该看到 git 从
origin/foo
更新:git Branch -avv
将显示方括号,指示跟踪的远程分支:In case you got "error: the requested upstream branch 'origin/foo' does not exist" after running:
Make sure
origin
does have afoo
branch.Make sure the
remote.origin.fetch
variable is set to+refs/heads/*:refs/remotes/origin/*
:git fetch -v
. You should see git updating fromorigin/foo
:git branch -avv
will show square brackets, indicating tracked remote branches:这不是这个问题的直接答案,但我想在这里为那些在尝试配置上游分支时可能遇到与我相同问题的人留下一条注释。
警惕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
以某种相关的方式,我试图将远程跟踪分支添加到现有分支,但无法访问我想要添加该远程跟踪分支的系统上的远程存储库(因为我经常导出此副本)通过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
或者简单地通过:
如果您还没有在分支中,则切换到分支:
运行
并准备好:
您可以alawys查看配置文件,以查看运行时跟踪的内容:
知道这一点也很高兴,它显示哪些分支被跟踪,哪些分支不被跟踪。 :
or simply by :
switch to the branch if you are not in it already:
run
and you ready to :
You can alawys take a look at the config file to see what is tracking what by running:
It's also nice to know this, it shows which branches are tracked and which ones are not. :
对于像我这样只想将本地分支名称与远程分支名称同步的人,这里有一个方便的命令:
For anyone who, like me, just wants to sync up your local branch name with the remote branch name, here's a handy command:
为了避免记住每次收到消息时需要执行的操作:
您可以使用以下脚本,将您所在的当前分支设置为起源为上游。
就我而言,我几乎从不 > 将除原点之外的其他内容设置为默认上游。 另外,我几乎总是为本地和远程分支保留相同的分支名称。 所以以下内容适合我:
To avoid remembering what you need to do each time you get the message:
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:
这也行
This would work too