遥控器包含各种分支,例如 origin/daves_branch
:
$ git branch -r
origin/HEAD -> origin/master
origin/daves_branch
origin/master
如何在本地结帐 daves_branch
以跟踪 origin/daves_branch
?我试过了:
$ git fetch origin discover
$ git checkout discover
git fetch --all
,然后查看所有分支:git branch
,然后我签出分支:git checkout nameofnewbranch
git fetch origin discover:discover && git checkout discover
更新:使用 Git 开关
下面写的所有信息都是准确的,但添加了一个新命令 git switch
以简化工作。
如果远程存储库中存在 daves_branch
,但本地分支中不存在,则只需键入:
git switch daves_branch
由于您在本地没有分支,这将自动使 switch
查看远程存储库。然后它还将自动设置远程分支跟踪。
请注意,如果本地不存在 daves_branch
,您需要先git fetch
,然后才能使用 switch
。
原帖
您需要创建一个跟踪远程分支的本地分支。以下命令将创建一个名为 daves_branch 的本地分支,跟踪远程分支 origin/daves_branch。当您推送更改时,远程分支将被更新。
对于最新版本的 Git:
git checkout --track origin/daves_branch
--track
是 git checkout -b [branch] [remotename]/[branch]
的简写,其中 [remotename] 在本例中是 origin,而 [branch] 在本例中是 daves_branch 的两倍。
对于 Git 1.5.6.5,你需要这个:
git checkout --track -b daves_branch origin/daves_branch
对于 Git 1.7.2.3 及更高版本,这就足够了(它可能开始得更早,但这是我能很快找到的最早确认):
git checkout daves_branch
请注意,在最近的 Git 版本中,此命令不会创建本地分支,而是会将您置于“分离 HEAD”状态。如果您想要一个本地分支,请使用 --track
选项。
完整的详细信息在这里:3.5 Git Branching - Remote Branches, Tracking Branches
git fetch <remote> <rbranch>:<lbranch>
git checkout <lbranch>
...其中 <rbranch>
是远程分支或 source ref,而 <lbranch>
是目前 不存在 本地分支或 destination ref 您想要跟踪并且您可能想要将其命名为与远程分支或源引用相同的名称。这在 <refspec>
的解释中的 options 下进行了解释。
Bash 非常聪明,如果我在远程分支的前几个字母之后按 Tab,它会自动完成第一个命令。也就是说,我什至不必命名本地分支; Bash 会自动为我复制远程分支的名称。谢谢,巴什!
同样如 the answer in this similar Stack Overflow post 所示,如果您不在 fetch
中命名本地分支,您仍然可以在使用 -b
标志检出它时创建它。 也就是说, git fetch <remote> <branch>
后跟 git checkout -b <branch> <remote>/<branch>
与我最初的答案完全相同。显然,如果您的存储库只有一个远程,那么您只需在 fetch
之后执行 git checkout <branch>
,它就会为您创建一个本地分支。 例如,您刚刚克隆了一个存储库,并希望从远程检查其他分支。
我相信 fetch
的某些文档可能是从 pull
逐字复制的。特别是 options 中的 <refspec>
部分是相同的。但是,我不相信 fetch
会永远merge
,因此如果您将冒号的目标端留空,fetch
应该什么都不做。
注意:git fetch <remote> <refspec>
是 git fetch <remote> <refspec>:
的缩写,因此不会执行任何操作,但 git fetch <remote> <tag>
与 git fetch <remote> <tag>:<tag>
相同,应将远程 <tag>
复制到本地。
我想这仅在您想在本地复制远程分支时才有用,但不一定要立即检查出来。否则,我现在将使用 the accepted answer,它在 checkout description 的第一部分以及稍后在 --track
的解释下的 options 部分中进行了详细说明,因为它是单行的。 嗯... 有点像单线,因为您仍然必须先运行 git fetch <remote>
。
仅供参考:<refspecs>
的顺序(来源:目的地)解释了 deleting remote branches 的奇怪的 pre Git 1.7 方法。也就是说,不向目标 refspec 推送任何内容。
git fetch remote branch
根本没有为我添加分支头,尽管所有的 refs 都被获取了,所以当我尝试按照接受的答案中的步骤操作时,我得到了 pathspec did not match any file(s) known to git.
的错误,但是rbranch:lbranch
方法有效。有趣的是,它还获取了所有以相同前缀开头的标签,就像它是通配符 (rbranch*
)。
fetch
命令。不过,接受的答案是有道理的,因为 OP 指出他已经进行了提取。这至少是我遇到的问题。
$ git checkout -b <branch> --track <remote>/<branch>
”
如果您尝试“签出”一个新的远程分支(仅存在于远程,而不是本地),这就是您需要的:
git fetch origin
git checkout --track origin/<remote_branch_name>
这假设您想从原点获取。如果不是,请将 origin 替换为您的远程名称。
git fetch
和 git fetch remote
都会做同样的事情。如果您需要从 origin
以外的远程获取,您可以使用 git fetch <other_remote_name>
来完成。这种情况很少见,这里只是为了完整起见。
fatal: 'fusedwm/fuse_keybindings-setborderpx-alphabar-transparency-monrules-nowarpresize' is not a commit and a branch 'fuse_keybindings-setborderpx-alphabar-transparency-monrules-nowarpresize' cannot be created from it
要检查远程而不是本地存在的 myBranch - 这对我有用:
git fetch --all
git checkout myBranch
我收到了这条消息:
Branch myBranch set up to track remote branch myBranch from origin
Switched to a new branch 'myBranch'
-t
?
-t
的情况下执行命令并得到 You are in 'detached HEAD' state. You can look around, make experimental changes and commit them, and you can discard any commits you make in this state without impacting any branches by performing another checkout.
,因为没有同名的本地分支。我不得不用 -t
重新运行才能修复。
checkout
来创建它。谢谢!
--all
绝不是一个好主意,因为它会下载每个分支上的每个文件。这将需要更多的时间和空间。最好具体说明分支名称并像 this
在本地获取分支:
git fetch origin <branchName>
移动到那个分支:
git checkout <branchName>
fetch
命令后,所需的分支将在本地计算机上可用。 git checkout -b 'your_branch' origin/'remote branch'
是签出此分支所必需的。
使用 git branch -a
(本地和远程分支)或 git branch -r
(仅远程分支)查看所有远程及其分支。然后,您可以对远程执行 git checkout -t remotes/repo/branch
并创建本地分支。
还有一个 git-ls-remote 命令可以查看该遥控器的所有参考和标签。
git checkout remotes/repo/branch
使 git checkout 查找路径规范,而不是远程存储库。
git checkout -t remote_branch_name
可能是所有答案中最简单和最懒惰的方法。
标题和问题混淆了:
Git 获取远程分支
我的同事怎么能专门拉那个分支。
如果问题是,我怎样才能得到一个远程分支,或者我怎样才能 Git 签出一个远程分支?,一个更简单的解决方案是:
使用 Git (>= 1.6.6),您可以使用:
git checkout <branch_name>
如果未找到本地 <branch_name>
,但在一个具有匹配名称的远程中确实存在跟踪分支,则将其视为等效于:
git checkout -b <branch_name> --track <remote>/<branch_name>
See documentation for Git checkout
给你的朋友:
$ git checkout discover
Branch discover set up to track remote branch discover
Switched to a new branch 'discover'
要获取远程存在的分支,最简单的方法是:
git fetch origin branchName
git checkout branchName
您可以通过以下方式查看它是否已存在于远程:
git branch -r
这会将远程分支获取到您的本地并自动跟踪远程分支。
利用:
git checkout -b serverfix origin/serverfix
这是一个足够常见的操作,Git 提供了 --track
简写:
git checkout --track origin/serverfix
事实上,这很常见,甚至还有一条捷径。如果您尝试签出的分支名称 (a) 不存在并且 (b) 仅与一个遥控器上的名称完全匹配,Git 将为您创建一个跟踪分支:
git checkout serverfix
要使用与远程分支不同的名称设置本地分支,您可以轻松地使用具有不同本地分支名称的第一个版本:
git checkout -b sf origin/serverfix
现在,您的本地分支 sf
将自动从 origin/serverfix
拉取。
来源:Pro Git, 2nd Edition, written by Scott Chacon and Ben Straub(为便于阅读而删减)
使用这个简单的命令:
git checkout -b 'your_branch' origin/'remote branch'
[快速回答]
有很多选择,我最喜欢的是:
- 备选方案 1:
git fetch --all
git checkout YourBranch
使用远程存在但不在本地的分支使用此替代方法。
- 备选方案 2:
git checkout -b 'YourBranch' origin/'YourRemote'
可能,这是最简单的方法。
帮助我的是
1)查看所有可用的远程分支(例如'remote-branch-name')
git branch -r
2) 使用远程分支名称创建本地分支
git fetch && git checkout 'remote-branch-name'
git push
时会发生什么?名为 remote-branch-name
的本地分支是否自动与名为 origin/remote-branch-name
的远程分支关联(跟踪)。或者您是否需要运行 git push -u origin remote-branch-name
git fetch
git branch -r
git checkout <branch_name>
您也可以一次性获取并签出远程分支:
git fetch && git checkout the-branch-name
我打了
git checkout <branch_name>
并得到
Branch <branch_name> set up to track remote branch <branch_name> from origin.
Switched to a new branch '<branch_name>'
git checkout -b <branch_name> --track <remote>/<branch_name>
有时,您会被要求不要摆弄 master 分支,而只使用远程分支(正如我被要求的那样)。所以你只需要远程分支。
因此,要单独克隆远程分支(没有主分支),请执行此操作
git clone url --branch remote_branch_name
其中,remote_branch_name 是远程分支的名称
例如,
git clone git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable.git --branch v3.15
这将确保您使用远程分支的名称将远程分支克隆到本地分支。
现在,如果您提交代码并推送,代码将单独提交到该分支。
步骤如下;
git fetch origin 或 git fetch --all ,这会将所有远程分支获取到您的本地,然后这是您可以处理的第二个选项。 git checkout --track origin/<你要切换的远程分支>
然后在这个分支上工作,你可以通过输入来验证你是否在那个分支上
git branch
它显示您当前所在的分支。
只需尝试:
git pull origin your_branch_name
假设您的遥控器是 git@xyz.git 并且您想要它的 random_branch 分支。该过程应如下所示:
首先通过 git remote -v 检查你的遥控器列表如果你在上面命令的输出中没有 git@xyz.git 遥控器,你可以通过 git remote add xyz git@xyz.git 添加它现在你可以获取通过 git fetch xyz 获取该远程的内容 现在通过 git checkout -b my_copy_random_branch xyz/random_branch 检查该远程的分支 通过 git branch -a 检查分支列表
本地分支 my_copy_random_branch 将跟踪远程的 random_branch 分支。
如果您想获取所有远程分支,请输入:
git fetch --all
如果您有一个使用 --depth 1
克隆的存储库,那么列出的许多命令将不起作用。例如,请参见此处
% git clone --depth 1 https://github.com/repo/code
Cloning into 'code'...
cd code
remote: Counting objects: 1778, done.
remote: Compressing objects: 100% (1105/1105), done.
remote: Total 1778 (delta 87), reused 1390 (delta 58), pack-reused 0
Receiving objects: 100% (1778/1778), 5.54 MiB | 4.33 MiB/s, done.
Resolving deltas: 100% (87/87), done.
Checking connectivity... done.
Checking out files: 100% (1215/1215), done.
% cd code
% git checkout other_branch
error: pathspec 'other_branch' did not match any file(s) known to git.
% git fetch origin other_branch
remote: Counting objects: 47289, done.
remote: Compressing objects: 100% (15906/15906), done.
remote: Total 47289 (delta 30151), reused 46699 (delta 29570), pack-reused 0
Receiving objects: 100% (47289/47289), 31.03 MiB | 5.70 MiB/s, done.
Resolving deltas: 100% (30151/30151), completed with 362 local objects.
From https://github.com/repo/code
* branch other_branch-> FETCH_HEAD
% git checkout other_branch
error: pathspec 'other_branch' did not match any file(s) known to git.
%
在这种情况下,我会重新克隆存储库,但也许还有其他技术,例如 git shallow clone (clone --depth) misses remote branches
git fetch --all & git checkout <branch name>
git fetch && git checkout <your friend's branch name>
应该可以解决问题
我想给你一个命令来获取所有远程分支到你的本地并切换到你想要的新创建的本地分支:
git fetch && git checkout discover
运行上述命令后,您将收到以下消息:
Switched to a new branch 'discover'
Branch discover set up to track remote branch discover from origin.
第一行说明切换到新分支 - 为什么是新分支?它已经在远程了!
但实际上你也必须在本地创建它。分支取自远程索引并在本地为您创建。
这里的 discover
是从存储库的远程分支 discover
创建的新分支。
但是第二行提供了比第一行更多的信息,它告诉我们:
我们的分支设置为跟踪具有相同名称的远程分支。
虽然git fetch
将所有分支 提取到本地。但是如果你在它之后运行 git branch
,你只会在本地看到 master
分支。 为什么?
因为对于您在远程拥有的每个分支,您也必须在本地创建它,以便将其跟踪为 git checkout <branchname>
,就像我们在上面的示例中所做的那样。
运行 git checkout
命令后,您可以运行 git branch
,现在您可以看到两个分支:
掌握和 2. 在您的本地列表中发现。
检查您的 .git/config
文件,特别是该遥控器的 fetch 上存在哪些跟踪。
[remote "randomRemote"]
url = git@github.com:someUser/someRepo.git
fetch = +refs/heads/*:refs/remotes/randomRemote/*
如果它有 heads/*
指向 randomRemote/*
,当您运行 git fetch randomRemote
时,它将获取所有分支。
然后你可以检查那个分支。
否则,
您需要使用此方法将远程分支添加到跟踪中。运行后检查你的 .git/config 。你会明白的。 git remote set-branches --add randomRemote randomBranch 运行 git fetch randomRemote。这将获取远程分支。现在你可以运行 git checkout randomBranch。
如果您已经像这样知道您的远程分支......
git remote
=> One
=> Two
并且您知道要结帐的分支名称,例如 br1.2.3.4,然后执行
git fetch One
=> returns all meta data of remote, that is, the branch name in question.
剩下的就是结帐分支
git checkout br.1.2.3.4
然后制作任何新的分支。
git branch <name> --track origin/<name>
你使用'git pull'来保持你的分支分开。我将使用实际的存储库和分支名称来提供帮助,因为“lbranch”和“rbranch”很难破译。
让我们使用:
myteam.unfuddle.com = 远程 Git 服务器
tlc = 解开存储库所在的项目帐户
daves_branch = 远程分支名称 您或任何同事都可以运行此命令以仅拉取您的分支,无论有多少分支: git init git pull git@myteam.unfuddle.com:myteam/tlc daves_branch:refs/remotes/origin /daves_branch
一个简单的命令 git checkout remote_branch_name
将帮助您创建一个本地分支,其中包含远程分支中的所有更改。
如果您使用 git clone <repo_url> -b <branch>
下载存储库(仅克隆某些分支),您应该修改 <repo_name>/.git/config
文件。替换或修改引用 [remote "origin"]
部分的 fetch 目标的行,以让命令 git fetch --all
发现所有分支:
[remote "origin"]
url = <repo_git_url>
fetch = +refs/heads/master:refs/remotes/origin/master
请务必将 fetch 参数点设置为 /heads/master
。
请注意 git fetch --all
,因为这将获取所有内容,因此可能需要很长时间。
不定期副业成功案例分享
git checkout -b --track daves_branch origin/daves_branch