Search code examples
gitgit-fetch

Make Git not track all branches


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.


Solution

  • 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.