Why git fetch does not create local branches?

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?

Upvotes: 5

Views: 2476

Answers (3)

Bruno Siqueira
Bruno Siqueira

Reputation: 892

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://[email protected]
    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/*

Upvotes: 1

Romain Valeri
Romain Valeri

Reputation: 22067

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.

Upvotes: 2

Obsidian
Obsidian

Reputation: 3912

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.

Upvotes: 2

Related Questions