git fetch 命令中的 --append 选项有什么用?

What use does the --append option in git fetch command?

在查看 git fetch 的文档时,我注意到了 --append 选项。文档说它是这样做的:

Append ref names and object names of fetched refs to the existing contents of .git/FETCH_HEAD. Without this option old data in .git/FETCH_HEAD will be overwritten.

这个有什么用?据我了解,FETCH_HEAD 跟踪刚刚获取的远程分支的提示,以便在它们上调用 merge 或 rebase。

为什么附加到列表很重要?为什么保留旧的抓取头很重要?

有时您可能需要一次获取一个分支:

$ git fetch origin master

# FETCH_HEAD now points to origin/master
$ cat .git/FETCH_HEAD
1234567890abcdef        branch 'master' of https://git.example.com/someproject

# Let's fetch another branch
$ git fetch origin dev

# FETCH_HEAD is overwritten and points to origin/dev
$ cat .git/FETCH_HEAD
fedcba0987654321        branch 'dev' of https://git.example.com/someproject

# Fetch origin/master again, preserving the previous contents of .git/FETCH
$ git fetch --append origin master

# FETCH_HEAD now contains pointers to both origin/master and origin/dev
$ cat .git/FETCH_HEAD
fedcba0987654321        branch 'dev' of https://git.example.com/someproject
1234567890abcdef        branch 'master' of https://git.example.com/someproject

同样,您可以从多个遥控器获取,将结果累积在 .git/FETCH_HEAD 中,而不是只有最近 git fetch.

的结果

然后你可以用一个git merge FETCH_HEAD操作合并在.git/FETCH_HEAD中注册的所有分支1:

When FETCH_HEAD (and no other commit) is specified, the branches recorded in the .git/FETCH_HEAD file by the previous invocation of git fetch for merging are merged to the current branch.


1 与单独合并每个分支相比,这是否是一个好的做法,不在本答案的范围内。