How to stop fetching a given branch from a given remote?

I’ve just added a new remote to my local repository and fetched, now I got all the branches from the new remote fetched. There’s one branch I have no interest, and I want to stop fetching it when I do git fetch <new remote>.

How do I stop fetching such remote branch?

  • git create patch with diff
  • Unable to execute DX - IntelliJ Idea (Android + Maven)
  • Merge or Rebase or Branch Default In Android Studio? What are the differences?
  • Is git pull --rebase safe if you are in a centralized model and never push force?
  • Git stash two branches
  • Can I pull only certain files from another git repository?
  • I’ve done git branch -d -r <new remote>/<non-wanted branch> but I need to do something more to stop refetching it at each git fetch <new remote>.

  • Is it possible to have a git repository as a “vendor branch” in subversion?
  • Your branch is ahead of 'origin/master' by 3 commits
  • Looking for a way automate the “bump version” with git flow
  • MATLAB git by command window
  • How to tell who was the author of removed line?
  • Jenkins Git SSH keys on Windows slave
  • 2 Solutions collect form web for “How to stop fetching a given branch from a given remote?”

    You do not. There is no benefit from not fetching them. You get the reference <remote>/<branch-name> added, which you can use, if you want, but don’t have to. And all the objects are fetched anyway, so there is no performance/storage size improvement.

    Just ignore the branches of that remote. Your local branches are not affected by this.

    If you didn’t know: a remote branch is nothing more than a file at .git/refs/remotes/<remote-name>/<branch-name> that contains the commit ID. This file should do you no harm 🙂

    As opatut noted, there’s little if any benefit to “not fetching” the remote’s branch references: just don’t use the ones you don’t care about.

    If you have some automated thingy that insists on using them [edit: and want to blacklist certain branches—if you just want to white-list, see below], there are two workarounds:

    • delete them every time (requires doing something after git fetch)
    • use the trick described below (also requires doing something after git fetch)

    A “remote branch” is simply a reference of the form refs/remotes/rmt/br, where rmt is the name of the remote (e.g., origin) and br is the name of the branch (e.g., master). What creates them is this line that you will see in your repo’s .git/config file (run git config -e to bring it up in your editor):

    fetch = +refs/heads/*:refs/remotes/origin/*
    

    This tells git fetch that it should take all the ref names matching refs/heads/* and update them (forcibly, because of the + character—otherwise they would be limited to fast-forward updates) but change their name to refs/remotes/origin/*, where the * part obviously matches.

    Thus, if you change this to:

    fetch = +refs/heads/*:refs/real-remotes/origin/*
    

    the git fetch operation will update (or create if needed) refs/real-remotes/origin instead of refs/remotes/origin.

    You can then do:

    git for-each-ref refs/real-remotes/origin/
    

    to enumerate all these refs (with all the options available in git for-each-ref), and run them through scripts that remove any black-listed ones and update the remaining ones in refs/remotes/origin/. For instance:

    git for-each-ref refs/real-remotes/origin/ |
    while read sha1 reftype refname; do
        shortname=${refname#refs/real-remotes/origin/}
        if [ $shortname != master ]; then
            git update-ref refs/remotes/origin/$shortname $sha1
        fi
    done
    

    This updates everything except master (which is a very odd thing to do, but hey, we have just done it). (Side note: it “hard-links”, as it were, a remote ref that is symbolic, e.g., if there’s a HEAD that is a ref: ref/remotes/origin/master this turns it into whatever that would refer to. Use git symbolic-ref to test for such refs, although it’s up to you to figure out what to do with them, once you find any.)

    (The primary difference between this and explicitly deleting some particular remote branch is that this version leaves the remote branch around, without updating it. For instance, this leaves origin/master unmoved; the “real” remote master is now only in refs/real-remotes/origin/master.)

    [Edit: if you just want to white list particular branches, just list them explicitly:

    fetch = +refs/heads/master:refs/remotes/origin/master
    fetch = +refs/heads/devel:refs/remotes/origin/devel
    

    Use as many fetch = lines as needed. Note that git fetch will error out if the remote no longer has one of these branches, though.]

    Git Baby is a git and github fan, let's start git clone.