Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ignore some branches/tags being copied to local git when do git fetch --all

I have many remotes added to my git. Each remote is a repository for one developer. Every day I fetch --all to see any new branches they create that are ready to review.

However, developers can push "private" branches to the remote. Say, all branches named with a underscore prefix are not ready to review, while other branches are ready to review.

When doing the git fetch --all, my git graph (by /libexec/git-core/git-gui) will see all branches no matter whether they have the underscore prefix or not. It complicates the graph.

I want git fetch to ignore those _XXXX branches from being downloaded to my local git. So when I view the git graph it's like:

  • Shows branches: RemoteA/Branch1 , RemoteB/Branch1, RemoteB/Branch2
  • Ignores branches: RemoteA/_Branch2, RemoteB/_Branch3

How can I do this?

like image 859
palazzo train Avatar asked Oct 29 '14 06:10

palazzo train


People also ask

Does git fetch all fetch tags?

git fetch fetches all branch heads (or all specified by the remote. fetch config option), all commits necessary for them, and all tags which are reachable from these branches. In most cases, all tags are reachable in this way. git fetch --tags fetches all tags, all commits necessary for them.

Does git fetch get all branches?

Git fetch commands and optionsFetch all of the branches from the repository. This also downloads all of the required commits and files from the other repository. Same as the above command, but only fetch the specified branch. The --dry-run option will perform a demo run of the command.

Does git fetch update local branches?

git fetch. On its own, git fetch updates all the remote tracking branches in local repository. No changes are actually reflected on any of the local working branches.

Does git clone fetch tags?

When you clone a repository, all the tags associated with the repository will be pulled down. To fetch all the remote tags, use the fetch command as shown below. You can list down all the tags from the git repository using the following command. You can also search for tags with patterns.


2 Answers

Update for Git v2.29.0

With negative refspecs,1 added in Git v2.29, you can achieve your original intent without any changes to your coworkers’ habits.

For each coworker’s remote, add a negative refspec (i.e. prefixed with ^) to exclude the _-prefixed branches. This is loosely adapted from the unit test for .git/config and the one for negative patterns. See the last line in the code block below for "Fred’s" remote.

[remote "fred"]
    url = something.git
    fetch = +refs/heads/*:refs/remotes/fred/*
    fetch = ^refs/heads/_*

You have two options to set this for fred on the command line:

  • git config --add remote.fred.fetch '^refs/heads/_*'
  • git remote set-branches --add fred '^refs/heads/_*'

Original answer

Expanding on VonC's excellent answer with a special "review" folder,2 you can modify your .git/config’s entries for your coworkers’ remotes. Here is the relevant documentation.

Where it was once

[remote "fred"]
    url = something.git
    fetch = +refs/heads/*:refs/remotes/fred/*

You can change it to

[remote "fred"]
    url = something.git
    fetch = +refs/heads/review/*:refs/remotes/fred/review/*
    fetch = +refs/heads/other_pattern/*:refs/remotes/fred/other_pattern/*
    fetch = +refs/heads/specific_branch:refs/remotes/fred/specific_branch

After doing so, you may have to clean up the references to refs you have already fetched, but future git fetch --alls or git fetch freds will not update anything outside of the patterns you specified.


1 As of 2022-02-23, there is still no documentation on negative refspecs, so the best place to look is in Git's history or in VonC's answer to a different question, where he pulls the relevant parts together.
2 Or as many special or interesting folders and branches as you need.

like image 138
Michael Avatar answered Oct 21 '22 10:10

Michael


Instead of using a '_' naming convention, you could use namespaces, pushing the branch in origin/review/Branch1 (git push Branch1:review/Branch1)
(named "group" in that answer, or "hierarchical branch name (branch names with slash) in that answer)

That way, you only have to fetch what is in the "review' namespace:

git fetch +refs/heads/review/*:refs/remotes/origin/review/*

The only other option would be a script, which would:

  • list the remote branches (without fetching anything): git ls-remote origin
  • for each branch with the right name, fetch only that branch
like image 36
VonC Avatar answered Oct 21 '22 11:10

VonC