My situation is this... someone working on the same repo has deleted a branch from his local & remote repo...
Most people who have asked about this kind of problem on Stack Overflow, or other sites have the issue of branches still showing in their remote tracking branch list git branch -a
at the bottom:
* master
develop
feature_blah
remotes/origin/master
remotes/origin/develop
remotes/origin/feature_blah
remotes/origin/random_branch_I_want_deleted
However, in MY situation the branch that shouldn't be there, is local:
* master
develop
feature_blah
random_branch_I_want_deleted
remotes/origin/master
remotes/origin/develop
remotes/origin/feature_blah
When I do any of the following, it doesn't get removed locally:
$ git prune
I also tried:
$ git remote prune origin
$ git fetch --prune
More useful info: When I check git remote show origin
this is how it looks:
* remote origin
Fetch URL: utilities:homeconnections_ui.git
Push URL: utilities:homeconnections_ui.git
HEAD branch: master
Remote branches:
master tracked
develop tracked
feature_blah tracked
other123 tracked
other444 tracked
other999 tracked
Local branches configured for 'git pull':
develop merges with remote develop
feature_blah merges with remote other999
master merges with remote master
random_branch_I_want_deleted merges with remote random_branch_I_want_deleted
Local refs configured for 'git push':
develop pushes to develop (local out of date)
master pushes to master (up to date)
feature_blah pushes to feature_blah(up to date)
Notice that it's only in the section titled Local branches configured for 'git pull':
Why?
I don't blame you for getting frustrated about this. The best way to look at is this. There are potentially three versions of every remote branch:
refs/heads/master
)refs/remotes/...
)refs/remotes/origin/master
)refs/heads/master
)Let's start with git prune
. This removes objects that are no longer being referenced, it does not remove references. In your case, you have a local branch. That means there's a ref named random_branch_I_want_deleted
that refers to some objects that represent the history of that branch. So, by definition, git prune
will not remove random_branch_I_want_deleted
. Really, git prune
is a way to delete data that has accumulated in Git but is not being referenced by anything. In general, it doesn't affect your view of any branches.
git remote prune origin
and git fetch --prune
both operate on references under refs/remotes/...
(I'll refer to these as remote references). It doesn't affect local branches. The git remote
version is useful if you only want to remove remote references under a particular remote. Otherwise, the two do exactly the same thing. So, in short, git remote prune
and git fetch --prune
operate on number 2 above. For example, if you deleted a branch using the git web GUI and don't want it to show up in your local branch list anymore (git branch -r
), then this is the command you should use.
To remove a local branch, you should use git branch -d
(or -D
if it's not merged anywhere). FWIW, there is no git command to automatically remove the local tracking branches if a remote branch disappears.