Git拉——帮助说:
在默认模式下,git pull是git fetch的简写,后面跟着git merge FETCH_HEAD。
FETCH_HEAD是什么? git拉取过程中合并了什么?
Git拉——帮助说:
在默认模式下,git pull是git fetch的简写,后面跟着git merge FETCH_HEAD。
FETCH_HEAD是什么? git拉取过程中合并了什么?
当前回答
FETCH_HEAD是对最后一次获取的尖端的引用,无论该获取是直接使用fetch命令启动的,还是作为拉取的一部分。FETCH_HEAD的当前值存储在。git文件夹中一个名为FETCH_HEAD的文件中。
所以如果我发出:
git fetch https://github.com/ryanmaxwell/Fragaria
FETCH_HEAD可以包含
3cfda7cfdcf9fb78b44d991f8470df56723658d3 https://github.com/ryanmaxwell/Fragaria
如果我将远程回购配置为远程跟踪分支,那么我可以使用跟踪分支的合并来跟踪我的fetch。如果我不这样做,我可以合并尖端的最后获取直接使用FETCH_HEAD。
git merge FETCH_HEAD
其他回答
FETCH_HEAD是一个短命的引用,用于跟踪刚刚从远程存储库获取的内容。Git pull首先调用Git fetch,在正常情况下从远程获取一个分支;FETCH_HEAD指向这个分支的顶端(它存储提交的SHA1,就像分支一样)。git pull然后调用git merge,将FETCH_HEAD合并到当前分支。
结果正是您所期望的:适当远程分支顶端的提交合并到当前分支顶端的提交。
这有点像不带参数的git获取(或git远程更新),更新所有远程分支,然后运行git merge origin/<branch>,但在内部使用FETCH_HEAD来引用所获取的任何单个引用,而不需要命名东西。
Git pull是一个fetch和merge的组合。当git获取发生时,它会注意到它在FETCH_HEAD中获取的内容的头提交(只是一个在.git中同名的文件),然后这些提交被合并到你的工作目录中。
我只是试图下拉一个(补丁)分支,我从GitHub直接进行更改创建。 该分支只出现在GH上。当我试着做g拉时,树枝没有出现。
我能够使用以下命令签出分支:
git fetch origin pull/2/head
git checkout -b <desired-branch-name> FETCH_HEAD
正如Jonathan的回答中提到的,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>
注意除了一个分支之外的所有分支都标记为not-for-merge。奇怪的是在取回之前检出的分支。总之:FETCH_HEAD本质上对应于当前签出的分支的远程版本。
我刚刚发现并使用了FETCH_HEAD。我想从服务器上获得一些软件的本地副本,我做到了
git fetch gitserver release_1
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 not created a local tag, but had set FETCH_HEAD to the value of the commit, so that I could find and use it. I then used FETCH_HEAD to create a local tag which matched the tag on the remote. That is a practical illustration of what FETCH_HEAD is 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.
在我看来,为了达到这个目的,最好避免这样做,而实现我想要做的更好的方法是
git fetch gitserver release_1:release_1
例如,获取release_1并在本地调用它。(来源:dest,见https://git-scm.com/book/en/v2/Git-Internals-The-Refspec;以防万一你想给它一个不同的名字!)
你可能想在某些时候使用FETCH_HEAD:-
git fetch gitserver bugfix1234
git cherry-pick FETCH_HEAD
从你的Git服务器上使用bug修复号1234可能是一个很好的方法,并且一旦修复被选中到你当前的分支上,让Git的垃圾收集来处理服务器上的副本。(我假设在服务器上有一个干净的标记提交,包含了整个错误修复!)