867

Using git branch --all shows all remote and local branches. When does Git refresh this list?

On pull/push? And how do I refresh it using Git Bash?

3
  • 6
    As a note, git ls-remote might be interesting here.
    – jthill
    Apr 1, 2016 at 16:46
  • 2
    Don't forget to use git fetch before git branch --all
    – Lukas
    Sep 13, 2021 at 13:21
  • 6
    git pull --all --prune does the trick for me
    – Pykler
    Feb 1, 2022 at 19:20

5 Answers 5

2033

To update the local list of remote branches:

git remote update origin --prune

To show all local and remote branches that (local) Git knows about:

git branch -a
11
  • 195
    git remote update origin --prune is what I was looking for thx
    – WonderLand
    Mar 29, 2017 at 10:22
  • 7
    On the difference between this answer, which uses git remote update, and the accepted answer, which uses git fetch, see here Apr 22, 2017 at 17:53
  • 11
    this is the correct answer. git fetch did not remove my local cache of remote branches. Only --prune was able to clean it all up. May 17, 2017 at 3:21
  • 37
    You can have git automatically do this with git config remote.origin.prune true
    – Slate
    Sep 25, 2017 at 9:07
  • 14
    git remote prune origin has the same effect and you type less.
    – Dvin
    Jan 30, 2018 at 17:02
77

The OP did not ask for cleanup for all remotes, rather for all branches of default remote.

So git fetch --prune is what should be used.

Setting git config remote.origin.prune true makes --prune automatic. In that case just git fetch will also prune stale remote branches from the local copy. See also Automatic prune with Git fetch or pull.

Note that this does not clean local branches that are no longer tracking a remote branch. See How to prune local tracking branches that do not exist on remote anymore for that.

1
  • 7
    To make it a little shorter git fetch -p Sep 5, 2019 at 18:03
36

I believe that if you run git branch --all from Bash that the list of remote and local branches you see will reflect what your local Git "knows" about at the time you run the command. Because your Git is always up to date with regard to the local branches in your system, the list of local branches will always be accurate.

However, for remote branches this need not be the case. Your local Git only knows about remote branches which it has seen in the last fetch (or pull). So it is possible that you might run git branch --all and not see a new remote branch which appeared after the last time you fetched or pulled.

To ensure that your local and remote branch list be up to date you can do a git fetch before running git branch --all.

For further information, the "remote" branches which appear when you run git branch --all are not really remote at all; they are actually local. For example, suppose there be a branch on the remote called feature which you have pulled at least once into your local Git. You will see origin/feature listed as a branch when you run git branch --all. But this branch is actually a local Git branch. When you do git fetch origin, this tracking branch gets updated with any new changes from the remote. This is why your local state can get stale, because there may be new remote branches, or your tracking branches can become stale.

4
  • 4
    Yep, git fetch did it.
    – BendEg
    Apr 1, 2016 at 14:13
  • 7
    git fetch doesn't work for me, need add --prune. Btw, I'm not downvoter ;)
    – Deqing
    Jun 3, 2019 at 8:06
  • 5
    git fetch doesn't remove deleted remote branches. That's probably why some people downvoted Oct 7, 2019 at 8:39
  • tl'dr not true: git remote update origin --prune
    – Macilias
    Apr 8, 2022 at 19:41
7

Use git fetch to fetch all latest created branches.

2
  • 1
    git fetch did not change anything for me with git 2.17.1. Which version and options have you been using? Jul 19, 2018 at 8:29
  • 5
    You need either --prune or git config remote.NAME.prune true for this to solve OP question
    – Oliver
    Jan 22, 2019 at 15:25
1

I use

git fetch --all --prune --tags --prune-tags --progress

and add this to my run commands file (.zshrc or .bashrc) so I can quickly type gitf to trigger this command:

alias gitf='git fetch --all --prune --tags --prune-tags --progress'

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.