Git 中的 FETCH_HEAD 是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9237348/
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 12:57:47  来源:igfitidea点击:

What does FETCH_HEAD in Git mean?

gitgit-pullgit-fetch

提问by Misha Moroshko

git pull --helpsays:

git pull --help说:

In its default mode, git pullis shorthand for git fetchfollowed by git merge FETCH_HEAD.

在其默认模式下,git pullgit fetch后跟 的简写git merge FETCH_HEAD

What is this FETCH_HEADand what is actually merged during git pull?

这是什么FETCH_HEAD以及在 期间实际合并的内容git pull

采纳答案by Cascabel

FETCH_HEADis a short-lived ref, to keep track of what has just been fetched from the remote repository. git pullfirst invokes git fetch, in normal cases fetching a branch from the remote; FETCH_HEADpoints to the tip of this branch (it stores the SHA1 of the commit, just as branches do). git pullthen invokes git merge, merging FETCH_HEADinto the current branch.

FETCH_HEAD是一个短期引用,用于跟踪刚刚从远程存储库中获取的内容。git pull首先调用git fetch,在正常情况下从远程获取分支;FETCH_HEAD指向这个分支的尖端(它存储提交的 SHA1,就像分支一样)。git pull然后调用git merge,合并FETCH_HEAD到当前分支。

The result is exactly what you'd expect: the commit at the tip of the appropriate remote branch is merged into the commit at the tip of your current branch.

结果正是您所期望的:适当远程分支尖端的提交合并到当前分支尖端的提交中。

This is a bit like doing git fetchwithout arguments (or git remote update), updating all your remote branches, then running git merge origin/<branch>, but using FETCH_HEADinternally instead to refer to whatever single ref was fetched, instead of needing to name things.

这有点像git fetch不带参数(或git remote update),更新所有远程分支,然后运行git merge origin/<branch>,但在FETCH_HEAD内部使用来引用获取的任何单个引用,而不是需要命名事物。

回答by Jonathan Mitchell

The FETCH_HEAD is a reference to the tip of the last fetch, whether that fetch was initiated directly using the fetch command or as part of a pull. The current value of FETCH_HEAD is stored in the .gitfolder in a file named, you guessed it, FETCH_HEAD.

FETCH_HEAD 是对上次提取的提示的引用,无论该提取是直接使用 fetch 命令启动还是作为拉取的一部分。FETCH_HEAD 的当前值存储在.git文件夹中的一个文件中,您猜对了,FETCH_HEAD.

So if I issue:

所以如果我发出:

git fetch https://github.com/ryanmaxwell/Fragaria

FETCH_HEAD may contain

FETCH_HEAD 可能包含

3cfda7cfdcf9fb78b44d991f8470df56723658d3        https://github.com/ryanmaxwell/Fragaria

If I have the remote repo configured as a remote tracking branch then I can follow my fetch with a merge of the tracking branch. If I don't I can merge the tip of the last fetch directly using FETCH_HEAD.

如果我将远程存储库配置为远程跟踪分支,那么我可以通过合并跟踪分支来跟踪我的提取。如果我不这样做,我可以使用 FETCH_HEAD 直接合并最后一次提取的提示。

git merge FETCH_HEAD

回答by Carsten Führmann

As mentioned in Jonathan's answer, FETCH_HEAD corresponds to the file .git/FETCH_HEAD. Typically, the file will look like this:

正如乔纳森的回答中提到, FETCH_HEAD 对应于文件.git/FETCH_HEAD. 通常,该文件将如下所示:

71f026561ddb57063681109aadd0de5bac26ada9                        branch 'some-branch' of <remote URL>
669980e32769626587c5f3c45334fb81e5f44c34        not-for-merge   branch 'some-other-branch' of <remote URL>
b858c89278ab1469c71340eef8cf38cc4ef03fed        not-for-merge   branch 'yet-some-other-branch' of <remote URL>

Note how all branches but one are marked not-for-merge. The odd one out is the branch that was checked out before the fetch. In summary: FETCH_HEAD essentially corresponds to the remote version of the branch that's currently checked out.

注意除一个之外的所有分支是如何标记的not-for-merge。奇怪的是在获取之前签出的分支。总之: FETCH_HEAD 本质上对应于当前签出的分支的远程版本。

回答by Ivan

I have just discovered and used FETCH_HEAD. I wanted a local copy of some software from a server and I did

我刚刚发现并使用了FETCH_HEAD. 我想要来自服务器的某些软件的本地副本,我做到了

git fetch gitserver release_1

gitserveris the name of my machine that stores git repositories. release_1is a tag for a version of the software. To my surprise, release_1was then nowhere to be found on my local machine. I had to type

gitserver是我存储 git 存储库的机器的名称。 release_1是软件版本的标签。令我惊讶的是,release_1在我的本地机器上找不到。我必须打字

 git tag release_1 FETCH_HEAD 

to complete the copy of the tagged chain of commits(release_1) from the remote repository to the local one. Fetch had found the remote tag, copied the commit to my local machine, had notcreated a local tag, but had set FETCH_HEADto the value of the commit, so that I could find and use it. I then used FETCH_HEADto create a local tag which matched the tag on the remote. That is a practical illustration of what FETCH_HEADis and how it can be used, and might be useful to someone else wondering why git fetch doesn't do what you would naively expect.

完成将标记的提交链(release_1) 从远程存储库复制到本地存储库。Fetch 找到了远程标签,将提交复制到我的本地机器,没有创建本地标签,但已设置FETCH_HEAD为提交的值,以便我可以找到并使用它。然后我用来FETCH_HEAD创建一个与遥控器上的标签匹配的本地标签。这是一个FETCH_HEAD关于它是什么以及如何使用它的实用说明,并且可能对其他人有用,因为他们想知道为什么 git fetch 没有按照你天真地期望的那样做。

In my opinion it is best avoided for that purpose and a better way to achieve what I was trying to do is

在我看来,最好避免为此目的,实现我想要做的更好的方法是

git fetch gitserver release_1:release_1

i.e. to fetch release_1 and call it release_1 locally. (It is source:dest, see https://git-scm.com/book/en/v2/Git-Internals-The-Refspec; just in case you'd like to give it a different name!)

即获取 release_1 并在本地调用它 release_1。(它是 source:dest,请参阅https://git-scm.com/book/en/v2/Git-Internals-The-Refspec;以防万一你想给它一个不同的名字!)

You might want to use FETCH_HEADat times though:-

不过,您FETCH_HEAD有时可能想使用:-

git fetch gitserver bugfix1234
git cherry-pick FETCH_HEAD

might be a nice way of using bug fix number 1234 from your Git server, and leaving Git's garbage collection to dispose of the copy from the server once the fix has been cherry-picked onto your current branch. (I am assuming that there is a nice clean tagged commit containing the whole of the bug fix on the server!)

可能是使用来自 Git 服务器的错误修复编号 1234 的好方法,并在修复已被挑选到当前分支后,让 Git 的垃圾收集处理来自服务器的副本。(我假设有一个漂亮干净的标记提交,其中包含服务器上的整个错误修复!)

回答by manojlds

git pull is combination of a fetch followed by a merge. When git fetch happens it notes the head commit of what it fetched in FETCH_HEAD (just a file by that name in .git) And these commits are then merged into your working directory.

git pull 是 fetch 和 merge 的组合。当 git fetch 发生时,它会记录它在 FETCH_HEAD 中获取的内容的头部提交(只是 .git 中该名称的文件)然后这些提交合并到您的工作目录中。