获取现有的 git 分支以跟踪远程分支
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1184518/
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
Getting existing git branches to track remote branches
提问by SpoonMeiser
My usual workflow when working with git, is something like this:
我在使用 git 时通常的工作流程是这样的:
- create a local repository
- do some work in that repository, add/change files etc.
- decide that I want a central remote location for the repository, and create one
- push all the commits from my local repository to this new remote repository
- 创建本地存储库
- 在该存储库中做一些工作,添加/更改文件等。
- 决定我想要存储库的中央远程位置,并创建一个
- 将所有提交从我的本地存储库推送到这个新的远程存储库
Now, however, I want to be able to push
and pull
from this remote repository without having to specify where I'm pushing to or pulling from; I want my local master to track the remote master.
然而,现在我希望能够push
和pull
从该远程存储库,而无需指定我推或拉离; 我希望我的本地主机跟踪远程主机。
The properway to do this isn't clear to me, and I've been unable to determine it from the documentation, even though it shouldn't really be more than one command.
执行此操作的正确方法对我来说并不清楚,而且我一直无法从文档中确定它,即使它实际上不应该是一个以上的命令。
Because it's something that's only ever done once per repository, I've generally employed one of two simple, but hacky, solutions:
因为它是每个存储库只执行一次的事情,所以我通常采用两种简单但很棘手的解决方案之一:
- used
git clone
to make a new local repository, and deleted the old one. After git cloning, the new repository is setup to track the origin. - manually edited .git/config to make master track origin.
- 用于
git clone
创建一个新的本地存储库,并删除旧的。在 git 克隆之后,新的存储库被设置为跟踪源。 - 手动编辑 .git/config 以制作主跟踪原点。
I think I should be able to run a command, probably some form of git remote
to setup an existing repository to have master track a remote master. Can anyone tell me what that command is?
我想我应该能够运行一个命令,可能是某种形式的git remote
设置现有存储库以使 master 跟踪远程 master。谁能告诉我那个命令是什么?
回答by Jorge
Use the set-upstream arg:
使用设置上游参数:
git branch --set-upstream local-branch-name origin/remote-branch-name
Running the above command updates your .git/config file correctly and even verifies with this output:
运行上面的命令会正确更新您的 .git/config 文件,甚至可以使用以下输出进行验证:
"Branch local-branch-name set up to track remote branch remote-branch-name from origin."
“分支本地分支名称设置为从源跟踪远程分支远程分支名称。”
EDIT:As martijnsaid: "In version Git v1.8.0, --set-upstream is deprecated. Use --set-upstream-to instead."
编辑:正如martijn所说:“在 Git v1.8.0 版本中,不推荐使用 --set-upstream。改用 --set-upstream-to。”
git branch --set-upstream-to local-branch-name origin/remote-branch-name
See thisfor more information.
有关更多信息,请参阅此内容。
回答by Mike Pape
git help remote
should show you what you need to know. I think what you want is
git help remote
应该向您展示您需要知道的内容。我想你想要的是
git remote add [remote-name] [remote-url]
# Set a local branch to follow the remote
git config branch.[branch-name].remote [remote-name]
# Set it to automatically merge with a specific remote branch when you pull
git config branch.[branch-name].merge [remote-master]
You can also manually edit .git/config to set these up.
你也可以手动编辑 .git/config 来设置这些。
回答by bcolfer
You can also use this if you want to create a new local branch to track a remote branch:
如果你想创建一个新的本地分支来跟踪远程分支,你也可以使用它:
git checkout --track -b [branch_name] --track origin[or other remote name]/[remote_branch_name]
or even better:
甚至更好:
git checkout -t origin/branch_name
回答by fazineroso
On newer versions of git you can use
在较新版本的 git 上,您可以使用
git branch --track origin/branch_name
回答by Denis C
The --set-upstream flag is deprecated and will be removed.
The --set-upstream flag is deprecated and will be removed.
git branch master --set-upstream-to myupstream/master
git branch master --set-upstream-to myupstream/master
回答by MikeA
Fast forward three years (see what I did there :-) ), I tried to pull an untracked branch using Git Bash and received
快进三年(看看我在那里做了什么:-)),我尝试使用 Git Bash 拉出一个未跟踪的分支并收到
If you wish to set tracking information for this branch you can do so with:
If you wish to set tracking information for this branch you can do so with:
git branch --set-upstream-to=origin/<branch> develop
The following achieved what I needed:
以下实现了我所需要的:
$ git branch --set-upstream-to=origin/develop develop
Branch 'develop' set up to track remote branch 'develop' from 'origin'.
$ git branch --set-upstream-to=origin/develop develop
Branch 'develop' set up to track remote branch 'develop' from 'origin'.