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?
The git prune command is an internal housekeeping utility that cleans up unreachable or "orphaned" Git objects. Unreachable objects are those that are inaccessible by any refs. Any commit that cannot be accessed through a branch or tag is considered unreachable. git prune is generally not executed directly.
In summary when enabling the prune on every fetch, this means your local list of branches is always up-to-date with the remote. Pruning will cleanup and remove your local tracking branches that no longer exist on the server.
git fetch is the command that tells your local git to retrieve the latest meta-data info from the original (yet doesn't do any file transferring. It's more like just checking to see if there are any changes available). git pull on the other hand does that AND brings (copy) those changes from the remote repository.
Git checkout remote branch is a way for a programmer to access the work of a colleague or collaborator for the purpose of review and collaboration. There is no actual command called “git checkout remote branch.” It's just a way of referring to the action of checking out a remote branch.
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.
git remote prune
and git fetch --prune
do the same thing: deleting the refs to the branches that don't exist on the remote, as you said. The second command connects to the remote and fetches its current branches before pruning.
However it doesn't touch the local branches you have checked out, that you can simply delete with
git branch -d random_branch_I_want_deleted
Replace -d
by -D
if the branch is not merged elsewhere
git prune
does something different, it purges unreachable objects, those commits that aren't reachable in any branch or tag, and thus not needed anymore.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With