4

From what I understand, whenever you run git fetch remote objects are downloaded locally and a lightweight pointer named FETCH_HEAD to the HEAD commit of the remote branch is created.

Since a branch is simply a pointer, how is this different from creating a local branch? What design considerations support the case for not creating a local branch whenever using git fetch?

Simón Ramírez Amaya
  • 2,436
  • 2
  • 14
  • 31

3 Answers3

1

Check out this entry What does FETCH_HEAD in Git mean? .

Since a branch is simply a pointer, how is this different from creating a local branch? What design considerations support the case for not creating a local branch whenever using git fetch?

Because git fetch is meant to repatriate the state of known remote branches and associated missing objects. This is different from having an homolog local branch that, in this case, would probably be configured to track its remote homolog.

Branches are very often configured to automatically create that local branches, indeed, but only once you check them out first with git checkout.

Obsidian
  • 3,719
  • 8
  • 17
  • 30
1

git fetch actually can create some new branches, locally, but not yours. For each new branch it creates a remote-tracking branch, an image of the remote state, with which you can't interact like with your local branches, the ones listed on git branch.

If some new branches have been created on your remote since last time you fetched, git will get their new references, with all needed ancestry.

Example :

On your local repo

A---B---C---D <<< master, origin/master

On the remote "origin", where work has been done (a new branch, and master has advanced)

A---B---C---D---G <<< master
             \
              \
               E---F <<< new-feature

If you fetch at this point, you'll get a new reference new-feature (which you can verify with git branch -r), and origin/master will be updated to point to G, but not master, which will still be unchanged.

               G <<< origin/master
              /
             /
A---B---C---D <<< master
             \
              \
               E---F <<< origin/new-feature

And then it also allows you to inspect these new changes before deciding whether and how to integrate them to your local work.

Romain Valeri
  • 19,645
  • 3
  • 36
  • 61
  • I agree that whenever you are fetching from a remote this is the case. You can check "your" branches and *remote-tracking* branches with git branch -a. However when you fetch from a repo somewhere in the internet that is not one of your remotes the only new reference created is FETCH_HEAD. Why? – Simón Ramírez Amaya Apr 30 '19 at 20:21
0

I was working with a repository now, and every time I fetched a new remote branch, the command did not create the local one. Checking the .git/config file and comparing it with other repositories, I could see that the fetch info was different:

[remote "origin"]
    url = ssh://git@MYREPOURL.git
    fetch = +refs/heads/master:refs/remotes/origin/master

Once I changed it to the following value, it started working:

fetch = +refs/heads/*:refs/remotes/origin/*
Bruno Siqueira
  • 860
  • 11
  • 25