为什么在拉动时我需要一直执行 `--set-upstream-to`?

Why do I need to do `--set-upstream-to` all the time when pulling?

我通常必须克隆我的 git 存储库中没有的远程分支(例如来自同事)

虽然 git push 有一个简单的设置(参见 Why do I need to do `--set-upstream` all the time?) 我还没有发现 git pull.

也是如此

现在我的工作流程是:

唉,迎接我的是:

There is no tracking information for the current branch. Please specify which branch you want to merge with. See git-pull(1) for details.

git pull <remote> <branch>

If you wish to set tracking information for this branch you can do so with:

git branch --set-upstream-to=origin/<branch> branch123

我怎样才能使这更容易?

这是我想出的:

  • 哦,我必须从 github
  • 的 projectABC 中拉出 branch123
  • cd projectABC && git checkout -b branch123
  • git track <= 这是有趣的一点
  • git拉

在我的 .gitconfig

中使用以下别名
  track = "!f(){ branch=$(git name-rev --name-only HEAD); cmd=\"git branch --set-upstream-to ${1:-origin}/${2:-$branch} $branch\"; $cmd; }; f"

来源:https://andre.arko.net/2012/05/29/track-remote-git-branches-with-ease/

当您使用 -b 标志进行结帐时,您实际上创建了一个新的本地分支,而不是 linked 到任何远程分支。第一次推送新分支时,您可以通过 git push -u link 将其推送到新的远程分支。如果您正在签出现有分支,只需删除 -b.

如果您正在签出一个仅作为 origin/branchName 存在的分支(因此 git branch -l 不会在没有 origin 的情况下列出 branchName),如下所示:

git checkout branchName

您将创建一个自动跟踪 origin/branchName 的新本地分支。 Git 还应告知您:

Branch branchName set up to track remote branch branchName from origin.
Switched to a new branch 'branchName'

当您使用 -b 标志时,您还需要添加 -t 标志以一次性设置跟踪:

git checkout -b branchName -t origin/branchName

只有当您希望本地分支的名称与 origin 上的名称不同时,此命令才真正有用。在简单的情况下,只需省略 -b.

或者,您可以在第一次推送时使用 中提到的 -u 参数。这本质上对远程分支的同名自动映射与普通 git checkout 所做的相同。

通常,您会:

git branch -u %remotename%/%branchname%

将 %remotename% 替换为您设置的远程名称,通常称为 "origin"。将 %branchname% 替换为远程分支名称。