git checkout 默认不创建本地分支并跟踪远程

标签 git

我正在克隆一个网站存储库。远程主机相当有限;我无法对 repo 进行 fork 。

当我克隆 repo 时,源被设置为远程主机,mysite@mysite.acquia.com。当我执行 git checkout {branch name} 时,新的本地分支会自动跟踪远程

因为我不想在远程 repo 上放一堆开发分支,所以我用 git remote rm origin 删除了 origin。然后,我基于内部共享驱动器上的 bare 存储库创建了一个名为 origin 的新远程:git remote add origin file:///h/path-to - repo 。 (这个 Remote 已经有了我的开发分支)。

然后,因为我最终想将我的开发分支推送到公共(public)远程仓库,所以我添加回原来的来源,这次称之为 acquia:git remote add acquia mysite@我的网站.acquia.com

所以现在我的 Remote 看起来像这样:

$ git remote -vv
acquia mysite@mysite.acquia.com
origin file:///h/path-to-repo

但是,当我从任一远程 checkout 分支时,默认行为不再是创建一个新的同名本地分支来跟踪远程。相反,我处于 headless 状态:

$ git checkout origin/2017-04-11_social-media-footer
Note: checking out 'origin/2017-04-11_social-media-footer'.

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 8fe45a25... Pinned social media view at 3 columns

$ git branch -a
* (HEAD detached at origin/2017-04-11_social-media-footer)
  Sprint-04282017
  remotes/acquia/2017-03-30_feature-a
  remotes/acquia/2017-03-31_feature-b
  ... 
  remotes/acquia/2017-04-11_social-media-footer
  ...

当然,我可以创建一个与远程分支名称相匹配的新本地分支,并将其设置为跟踪远程分支,但我习惯于自动发生这种情况。在我更换 Remote 之前,确实发生了这种情况。

当我 checkout 远程分支时,如何让我的 git 存储库自动分支并跟踪远程分支?

我在 Windows 7 上的 git-bash 上使用 git 版本 2.12.0.windows.1。

编辑 我做了一个常规克隆,没有更改 Remote ,以测试我认为的预期行为

$ git clone mysite@mysite.acquia.com
Cloning into 'mysite'...
remote: Counting objects: 19980, done.
remote: Compressing objects: 100% (16606/16606), done.
remote: Total 19980 (delta 6440), reused 15091 (delta 2786)
Receiving objects: 100% (19980/19980), 49.65 MiB | 176.00 KiB/s, done.
Resolving deltas: 100% (6440/6440), done.
Checking out files: 100% (12066/12066), done.

$ cd mysite; git branch
* master

$ git checkout 2017-04-05_memcache
Checking out files: 100% (210/210), done.
Switched to a new branch '2017-04-05_memcache'
Branch 2017-04-05_memcache set up to track remote branch 2017-04-05_memcache from origin.

$ git branch
* 2017-04-05_memcache
  master

# what is this branch tracking?
$ git  rev-parse --abbrev-ref --symbolic-full-name @{u}
origin/2017-04-05_memcache

所以默认情况下,当我克隆一个远程存储库时,检查远程上的一个分支会创建一个同名的本地分支,并将其设置为跟踪远程。这是我在所有工作场所和家中习惯的行为。我在这里做了什么来更改此默认行为?

最佳答案

为什么不起作用:快捷方式有太多选择

基本问题归结为the git checkout documentation中的这句话:

If <branch> is not found but there does exist a tracking branch in exactly one remote (call it <remote>) with a matching name, treat as equivalent to

$ git checkout -b <branch> --track <remote>/<branch>

(强调我的)。

假设你只有一个 Remote ,你输入命令:

git checkout 2017-04-05_memcache

Git 看到没有2017-04-05_memcache然而,检查你所有的 Remote ,找到一个匹配的 origin/2017-04-05_memcache ,并做引用的事情。

假设现在您有两个 Remote ,originacquia , 并输入相同的命令。如果只有一个 Remote 有origin/2017-04-05_memcacheacquia/2017-04-05_memcache ,它再次一切正常。但是,如果两者 都有相应的远程跟踪分支,Git 不知道使用哪一个。它举起比喻性的手,让您输入更长的命令。

漫长的道路和另一条不同的捷径

就个人而言,我可能只输入 git checkout -b 2017-04-05_memcache <remote>/2017-04-05_memcache .然而,在同一文档的更下方,我们看到:

--track

    When creating a new branch, set up "upstream" configuration. See "--track" in git-branch(1) for details.
    If no -b option is given, the name of the new branch will be derived from the remote-tracking branch, by looking at the local part of the refspec configured for the corresponding remote, and then stripping the initial part up to the "*". This would tell us to use "hack" as the local branch when branching off of "origin/hack" (or "remotes/origin/hack", or even "refs/remotes/origin/hack"). If the given name has no slash, or the above guessing results in an empty name, the guessing is aborted. You can explicitly give a name with -b in such a case.

因此,您可以输入:

git checkout -t origin/2017-04-11_social-media-footer

而不是:

git checkout -b 2017-04-05_memcache origin/2017-04-05_memcache

并获得相同的效果:您指定您的远程跟踪分支名称(它允许您选择您想要的哪个远程) -t--track选项,Git 猜测要创建的本地 分支名称,而不是指定本地名称并让 Git 猜测您指的是哪个远程跟踪分支的通常方法。

关于git checkout 默认不创建本地分支并跟踪远程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43762884/

相关文章:

Git Rebase 似乎已经工作,但所有提交仍在日志中显示......我处于什么状态?

git - 如何为 HTTPS git push 设置默认用户名?

windows - 在 Windows 10 命令行中启用 git

Git fatal error : info/refs not found

git - 如何将某些文件推送到 Git 中的 origin/master?

git - 无法安装git flow

visual-studio-2010 - GitExtensions 配置错误

git - git [master +2 ~1 -0 !] 是什么意思?

git - 受污染的远程主分支或反樱桃选择

公司网络上的 git SSL 问题