git 跟踪在 GitHub 上创建的新远程分支

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11262703/
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 14:02:59  来源:igfitidea点击:

Track a new remote branch created on GitHub

gitgithubgit-branch

提问by MLister

I have already got a local master branch tracking the remote master branch of a github project. Now, a collaborator of mine has created a new branch in the same project, and I want to do the following accordingly:

我已经有一个本地 master 分支跟踪 github 项目的远程 master 分支。现在,我的一个合作者在同一个项目中创建了一个新分支,我想相应地执行以下操作:

  1. create a new branch locally
  2. make this new branch track the newly create remote branch.
  1. 在本地创建一个新分支
  2. 使这个新分支跟踪新创建的远程分支。

How should I do it properly?

我该怎么做才能正确?

回答by max

git fetch
git branch --track branch-name origin/branch-name

First command makes sure you have remote branch in local repository. Second command creates local branch which tracks remote branch. It assumes that your remote name is originand branch name is branch-name.

第一个命令确保您在本地存储库中有远程分支。第二个命令创建跟踪远程分支的本地分支。它假设您的远程名称是origin,分支名称是branch-name

--trackoption is enabled by default for remote branches and you can omit it.

--track远程分支默认启用选项,您可以省略它。

回答by kotoole

If you don't have an existing local branch, it is truly as simple as:

如果您没有现有的本地分支,它真的很简单:

git fetch
git checkout <remote-branch-name>

For instance if you fetch and there is a new remote tracking branch called origin/feature/Main_Page, just do this:

例如,如果您 fetch 并且有一个名为 的新远程跟踪分支origin/feature/Main_Page,请执行以下操作:

git checkout feature/Main_Page

This creates a local branch with the same name as the remote branch, tracking that remote branch. If you have multiple remotes with the same branch name, you can use the less ambiguous:

这将创建一个与远程分支同名的本地分支,跟踪该远程分支。如果您有多个具有相同分支名称的遥控器,则可以使用不那么模糊的:

git checkout -t <remote>/<remote-branch-name>

If you already made the local branch and don't want to delete it, see How do you make an existing Git branch track a remote branch?.

如果您已经创建了本地分支并且不想删除它,请参阅如何使现有的 Git 分支跟踪远程分支?.

回答by Anghel Contiu

First of all you have to fetch the remote repository:

首先,您必须获取远程存储库:

git fetch remoteName

Than you can create the new branch and set it up to track the remote branch you want:

然后您可以创建新分支并将其设置为跟踪您想要的远程分支:

git checkout -b newLocalBranch remoteName/remoteBranch

You can also use "git branch --track" instead of "git checkout -b" as max specified.

您还可以使用“git branch --track”而不是“git checkout -b”作为最大值。

git branch --track newLocalBranch remoteName/remoteBranch

回答by René H?hle

When the branch is no remote branch you can push your local branch direct to the remote.

当分支不是远程分支时,您可以将本地分支直接推送到远程。

git checkout master
git push origin master

or when you have a dev branch

或者当你有一个开发分支时

git checkout dev
git push origin dev

or when the remote branch exists

或者当远程分支存在时

git branch dev -t origin/dev

There are some other posibilites to push a remote branch.

还有一些其他可能性可以推送远程分支。