现在在 git 中检查远程分支安全吗?
我刚刚开始学习 Git,我正在使用的教程说克隆远程存储库时,切换到远程分支是一个很大的禁忌。 也就是说,如果我克隆了一个具有分支名称 branch1
的存储库,并且我想要处理它,我需要使用
git checkout --track -b branch1 origin/branch1
来跟踪它。然而,当我尝试使用
git checkout branch1
Git 回答
Branch branch1 set up to track remote branch branch1 from origin.
Switched to a new branch 'branch1'
时,我的常识告诉我,自从编写该教程以来,Git 的开发人员修复了这个陷阱,并使得如果有人尝试切换到远程分支,Git 会自动以正确的方式执行此操作,并且创建一个跟踪它的本地分支。但是,我在发行说明或 Google 搜索中找不到任何对此更改的引用。
那么,他们修好了吗?现在简单地检查这些远程分支是否安全?或者也许我误解了该教程中的警告,它指的是我应该警惕的另一个陷阱?
I've just started learning Git, and the tutorial I'm using says that when cloning remote repositories, switching to a remote branch is a big no-no. That is - if I've cloned a repository that has a branch names branch1
, and I want to work on it, I need to use
git checkout --track -b branch1 origin/branch1
to track it. However, when I tried to use
git checkout branch1
Git replied
Branch branch1 set up to track remote branch branch1 from origin.
Switched to a new branch 'branch1'
So, my common sense tells my that since that tutorial was written, Git's developers fixed that pitfall and made it so if someone tries to switch to a remote branch, Git automatically does it the right way and creates a local branch that tracks it. However, I can't find any reference to this change in the release notes or in a Google search.
So, did they fix it? Is it now safe to simply checkout those remote branches? Or maybe I misunderstood the warning in that tutorial, and it was referring to another pitfall I should watch out from?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您没有签出远程分支。
签出
branch1
与签出“远程分支”不同。以下命令尝试签出不存在的本地分支:而以下命令签出远程分支指向的提交:
这会导致 分离头,可能是教程警告的陷阱。
如果您尝试签出本地不存在但远程上有同名分支的分支,Git 将自动创建本地分支并将其设置为跟踪同名远程。
You aren't checking out a remote branch.
Checking out
branch1
isn't the same as checking out the "remote branch". The following command attempts to check out a local branch which doesn't exist:while the following command checks out the commit pointed to by the remote branch:
which results in a detached head, possibly the pitfall the tutorial was warning about.
Git will automatically create a local branch and set it up to track a remote of the same name if you attempt to check out a branch which doesn't exist locally, but which has a branch of the same name on a remote.
来自
Documentation/RelNotes/1.6.6.txt
:
所以,是的,Git 开发人员稍微简化了这个过程:您不必再指定
--track -b
来创建本地远程跟踪分支。 IIRC,在此更改之前,Git 曾经抱怨缺少本地分支“frtz”。就像 meagar 所说的那样,仍然可以使用 git checkout origin/branch1 语法来检查远程分支:如果您只是想四处看看,那么可以这样做,但您不应该进行永久性更改给他们。以下是当您签出远程分支时 Git 打印出的警告:
From
Documentation/RelNotes/1.6.6.txt
:So yes, Git developers simplified the process a bit: you don't have to specify
--track -b
any longer to create a local remote-tracking branch. IIRC, before this change Git used to complain about a missing local branch "frotz".Like meagar says, checking out remote branches is still possible by using the
git checkout origin/branch1
syntax: it's OK to do this if you just want to look around, but you are not supposed to make permanent changes to them. Here's the warning that Git prints out when you check out a remote branch: