0

I usually fetch from a remote repo that I share with my team members. Then I merge their branches into my main branch and create new local branch to do development. Everything is going well EXCEPT that the list that I see in git branch -va keeps getting bigger and bigger as I fetch more from the remote repo. and merge (please see the screenshot below) these into my local repo.

What I see when I type git branch -va in my repo.

enter image description here

I'm wondering if there's a git command that will allow me to remove these 'remotes/origin/Brazil', 'remotes/origin/Chile', etc. from appearing in the git branch -va command.

I tried using the following approaches:

  1. git branch -d remotes/origin/Brazil (error: branch 'remotes/origin/Brazil' not found) (StackOverflow suggestion link)
  2. git fetch --prune (nothing happens) (StackOverflow suggestion link)
  3. git remote prune origin (nothing happens) (StackOverflow suggestion link)

I some of these branches to go away when using git push --all --prune as suggested here. But some comment related to that StackOverflow answer say this command is dangerous.

What is the safest and correct way to remove these old 'remotes/origin/*' from appearing in the result of git branch -va? Thanks in advance for your answers!

user1330974
  • 2,500
  • 5
  • 32
  • 60

1 Answers1

1

Your question is a little unclear, because you're asking how you can avoid seeing branches in your output, but the solutions you're talking about have to do with deleting branches - which is a very different proposition.

If nobody needs the branches anymore, they can be deleted

git push --delete origin <branch-name>

After that is done, other clones can update to reflect that the branch has been deleted using git fetch --prune; but as you note, this only affects deleted branches, which is why it doesn't appear to do anything when you use it.

If the remote branches are still needed and you just don't want to see them, that's likely a bigger problem.

Just to make sure we don't miss the obvious: If you're asking how to not see any remote branches, you'd just not include the -a option (i.e. git branch -v). Since you're including -a, I assume you want to see remote branches, but only some of them.

And the problem with that is, there's not a lot of info git can use to know which you want to see. Having some kind of agreement with the others using the repo as to when branches can be deleted from the remote may be the best thing you can do.

Mark Adelsberger
  • 42,148
  • 4
  • 35
  • 52
  • Thank you for the suggestions! I should have been clearer with my question (will fix that). My git workflow these days is to 1) `git fetch upstream` (where 'upstream' is a repository, say 'https://github.com/foobar/project.git') 2) `git branch -va` to see which remote branch or commit number that I'd like to merge to my main branch (say, it's 'remotes/foobar/south_africa') 3) `git merge remotes/foobar/south_africa` 4) `git rebase ` to start working off of that remote branch's work in my local repo. (comment continued below) – user1330974 Nov 22 '20 at 10:43
  • Over time, the output of `git branch -va` gets longer and I'd like to trim it because usually, what I want to see from `git branch -va` is the commit message and the hash ID to make sure I'm choosing the right one to `merge` into my `main` branch. Since I do not own the remote branch, I have no way to do `git push --delete origin ` (insufficient permission). The remote repo owner (in our case, `foobar` owner) usually review my local changes via a Github pull request, which I make. Then he decides to merge my pull request to the `foobar` repo and tells me to `fetch` it. – user1330974 Nov 22 '20 at 10:47
  • Then, I'd resume my git workflow of `fetch`-ing the upstream (I set upstream in my local repo to `https://github.com/foobar/project.git') and repeat the process. Hope this makes sense and please let me know what I need to do in order to trim the list that I see in the `git branch -va` (if there's a way). Thank you very much in advance for your help! – user1330974 Nov 22 '20 at 10:48