【问题标题】:Git: Track branch without checking out?Git:跟踪分支而不签出?
【发布时间】:2018-12-11 22:54:21
【问题描述】:

所以,我知道你可以在不检查的情况下拉出一个分支:

git fetch origin branchname:branchname

有没有办法在不检查的情况下也跟踪该分支? (通常,这是通过:git checkout --track origin/branchnamegit branch -u origin branchname 完成的)答案可以是完全单独的命令,也可以是上述提取的一部分。

【问题讨论】:

  • git branch -u <remote/branch> <branch>
  • 您列出的git branch + git fetch 命令应该可以实现目标。
  • 如果分支尚不存在,请使用git branch --track <branch> <upstream>。这会创建分支但不会检查它。请注意,参数的顺序首先是本地分支名称。

标签: git git-pull git-fetch


【解决方案1】:

答案已经嵌入到您的问题中,正如一些评论者指出的那样:使用git branch --set-upstream-to=origin/<em>branchname branchname</em>(或使用-u 的较短版本)。不过,在这里添加一些注释可能很重要。

这不是 拉分支(或者,好吧,也许是,因为该短语的定义不明确:-))。它的作用是:

  • 从源获取任何需要的提交,然后
  • 根据提取在origin 的分支branchname 中看到的内容创建或更新名为branchname 的本地分支。

如果对 branchname 的更改不是快进操作,则更新(如果是更新)将失败。要在这种情况下强制它成功,请添加强制标志:--force 或前导加号 +

如果 branchname 是当前签出的分支,即如果 HEAD 是对该分支的符号引用,则更新也会失败。

如果本地分支已经存在并且已经有上游设置,您可能不想覆盖当前上游设置。您可以使用git rev-parse 进行检查:<em>branchname</em>@{upstream} 将当前上游设置命名为 branchname,如果没有则失败,因此:

if ! git rev-parse $branchname@{upstream} >/dev/null 2>&1; then
    git branch --set-upstream-to=origin/$branchname $branchname
fi

如果未设置上游,将设置上游,但如果已设置,则不执行任何操作。

【讨论】:

  • 那么,我可以在 otherbranch 签出的同时 git branch -u branchname origin/branchname 吗?
  • 是的。顺序是另一种方式:-u &lt;upstream&gt; &lt;branch&gt;,而不是-u &lt;branch&gt; &lt;upstream&gt;。您可能希望坚持使用 --set-upstream-to=&lt;upstream&gt; &lt;branch&gt; 语法,因为这样更清晰。
猜你喜欢
  • 2016-08-09
  • 1970-01-01
  • 2011-03-04
  • 1970-01-01
  • 2012-03-03
  • 2013-03-13
  • 2014-02-04
  • 2014-08-22
  • 2020-09-26
相关资源
最近更新 更多