从原点拉取时“git pull”命令之间的差异?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15797183/
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
Differences between "git pull" commands when pulling from origin?
提问by Ben Gao
What are the differences between these commands?:
这些命令之间有什么区别?:
# 1
git pull
# 2
git pull origin
# 3
git pull origin master
# 4
git pull origin/master
# 5
git pull origin HEAD:master
回答by michas
git pull
is a convenience command, which is doing different things at the same time. Basically it is just a combination of git fetch
, which connects to the remote repository and fetches new commits, and git merge
(or git rebase
) which incorporates the new commits into your local branch. Because of the two different commands involved the meaning of git pull
is not always obvious.
git pull
是一个方便的命令,它同时做不同的事情。基本上它只是git fetch
, 连接到远程存储库并获取新提交,和git merge
(或git rebase
)将新提交合并到本地分支的组合。由于涉及两个不同的命令,其含义git pull
并不总是很明显。
You can configure an upstream for a local branch. After a fresh clone you will have a local branch "master", a remote "origin" and your master branch has "origin/master" as upstream.
I assume this setup below. (You can see your upstream configuration with git branch -vv
or by looking at .git/config.)
您可以为本地分支配置上游。新克隆后,您将拥有一个本地分支“master”、一个远程“origin”,并且您的 master 分支将“origin/master”作为上游。我假设下面的这个设置。(您可以使用git branch -vv
.git/config 或通过查看 .git/config来查看您的上游配置。)
Now for your questions:
现在回答您的问题:
git pull
=git fetch origin
+git merge origin/master
(or whatever your upstream is)git pull origin
=git pull
(as long as origin is your upstream remote)git pull origin master
=git fetch origin master
+git merge FETCH_HEAD
git pull origin/master
: invalid unless you have a remote called "origin/master"git pull origin HEAD:master
: Tries to directly reset you local master to whatever HEAD points to on origin. (Don't do this.)
git pull
=git fetch origin
+git merge origin/master
(或任何你的上游)git pull origin
=git pull
(只要来源是您的上游遥控器)git pull origin master
=git fetch origin master
+git merge FETCH_HEAD
git pull origin/master
: 无效,除非你有一个叫做“origin/master”的遥控器git pull origin HEAD:master
:尝试将您的本地主机直接重置为 HEAD 指向的任何原点。(不要这样做。)
回答by Noufal Ibrahim
A pull
is basically a fetch
(which gets some commits and associated objects from a remote repository into yours) and then an operation which "applies" these into your working copy. The second stage is, by default, done using a merge
but you can set the pull.rebase
variable to true
and then it will rebase instead.
Apull
基本上是一个fetch
(它将一些提交和关联对象从远程存储库获取到您的存储库中),然后是一个将这些“应用”到您的工作副本中的操作。默认情况下,第二阶段是使用 a 完成的,merge
但您可以将pull.rebase
变量设置为true
,然后它将变基。
There are two questions that pop up with the pull
command. The first is, what exactly gets fetched? And the second is, how does it apply these changes to my working copy? Let's start with the first. The full form of the command is
pull
命令会弹出两个问题。首先是,究竟获取了什么?第二个是,它如何将这些更改应用于我的工作副本?让我们从第一个开始。命令的完整形式是
git pull [options] [repository] [<refspec>...]
The options
are flags that control behaviour (e.g. --rebase to make pull
work as a fetch
+ rebase
even if pull.rebase
is false
).
这些options
是控制行为的标志(例如 --rebase 使pull
工作作为fetch
+rebase
即使pull.rebase
是false
)。
repository
is the name (or URL) of the remote to fetch from.
repository
是要从中获取的遥控器的名称(或 URL)。
refspecs
are a succinct way of specifying which references on the remote you want to fetch and where do you want to put them in your current working copy.
refspecs
是一种简洁的方式,用于指定要获取的远程引用以及要将它们放在当前工作副本中的哪个位置。
Let's take the most explicit form first.
让我们首先采用最明确的形式。
git pull origin branch1:branch2
This basically says, pull the changes in the reference branch1
on the remote called origin
and then merge (or rebase) them into the local branch branch2
. If I, for example, say git pull origin master:dev
, I will get a local branch called dev
which will point to the same commit as master
. The details of how to specify refspecs are here. You can use a *
to indicate multiple refspecs. For example, git pull origin refs/heads/*:refs/heads/*
will pull all the branches (stored under heads
) into the local repository and merge them into local branches with the same names.
这基本上是说,在被branch1
调用的远程引用中拉取更改origin
,然后将它们合并(或变基)到本地分支中branch2
。例如,如果我说git pull origin master:dev
,我将得到一个名为的本地分支dev
,该分支将指向与master
. 有关如何指定 refspecs 的详细信息,请参见此处。您可以使用 a*
来指示多个参考规范。例如,git pull origin refs/heads/*:refs/heads/*
将所有分支(存储在 下heads
)拉入本地存储库并将它们合并到具有相同名称的本地分支中。
Now, let's remove arguments one by one to discuss how the default work. First, we can remove the destination from our refspec and simply say git pull origin branch1
. This will first fetch
the remote branch branch1
into your local repository. It will be available as a temporary reference called FETCH_HEAD
. After that, it will run git merge FETCH_HEAD
which will merge this branch into your current active branch (i.e. HEAD
). This is often done when you're in a local branch and want to fetch changes from a remote into that branch.
现在,让我们一一删除参数来讨论默认值是如何工作的。首先,我们可以从我们的 refspec 中删除目的地并简单地说git pull origin branch1
. 这将首先fetch
将远程分支转移branch1
到您的本地存储库中。它将作为临时参考提供,称为FETCH_HEAD
。之后,它将运行git merge FETCH_HEAD
,将这个分支合并到您当前的活动分支(即HEAD
)。当您在本地分支中并希望从远程获取更改到该分支时,通常会执行此操作。
Now, let's drop the branch1
completely and just say git pull origin
. Now, git knows where to fetch from (origin
) but doesn't know what to fetch. It has some defaults for this. The most scenario is when your config file has a branch.<name>.merge
option (this is an entry called merge
inside a section like [branch "master"]
). If so, it will use the refspecs there for the operation.
现在,让我们branch1
完全放弃,只说git pull origin
. 现在,git 知道从 ( origin
)哪里获取,但不知道要获取什么。它有一些默认设置。大多数情况是当您的配置文件有一个branch.<name>.merge
选项时(这是merge
在像 那样的部分中调用的条目[branch "master"]
)。如果是这样,它将使用那里的 refspecs 进行操作。
If we drop the origin
completely and simply say git pull
, it will check the config to see if there is a branch.<name>.remote
which specifies which remote to pull from. That along with the above tells you what to pull.
如果我们origin
完全删除并简单地说git pull
,它将检查配置以查看是否有branch.<name>.remote
指定从哪个远程拉取的 。连同上面的内容一起告诉你要拉什么。
Your points #4 and #5 are not normal use cases. The first makes sense if you have a remote called origin/master
which is unlikely. origin/master
is usuallya local reference that tracks the master
branch on the remote origin
. The second will try to fetch changes on HEAD
on the remote (the default branch which is usually master
) and then merge those into your local master
. While this might be something you want to do on a regular basis, the command is quite unconventional and not something I've seen used often.
您的第 4 点和第 5 点不是正常用例。如果你有一个origin/master
不太可能被调用的遥控器,第一个是有意义的。origin/master
是通常用于跟踪本地参考master
遥控器上的分支origin
。第二个将尝试HEAD
在远程(通常是默认分支master
)上获取更改,然后将它们合并到您的本地master
. 虽然这可能是您想要定期执行的操作,但该命令非常非常规,而且我看到的不是经常使用的命令。
I've skipped a few details but these should be enough to keep you safe and comfortable in your daily work. For all the gory details, you can check out the manual page for git pull
.
我跳过了一些细节,但这些应该足以让您在日常工作中保持安全和舒适。有关所有血腥详细信息,您可以查看git pull
.