现在在 git 中检查远程分支安全吗?

发布于 2024-12-11 05:47:01 字数 668 浏览 0 评论 0原文

我刚刚开始学习 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

囚我心虐我身 2024-12-18 05:47:01

没有签出远程分支。

签出branch1与签出“远程分支”不同。以下命令尝试签出不存在的本地分支:

git checkout branch1

而以下命令签出远程分支指向的提交:

git checkout origin/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:

git checkout branch1

while the following command checks out the commit pointed to by the remote branch:

git checkout origin/branch1

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.

和影子一齐双人舞 2024-12-18 05:47:01

来自 Documentation/RelNotes/1.6.6.txt

  • "git checkout frotz" 当没有本地分支“frotz”但是
    只有一个远程跟踪分支“frotz”被视为
    请求在相应的远程启动指定分支
    跟踪分支。

所以,是的,Git 开发人员稍微简化了这个过程:您不必再指定 --track -b 来创建本地远程跟踪分支。 IIRC,在此更改之前,Git 曾经抱怨缺少本地分支“frtz”。

就像 meagar 所说的那样,仍然可以使用 git checkout origin/branch1 语法来检查远程分支:如果您只是想四处看看,那么可以这样做,但您不应该进行永久性更改给他们。以下是当您签出远程分支时 Git 打印出的警告:

$ git checkout origin/master
Note: checking out 'origin/master'.

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.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b new_branch_name

HEAD is now at a48aafd... Merge branch 'maint'

From Documentation/RelNotes/1.6.6.txt:

  • "git checkout frotz" when there is no local branch "frotz" but
    there is only one remote tracking branch "frotz" is taken as a
    request to start the named branch at the corresponding remote
    tracking branch.

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:

$ git checkout origin/master
Note: checking out 'origin/master'.

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.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b new_branch_name

HEAD is now at a48aafd... Merge branch 'maint'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文