Thayne
Thayne

Reputation: 7002

Fetch only local branches by default?

So, I have a repository that has a large number of remote branches. Pulling down all of these branches takes a long time and uses a lot of space.

What I'd like to do is, by default, only pull references that correspond to local branches.

I can sort-of accomplish this by changing the default git config from something like this:

[remote "origin"]
    fetch = +refs/heads/*:refs/remotes/origin/*

to

[remote "origin"]
    fetch = +refs/heads/master:refs/remotes/origin/master
    # additional fetch lines for other branches that should be fetched

but this has the unfortunate side effect that if I push a new branch with --set-upstream, it doesn't automatically add a remote ref (in refs/remotes/origin), and doesn't fetch that branch as part of a git fetch. Also, git status no longer reports on the status compared to the upstream remote branch, even if the reference for the remote actually exists.

Is there a better way to do this, or to automatially add refspec to the remote.*.fetch config when creating new branches?

Upvotes: 1

Views: 143

Answers (1)

LeGEC
LeGEC

Reputation: 51988

You can add a refspec from the command line :

git config --add remote.origin.fetch +refs/heads/newbranch:refs/remotes/origin/newbranch

You can use this for example to create a shortcut :

git config alias.rfspec '! f () { git config --add remote.origin.fetch +refs/heads/$1:refs/remotes/origin/$1; }; f'

# usage :
git rfspec new/branch

or include it in a more elaborate script, which would for example create the refspec and push in one go :

# in file git-pushnew :

#!/bin/bash

# first argument is branch :
br="$1"
if [ -z "$br" ]; then
# set default to : current active branch
    br=$(git rev-parse HEAD)
fi

# add the refspec for this branch :
git config --add remote.origin.fetch +refs/heads/$br:refs/remotes/origin/$br
# push this specific branch to origin :
git push -u origin $br

If you set this file as executable, and it is accessible from your PATH, you can type :

git pushnew
# or
git pushnew that/branch

You may create a git-fetchnew script in a similar way, which would fetch and add to the refspec a specific branch.

Upvotes: 1

Related Questions