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:
RemoteA/Branch1
, RemoteB/Branch1
, RemoteB/Branch2
RemoteA/_Branch2
, RemoteB/_Branch3
How can I do this?
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.
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.
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.
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.
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/_*'
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 --all
s or git fetch fred
s 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.
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:
git ls-remote origin
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With