Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

git-fetch

While looking at the documentation for git fetch I noticed the --append option. The documentation says it does this:

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.

What use does this have? From what I understand, FETCH_HEAD keeps track of the tips of the remotes branche that were just fetched so that merge or rebase gets called on them.

Why would appending to the list matter? Why does keeping old fetch heads matter?

like image 890
Yazeed Sabri Avatar asked Feb 26 '18 23:02

Yazeed Sabri


People also ask

What does the fetch command do in git?

The git fetch command downloads commits, files, and refs from a remote repository into your local repo. Fetching is what you do when you want to see what everybody else has been working on.

What is git pull fetch?

git fetch is the command that tells your local git to retrieve the latest meta-data info from the original (yet doesn't do any file transferring. It's more like just checking to see if there are any changes available). git pull on the other hand does that AND brings (copy) those changes from the remote repository.

What is the benefit of git fetch?

It lets you see new branches and commit, and inspect changes that are ready to be merged into local branches without actually merging them. It also lets you slightly optimize your work flow to reduce network hits.

Which git command performs git fetch and merge?

Git fetch command and git pull command are both different in operation. Git fetch fetches the changes while git pull merges them after fetching. So in a way, git fetch is a part of git pull as it first fetches the changes and then performs git merge.


Video Answer


1 Answers

Sometimes you may need to fetch one branch at a time:

$ 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

Similarly you can fetch from multiple remotes, accumulating the results in .git/FETCH_HEAD instead of having there only the results of the most recent git fetch.

Then you can merge all branches registered in .git/FETCH_HEAD with a single git merge FETCH_HEAD operation1:

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 Whether it is a good practice, compared to merging each branch separately, is outside of the scope of this answer.

like image 159
Leon Avatar answered Sep 25 '22 15:09

Leon