2

I have 4 remote branches: A, B, C, D. A and B aren't anymore relevant to me but I want to keep them remotely. So when I execute git fetch I want only C and D to be fetched.

Amio.io
  • 20,677
  • 15
  • 82
  • 117

1 Answers1

3

How to checkout specific branch

# fetch a remote branch from remote but the catch here is that the branch
# will not have a tracking branch. It will be checked out locally. 
git fetch <remote> <remote branch>:refs/remotes/<remotename>/<local branch>

Few more notes: Git doesn't care where your commits are.

Git has a big content file (.pack) which store all the content of the repository. When you execute fetch it's simply executing all the content from the remote. Git doesn't "see" any branches inside that file.

When you checkout branch git checkout the latest commit of this branch. You can of course move and change the HEAD and set it to any other commit.

The point is that git doesn't care about tags, branches etc. it simply track the content and since sommit store its parent git know how to extract the "previous" commit from this file.


Summary

Git doesn't track branches or tags. Its simply stores their SHA-1 internally and knows how to extract them when needed.


Community
  • 1
  • 1
CodeWizard
  • 128,036
  • 21
  • 144
  • 167
  • Thx for explanation. We create a feature branch for each task. I would like to keep all the branches but when I `git fetch` I receive a bunch of them even the ones that are super old. Using `git fetch --depth=` I could limit the branches but is there any other way? – Amio.io Apr 18 '16 at 07:29
  • Why do you care if the branches are fetched locally? you can also do this to remove old deleted data from server: `git fetch --all --prune` – CodeWizard Apr 18 '16 at 07:30
  • 1
    The depth only limit the number of commits to fetch. Same as in `git clone --depth` but it has nothing to do with not fetching the other branches. – CodeWizard Apr 18 '16 at 07:31
  • I care so that in team somebody can fetch branches and select from them. Is there a way how to keep branches in remote yet not download them using `fetch`? – Amio.io Apr 18 '16 at 07:34
  • They are there, you don't need to fetch them. If you wish to use them without fetch you can simply check them out (detached HEAD) and create a local branch to work on them. if you have branch A on remote: `git checkout origin/A` now you are in detached head (since you did not checked out local branch) and you will have to create branch in order to update it – CodeWizard Apr 18 '16 at 07:38
  • I am not explaining myself correctly. I know how to create branch, how to download it locally and push back to it. Let's try it this way. I have 4 remote branches: A, B, C, D. A and B aren't anymore relevant to me but I want to keep them remotelly. So when I execute `git fetch` I want only C and D to be fetched. Is it a better explanation now? – Amio.io Apr 18 '16 at 07:42
  • I hope i get you now: `git fetch :refs/remotes//` This will fetch the desired branch but will not set up a tracking branch for it. – CodeWizard Apr 18 '16 at 07:45
  • I am going to modify the question with my example and you can respond with your last comment. Then I accept it. Thx CodeWizard. ;) – Amio.io Apr 18 '16 at 07:49