git pull是否在当前分支上运行? [英] Does git pull operate on the current branch?
问题描述
git pull
是否在分支上运行是否正确?
Is it correct that git pull
operates on branches?
在存储库中,当在不同分支上运行git pull
时,它会做不同的事情吗?
In a repository, when running git pull
on different branches, will it do different things?
当未在git pull
之后指定任何参数时,
When not specifying any argument after git pull
,
- 它仅从远程
origin
中的同名分支拉到当前分支吗? - 它对其他任何分支机构有作用吗?
- does it only pull to the current branch from a same-name branch in the remote
origin
? - does it do anything for any other branch?
谢谢.
推荐答案
首先,不要使用git pull
.完全不需要此命令,如果您是Git管理员,请避免使用它,这样会更好.但是,如果您坚持使用它,因为它很方便-并且很方便-请记住,它只先运行git fetch
,然后根据各种情况通常再运行git merge
. /p>
First, don't use git pull
. This command is never needed, and you will be better off if you avoid it until you are a Git-master. But if you insist on using it, because it's convenient—and it is convenient—remember that it just runs git fetch
first, and then, depending on various things, usually runs git merge
second.
git pull
在分支上运行是否正确?
Is it correct that
git pull
operates on branches?
是的-但最好将git pull
视为运行两个 Git命令,因为这就是git pull
的作用.
Yes—but it's better to think of git pull
as running two Git commands, because that's what git pull
does.
在存储库中,当在不同分支上运行
git pull
时,它会做不同的事情吗?
In a repository, when running
git pull
on different branches, will it do different things?
不清楚在不同的分支机构"和做不同的事情"是什么意思.您可能会说:
It's not clear what you mean by "on different branches" and "do different things". You might mean:
git checkout branch1; git pull; git checkout branch2; git pull
或:
git checkout branch1; git pull origin branch2 branch3
,后者非常不同. (何时使用此表单"的简短答案是永远不会".长答案是尚未:首先了解章鱼的合并方法,然后重新阅读文档.然后,一旦您是Git,大师,您会意识到,无论如何,章鱼都绝对不能直接与git pull
合并,但是至少现在您知道什么时候可以安全使用git pull
了.")
and the latter is very different. (The short answer to "when to use this form" is never. The long answer is "Not yet: first learn about octopus merges, then re-read the documentation. Then, once you are a Git master, you will realize that you probably should never do an octopus merge directly with git pull
anyway, but at least now you know when it's safe to use git pull
at all.")
前者-在branch1
上运行git pull
,然后在branch2
上再次运行git pull
-只需经过以下四个步骤两次,一次在branch1
上,然后再一次在
The former—running git pull
while on branch1
, then running git pull
again while on branch2
—just goes through the four steps listed below twice, once on branch1
, and then again on branch2
.
在
git pull
之后不指定任何参数时,
When not specifying any argument after
git pull
,
- 它仅从远程
origin
中的同名分支拉到当前分支吗? - 它对其他任何分支机构有作用吗?
- does it only pull to the current branch from a same-name branch in the remote
origin
? - does it do anything for any other branch?
让我们先处理第二部分,因为这很容易:不,只要您不谈论远程跟踪分支."
Let's dispose of the second part first, because that's easy: "no, as long as you are not talking about remote-tracking branches."
接下来,我们需要注意一个假设:遥控器名为origin
.您可以有多个遥控器,如果这样做,显然其中最多一个被命名为origin
.即使只有一个遥控器,也可以随心所欲地调用它.因此,以遥控器为origin
的想法开始时有点摇摇欲坠.
Next, we need to note an assumption: that the remote is named origin
. You can have more than one remote, and if you do, obviously at most one of them is named origin
. Even if you have only one remote, you can call it whatever you want. So the idea that the remote is origin
is a bit shaky to start with.
(不过,通常情况下,它是 origin
.大多数人只有一个遥控器,并且命名为origin
,因此只有 the 遥控器,而不是可以选择的七个不同的遥控器之一".
(Usually, though, it is origin
. Most people have just one remote, and it's named origin
, so there's only the remote, not "one of seven different remotes to pick from" or whatever.)
在不带任何参数的情况下运行git pull
时,Git将:
When you run git pull
with no arguments, Git will:
-
标识当前分支的上游.例如,
branch1
的上游可能是origin/branch1
.请注意,此上游有两个部分:
Identify the current branch's upstream. For instance, the upstream of
branch1
is probablyorigin/branch1
. Note that there are two parts to this upstream:
- 遥控器的名称,例如
origin
. - 在远程服务器上找到的分支的名称,例如
branch1
.
- The name of the remote, such as
origin
. - The name of a branch found on the remote, such as
branch1
.
要创建远程跟踪分支,Git会将这两个部分有效地粘贴在一起,这就是为什么我们将其视为origin/branch1
的原因.但是仍然有两个部分.
To make a remote-tracking branch, Git effectively pastes these two parts together, which is why we see it as origin/branch1
. But there are still the two parts.
不要求上游分支名称与本地分支名称匹配.这只是一个好主意.
There is no requirement that the upstream branch name match the local branch name. It's just a good idea.
(有时,当您有两个或多个远程控制器时,您必须违反此好主意".例如,假设远程fred
具有一个名为develop
的分支,而另一个远程susan
具有一个分支命名为develop
.您现在在您的存储库中同时具有fred/develop
和susan/develop
. fred-develop
和另一个susan-develop
.但是现在上游名称不再与本地名称匹配:fred-develop
的上游名称是fred/develop
,而不是fred/fred-develop
.)
(There are times when you must violate this "good idea", when you have two or more remotes. Suppose, for instance, that remote fred
has a branch named develop
and different remote susan
has a branch named develop
. You now have both fred/develop
and susan/develop
in your repository. You want to do something with both of those; what branch names will you use? Maybe you can call one fred-develop
and the other susan-develop
. But now the upstream name doesn't match the local name any more: the upstream for fred-develop
is fred/develop
, not fred/fred-develop
.)
将此上游分为两个部分:远程部分和分支名称,如远程所示.
Split up this upstream into its two parts: remote, and name-of-branch as seen on the remote.
使用几个参数运行git fetch
. git fetch
步骤将使用遥控器的URL调用另一个Git.一旦您的Git在互联网电话上安装了外部Git,您的Git将获得他们在其分支上所拥有的任何新提交,而您还没有任何提交.
Run git fetch
with several arguments. The git fetch
step will call up another Git using the URL for the remote. Once your Git has the foreign Git on the Internet-phone, your Git will obtain any new commits they have on their branch, that you do not have anywhere yet.
比方说,当前分支为branch1
,上游分支为origin/branch1
.如果您自己的Git不太古老(至少是1.8.4版),则会更新您的origin/branch1
. (如果您的Git早于该版本,则git pull
提供的参数将阻止更新您的远程跟踪分支.这不是一个好情况,您应该更新您的Git版本.所有都可以,这很难解释,如果您完全避免使用git pull
,那么Git 1.8.3和更早版本执行此操作的怪异方式就不再重要了,因为git fetch origin
更新了 all 分支,甚至在这些古老版本的Git中也是如此.)
Let's say the current branch is branch1
and the upstream is origin/branch1
. If your own Git is not too ancient (is at least version 1.8.4), this updates your origin/branch1
. (If your Git is older than that, the arguments that git pull
provides prevent your remote-tracking branch from being updated. This is not such a good situation, and you should update your Git version. It all works, it's just hard to explain. If you avoid git pull
entirely, the weird way that Git 1.8.3 and earlier does this no longer matters, because git fetch origin
updates all your remote-tracking branches, even in these ancient versions of Git.)
现在git fetch
在您的 存储库中有这些新提交,在origin/branch1
远程跟踪分支下,您都已设置好.以后的git fetch
将很快完成,因为您现在拥有那些新的提交.
Now that git fetch
has those new commits in your repository, under your origin/branch1
remote-tracking branch, you're all set. A later git fetch
will finish very quickly, because you now have those new commits.
运行git merge
,或其他一些Git命令,并带有多个参数.
Run git merge
, or some other Git command, with several arguments.
这是最后一个步骤-通常是git merge
-影响您的分支.当git merge
成功时,它通常进行新的提交.与所有普通的Git命令一样,进行 new 提交会将提交添加到当前分支中.当git merge
执行快进而不是进行新提交时, 也会影响您当前的分支.
It's this last step—usually git merge
—that affects your branch. When git merge
succeeds, it often makes a new commit. As with all normal Git commands, making a new commit adds the commit to your current branch. When git merge
does a fast-forward instead of making a new commit, that also affects your current branch.
因此,我们可以这样说:当git pull
运行git merge
时,git merge
步骤会影响您当前的分支,就像git merge
总是会影响您的当前分支一样分支.
Hence, we can say this: When git pull
runs git merge
, the git merge
step affects your current branch in the same way that git merge
always affects your current branch.
不过,您可以要求git pull
运行另一个 命令.具体来说,您可以进行设置,以使git pull
运行git rebase
而不是git merge
.要弄清楚它的作用,我们必须看一下git rebase
的作用,这更加复杂-但最终,它也会像git merge
一样影响您当前的分支. 1 因此,我们也可以说:当git pull
运行git rebase
时,git rebase
步骤以与git rebase
始终 1 影响的方式相同的方式影响您的当前分支.您当前的分支.
You can ask git pull
to run a different second command, though. Specifically, you can set things up so that git pull
runs git rebase
instead of git merge
. To figure out what that does, we must look at what git rebase
does, and that's more complicated—but in the end, it also affects your current branch, just like git merge
.1 Hence we can also say: When git pull
runs git rebase
, the git rebase
step affects your current branch in the same way that git rebase
always1 affects your current branch.
当我们将所有这些事实放在一起时,最终看到git pull
的 second 命令是影响当前分支的因素.而且,它 only 会影响当前分支,因为git merge
和git rebase
在当前分支上仅 起作用.
When we put all these facts together, we end up seeing that git pull
's second command is what affects the current branch. And, it only affects the current branch, because git merge
and git rebase
work only on the current branch.
( first 命令-git fetch
步骤会影响远程跟踪分支.但是由于这些只是Git记住外来对象的方式Git,上一次它是从国外Git那里得到的东西,这不是很重要.)
(The first command—the git fetch
step—affects remote-tracking branches. But since those are just your Git's way of remembering what it got from a foreign Git, the last time it got things from that foreign Git, that's not very important.)
最后,请勿使用git pull
.使用git fetch
,然后使用根据fetch
附带的内容选择的命令:git merge
或git rebase
.
Last, don't use git pull
. Use git fetch
, followed by whichever command you choose based on what came in with the fetch
: git merge
or git rebase
.
1 有一种方法可以使git rebase
影响其他分支,但是-幸运的是,git pull
不使用它. (除非您知道自己在做什么,否则您可能也不应该这样做.这非常简单:它实际上只是先对另一个分支名称进行git checkout
,然后像您自己进行此操作一样进行,并且未指定额外的分支名称.)
1There is a way to make git rebase
affect a different branch, but—fortunately—git pull
doesn't use it. (You probably shouldn't either, unless you know what you are doing. It is pretty trivial: it literally just does a git checkout
of another branch-name first, then proceeds as if you had done this yourself, and had not specified the extra branch-name.)
这篇关于git pull是否在当前分支上运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!