git 什么是跟踪参考?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15375192/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 15:43:51  来源:igfitidea点击:

What is a Tracking Reference?

gitgit-extensions

提问by Simon Tewsi

When I push a new branch up to a remote repository via Git Extensions, I get an alert saying

当我通过 Git 扩展将一个新分支推送到远程存储库时,我收到一条警告说

The branch {branch name} does not have a tracking reference. Do
you want to add a tracking reference for {branch name}?

What is a tracking reference? I've found only a few mentions of tracking references in Google and no real definition.

什么是跟踪参考?我发现在 Google 中只提到了几次跟踪引用,并没有真正的定义。

采纳答案by Eric Walker

The basic idea is that there are purely local references (e.g., branches, tags), and then there are remote tracking references, which follow what happens in other repos. Because Git is decentralized, it is possible for you to choose a name for a branch that is the same as one used in a remote, without having known about the other one, such that they have completely different work on them. Git lets you do this, but it also provides a way to link local references to remote ones as well.

基本思想是有纯粹的本地引用(例如,分支、标签),然后有远程跟踪引用,这些引用遵循其他 repos 中发生的情况。因为 Git 是去中心化的,所以你可以为一个分支选择一个与远程使用相同的名称,而不知道另一个,这样它们就可以进行完全不同的工作。Git 允许您这样做,但它也提供了一种将本地引用链接到远程引用的方法。

For example, consider the following:

例如,请考虑以下情况:

% git branch -a
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/maint
  remotes/origin/master
  remotes/origin/next
  remotes/origin/pu
  remotes/origin/todo

Here we have branches on origincalled nextand todo.

在这里,我们有origin被调用next和的分支todo

% git checkout -t remotes/origin/next
Branch next set up to track remote branch next from origin.
Switched to a new branch 'next'
% git branch todo    

Now we have a local branch nextthat tracks the remote branch of the same name and local branch todothat will not be updated with changes to remotes/origin/todo.

现在我们有一个本地分支next来跟踪同名的远程分支,而本地分支todo不会随着对remotes/origin/todo.

回答by Hemaolle

A local git branch can track a remote branch, which means that git push and git pull commands will know to push and pull commits to and from the tracked branch by default. Also git status will tell the status between your current local branch and the remote branch it's tracking. When you clone a git repository, git will add a tracking reference to the local master branch to track the remote master branch. When you checkout from a new remote branch, git will add a tracking reference to the created local branch to track the remote branch you checked out.

本地 git 分支可以跟踪远程分支,这意味着 git push 和 git pull 命令将知道默认情况下向跟踪分支推送和拉取提交。另外 git status 会告诉你当前的本地分支和它正在跟踪的远程分支之间的状态。当你克隆一个 git 仓库时,git 会添加一个对本地 master 分支的跟踪引用来跟踪远程 master 分支。当您从新的远程分支签出时,git 会添加对创建的本地分支的跟踪引用,以跟踪您签出的远程分支。

However, if you create a new branch locally, and then push it to the remote repository, you have to explicitly tell git if you want your local branch to start tracking the new remote branch. You do that with the -uor --set-upstreamoption when pushing the local branch to the remote repository: git push -u origin my-new-branch.

但是,如果您在本地创建一个新分支,然后将其推送到远程存储库,则您必须明确告诉 git 是否希望您的本地分支开始跟踪新的远程分支。你这样做,与-u--set-upstream推动当地分支机构的远程仓库时,选项:git push -u origin my-new-branch

You can check which remote branches your local branches are tracking (if any) with the command git branch -vvBelow is a small example of the output.

您可以使用以下命令检查本地分支正在跟踪哪些远程分支(如果有)git branch -vv下面是输出的一个小示例。

  b1     560eb64 Added file.txt
  b2     560eb64 [origin/b2] Added file.txt
  b3     b638c18 [origin/r1: ahead 1] Added file3.txt
* master 560eb64 [origin/master] Added file.txt

In this case we have local branches master, b1, b2and b3. The masterbranch is tracking a remote branch called master, the b1branch isn't tracking any remote branches, the b2branch is tracking a remote branch called b2and the b3branch is tracking a remote branch called r1. git branch -vvalso shows the status of the branch related to the traced branch. Here branch b3is 1 commit ahead of the tracked remote branch and the other branches are up to date with their respective remote tracked branches.

在这种情况下,我们有地方分支机构masterb1b2b3。该master分支正在跟踪名为 的远程分支master,该b1分支未跟踪任何远程分支,该b2分支正在跟踪名为 的远程分支,b2而该b3分支正在跟踪名为 的远程分支r1git branch -vv还显示与跟踪分支相关的分支的状态。这里的分支b3比被跟踪的远程分支提前 1 次提交,其他分支与各自的远程被跟踪分支保持同步。

So if you create a local branch and push to the remote repository, would you want to add a tracking reference to the branch or not? Usually when you push a new local branch to the remote repository, you do it to collaborate with other developers on a feature. If you add a tracking reference to your local branch, you can conveniently pull the changes that other people have made to the branch, so I would say that in most cases you want to add the tracking reference.

因此,如果您创建一个本地分支并推送到远程存储库,您是否要添加对该分支的跟踪引用?通常,当您将新的本地分支推送到远程存储库时,您这样做是为了与其他开发人员在功能上进行协作。如果您向本地分支添加跟踪引用,则可以方便地拉取其他人对该分支所做的更改,因此我会说在大多数情况下您希望添加跟踪引用。

回答by Simon Tewsi

Nick Quaranto's excellent blog git readyhas a post explaining remote tracking branches:

Nick Quaranto 的优秀博客git ready有一篇解释远程跟踪分支的帖子:

Remote-tracking branches have a few different purposes:

  • They're used to link what you're working on locally compared to what's on the remote.

  • They will automatically know what remote branch to get changes from when you use git pull or git fetch.

  • Even better, git status will recognize him how many commits you are in front of the remote version of the branch.

远程跟踪分支有几个不同的目的:

  • 与远程工作相比,它们用于链接您在本地工作的内容。

  • 当您使用 git pull 或 git fetch 时,他们会自动知道要从哪个远程分支获取更改。

  • 更好的是, git status 会认出他你在远程版本的分支前面有多少提交。

回答by whatsupanna

yes you likely want to add it. do this in the console: git branch --set-upstream-to origin/master

是的,您可能想添加它。在控制台中执行此操作: git branch --set-upstream-to origin/master