Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can git permanently ignore a remote branch?

Tags:

git

branch

I'm using the Pages feature of GitHub. This works by putting the published HTML in a branch called gh-pages. I have two separate working directories, one for the project itself and one for the HTML docs.

In the former, I want to completely ignore the gh-pages branch, as it's an unrelated line of work and I don't want it to clutter up my various commit visualizations.

That is, what I have is:

$ git remote show origin
* remote origin
  Fetch URL: git@github.com:reidpr/quac.git
  Push  URL: git@github.com:reidpr/quac.git
  HEAD branch: master
  Remote branches:
    bar              tracked
    foo              tracked
    gh-pages         tracked
    master           tracked
  Local branches configured for 'git pull':
    master    merges with remote master
  Local refs configured for 'git push':
    master    pushes to master    (up to date)

and what I want is something like:

$ git remote show origin
  [...]
  Remote branches:
    bar              tracked
    foo              tracked
    gh-pages         ignored
    master           tracked
  [...]

Note there are several branches that I do want to track, and just one that I don't. I want to specify the latter, not the former.

I can delete the local references to origin/gh-pages, but then it comes back next time I git fetch.

like image 447
Reid Avatar asked May 30 '13 17:05

Reid


People also ask

How do I exclude a remote branch?

Delete a branch with git branch -d <branch> . The -d option will delete the branch only if it has already been pushed and merged with the remote branch. Use -D instead if you want to force the branch to be deleted, even if it hasn't been pushed or merged yet. The branch is now deleted locally.

Does git prune remove remote branches?

git fetch --prune is the best utility for cleaning outdated branches. It will connect to a shared remote repository remote and fetch all remote branch refs. It will then delete remote refs that are no longer in use on the remote repository.

Does git pull pull all remote branches?

git fetch --all and git pull -all will only track the remote branches and track local branches that track remote branches respectively. Run this command only if there are remote branches on the server which are untracked by your local branches. Thus, you can fetch all git branches.

Does git pull affect remote branch?

The short answer is simple: no, the remote-tracking branch remains unaffected.


3 Answers

Since git 2.29, released in october 2020, you can leverage negative refspec to exclude a specific branch to be fetched.

For GitHub Pages, you can do it like so:

git config --add remote.origin.fetch '^refs/heads/gh-pages'

And Dependabot:

git config --add remote.origin.fetch '^refs/heads/dependabot/*'

You can read more about it on https://github.blog/2020-10-19-git-2-29-released/#user-content-negative-refspecs

like image 128
PowerKiKi Avatar answered Oct 08 '22 13:10

PowerKiKi


You can modify the .gitconfig, so it tells git to fetch only what you just want:

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

Also, you can create an alias for the fetch which fetches what you want:

 git fetch origin +refs/heads/mybranch:refs/remotes/origin/mybranch

The creation of an alias is as simple as adding the new alias in .gitconfig:

 [alias]
   myfetch= git fetch origin +refs/heads/mybranch:refs/remotes/origin/mybranch

UPDATE:

You can specify more branches of course:

 git fetch origin +refs/heads/master:refs/remotes/origin/master +refs/heads/develop:refs/remotes/origin/develop
like image 35
Ondrej Peterka Avatar answered Oct 08 '22 13:10

Ondrej Peterka


I just hit the same problem. I was interested in one branch from 'bob', but he had many branches cluttering up my git branch -a output.

I did this:

rm .git/refs/remotes/bob/{next,master,maint}

and the branches were gone. A git fetch might restore them though, but I don't intend to fetch from bob regularly.

like image 3
steveire Avatar answered Oct 08 '22 12:10

steveire