857

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
BendEg
  • 20,098
  • 17
  • 57
  • 131

6 Answers6

2008

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
Kirill Kobelev
  • 10,252
  • 6
  • 30
  • 51
centralcmd
  • 20,234
  • 2
  • 10
  • 7
  • 193
    `git remote update origin --prune` is what I was looking for thx – WonderLand Mar 29 '17 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](http://stackoverflow.com/a/1856504/1175496) – Nate Anderson Apr 22 '17 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. – Felipe Alvarez May 17 '17 at 03:21
  • 37
    You can have git automatically do this with `git config remote.origin.prune true` – Slate Sep 25 '17 at 09:07
  • 13
    `git remote prune origin` has the same effect and you type less. – Dvin Jan 30 '18 at 17:02
  • 4
    @Dvin the OP asked for a refresh of the list; `remote prune` only cleans up dead branches list, it does not get new branches added to remote – Oliver Jan 22 '19 at 15:04
  • 1
    Why does `git remote update origin --prune` have to be run manually? It should be clever enough to do this by itself if you have, for instance, checked out master from the server and there's no other branches (because they just got merged). – Syntax Error Feb 18 '19 at 11:02
  • was able to pull my upstream branches using `git remote update upstream --prune` – Uriahs Victor Mar 26 '19 at 18:23
  • I have to run this script to sync local with remote. `git fetch -p && for branch in `git branch -vv | grep ': gone]' | awk '{print $1}'`; do git branch -D $branch; done` – Lal Krishna May 17 '19 at 06:04
  • If using with tortoise git then, have an option for Git Sync -> Pull (as Remote Update) – dsi Dec 27 '19 at 18:26
  • Bless you @centralcmd. it's very rare indeed that you search for a problem and find in the first try the exact solution you needed. – RodrikTheReader Apr 06 '20 at 08:03
  • This works perfectly. Thanks @centralcmd – realsajeel289 Jul 28 '23 at 06:50
76

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Oliver
  • 27,510
  • 9
  • 72
  • 103
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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
7

Use git fetch to fetch all latest created branches.

Oliver
  • 27,510
  • 9
  • 72
  • 103
vvv
  • 373
  • 1
  • 3
  • 8
  • 1
    `git fetch` did not change anything for me with git 2.17.1. Which version and options have you been using? – PointedEars Jul 19 '18 at 08:29
  • 5
    You need either `--prune` or `git config remote.NAME.prune true` for this to solve OP question – Oliver Jan 22 '19 at 15:25
0

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'
Finn
  • 1,999
  • 2
  • 24
  • 29
-6

If you are using Eclipse,

  1. Open "Git Repositories"
  2. Find your Repository.
  3. Open up "Branches" then "Remote Tracking".

Git

They should all be in there. Right click and "checkout."

markthegrea
  • 3,731
  • 7
  • 55
  • 78
  • 1
    How does this answer the question? - *"When does Git refresh the list of remote branches?"* – Peter Mortensen Oct 24 '19 at 11:49
  • 2
    When I wanted to refresh my local branches in eclipse, I googled and it took me here. This post has actually helped a few people as it has had some upvotes. Maybe I misunderstood the question. To me, Eclipse IS Git (the interface to it). Sorry for offending everyone! – markthegrea Oct 25 '19 at 14:31