简体   繁体   English

Git - 如何跟踪从本地分支创建的远程分支?

[英]Git - How to track a remote branch that you created from a local branch?

Here's the scenario. 这是场景。

I have a series of commits in branch my_new_branch 我在分支my_new_branch中有一系列提交

I want to push this branch up to the remote repo as a different name and track it. 我想将这个分支作为一个不同的名称推送到远程仓库并跟踪它。

If I do: 如果我做:

git push origin my_new_branch:different_name

it pushes the branch up fine. 它把分支推得很好。

But if I stay in my_new_branch and make another commit, if I just do 但是,如果我留在my_new_branch并进行另一次提交,如果我这样做的话

git push

it says 'Everything up to date' 它说“一切都是最新的”

Further, if I look at .git/config, I can see it hasn't set up a tracking branch. 此外,如果我查看.git / config,我可以看到它没有设置跟踪分支。

Of course I can just do 我当然可以这样做

git push origin my_new_branch:different_name 

again, but how do I get it to track? 再次,但我如何让它跟踪?

If I do 如果我做

git checkout -b my_new_branch origin/different_name

it complains that the branch name already exists. 它抱怨分支名称已经存在。

Thanks! 谢谢!

As you mention, the question " How do you make an existing git branch track a remote branch? " indicate the possibility to set the upstream branch (the one to where you push) for a local branch (since Git1.7.0). 正如您所提到的,“ 如何使现有的git分支跟踪远程分支? ”这一问题表明可以为本地分支设置上游分支(推送的分支)(自Git1.7.0起)。

git branch --set-upstream my_new_branch origin/different_name

which replaces: 取代:

git config branch.my_new_branch.remote origin
git config branch.my_new_branch.merge refs/heads/different_name

By default, git push uses a special refspec ' : ' 默认情况下, git push使用特殊的refspec' : '

The special refspec : (or +: to allow non-fast-forward updates) directs git to push "matching" branches : 特殊refspec :+:允许非快进更新)指示git推送“匹配”分支
for every branch that exists on the local side, the remote side is updated if a branch of the same name already exists on the remote side. 对于本地端存在的每个分支,如果远程端已存在同名分支,则更新远程端。
This is the default operation mode if no explicit refspec is found (that is neither on the command line nor in any Push line of the corresponding remotes file). 如果未找到显式refspec,则这是默认操作模式(既不在命令行上也不在相应远程文件的任何Push行中)。

Hence your troubles with " git push " (no arguments). 因此你的麻烦是“ git push ”(没有参数)。

If you remember to use -u when you push, git will set this up for you automatically. 如果你在推动时记得使用-u,git会自动为你设置。

-u, --set-upstream -u, - set-upstream

For every branch that is up to date or successfully pushed, add upstream (tracking) reference, used by argument-less git-pull(1) and other commands. 对于每个最新或成功推送的分支,添加上游(跟踪)引用,由无参数git-pull(1)和其他命令使用。 For more information, see branch..merge in git-config(1). 有关更多信息,请参阅git-config(1)中的branch..merge。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM