Aaron Fi
Aaron Fi

Reputation: 10396

Is there a way to git ignore entire branches?

This might be against the design philosophy of git, but:

in my org, I am constantly pulling down thousands of developer branches, named e.g. Dev/<login>/<something...>

I'd much rather have git, by default, not pull down these branches. And then, if it turns out I do need to collaborate in a specific branch, I would just explicitly pull it down.

Is this possible? (My scant research indicates no)

Thanks

Upvotes: 2

Views: 487

Answers (2)

user3035850
user3035850

Reputation:

git-pull uses git-fetch.

See https://git-scm.com/docs/git-fetch for its documentation, especially the "Configure remote-tracking branches" section (attached verbatim below).

Looking at that, you can only whitelist patterns, not exclude some. What you could do therefore is one of:

  • Manually list the branches you want to pull from instead of the wildcard
  • Get everyone to use a prefix for all branches, so that you can distinguish between Dev/* and Foo/*

git fetch allows you to configure remote.<repository>.fetch configuration variables.

Typically such a variable may look like this:

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

This configuration is used in two ways:

When git fetch is run without specifying what branches and/or tags to fetch on the command line, e.g. git fetch origin or git fetch, remote.<repository>.fetch values are used as the refspecs—​they specify which refs to fetch and which local refs to update. The example above will fetch all branches that exist in the origin (i.e. any ref that matches the left-hand side of the value, refs/heads/) and update the corresponding remote-tracking branches in the refs/remotes/origin/ hierarchy.

Upvotes: 3

CodeWizard
CodeWizard

Reputation: 142164

You can always simply fetch the current branch that you are working on and only this branch.

I'd much rather have git, by default, not pull down these branches

# pull only the current branch you are working on
# git pull = git fetch && git merge
git pull origin <branch name>

Now when you need to merge your branch you can always use this syntax to pull/merge it:

# grab the remote branch
git pull origin Dev/<branch name>

Another way is to "play" with your refspec but im not recommending doing so even due that its pretty simple.

Here is a sample form the documentation:

... However, you can use namespaces (or directories) to accomplish something like that.

If you have a QA team that pushes a series of branches,
and you want to get the master branch and any of the QA team’s branches but nothing else, you can use a config section like this:

[remote "origin"]
    url = https://github.com/schacon/simplegit-progit
    fetch = +refs/heads/master:refs/remotes/origin/master
    fetch = +refs/heads/qa/*:refs/remotes/origin/qa/*

Upvotes: 2

Related Questions