Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git fetch origin --prune doesn't delete local branches?

Tags:

git

git-fetch

At one point I thought that git fetch origin --prune deleted local branches that were no longer present on the server. Somehow this is not my experience at the moment.

I ran this command, and the local branch was not deleted. It is not currently checked out. I ran git branch -vv to check this info, and I see

feature/MyGreatFeature           f30efc7 [origin/feature/MyGreatFeature: gone] 

So it seems to know that it is gone. Why would it not delete my local branch?

Running git version 2.7.4 (Apple Git-66)

like image 718
yano Avatar asked Jun 06 '16 18:06

yano


People also ask

Does git prune delete local branches?

Does Git Remote Prune Origin Delete the Local Branch? No git remote prune origin will only delete the refs to remote branches that no longer exist. Git stores both local and remote refs.

How do I delete a local branch in origin?

Deleting a branch LOCALLY Delete a branch with git branch -d <branch> . The -d option will delete the branch only if it has already been pushed and merged with the remote branch. Use -D instead if you want to force the branch to be deleted, even if it hasn't been pushed or merged yet. The branch is now deleted locally.

How do I delete local branches no longer on my remote?

Remove All Local Branches not on Remote First we get all remote branches using the git branch -r command. Next, we get the local branches not on the remote using the egrep -v -f /dev/fd/0 <(git branch -vv | grep origin) command, Finally we delete the branches using the xargs git branch -d command.

Why can't I delete my git branch?

If you are sure you want to delete it, run ' git branch -D issue-5632 '. This error is caused because we have some un-merged changes in branch issue-5632 due to which the branch delete has failed. In such case either you can delete the branch forcefully or merge the changes and then perform the delete operation.


2 Answers

The various prune options (git remote update --prune, git remote prune, git fetch --prune) only delete remote-tracking branches.1

You'll need to manually delete local branches you no longer want, or change or remove their upstream setting if the remote-tracking branch no longer exists. Note that each local branch can record a remote and/or branch that do not now, or even never did, exist. In this case Git mostly acts as if those local branches have no upstream set, except that since version 1.8.5, several commands report the upstream as "gone" or otherwise invalid, and may suggest using --unset-upstream.


1More precisely, they delete destination refs after doing the refspec mapping from the command line or fetch lines from the configuration. Hence, for fetch mirrors, they can delete local branches. Most clones are not set up as fetch mirrors, though.

There were some recent bug fixes for complex mappings, to make sure that Git did not prune a mapped branch in some cases when it should not. For any normal repository—ordinary clone or pure fetch mirror—these fixes have no effect; they matter only if you have complicated fetch configurations.

like image 169
torek Avatar answered Sep 22 '22 12:09

torek


The following command chain can be used to delete local branches:

git branch --v | grep "\[gone\]" | awk '{print $1}' | xargs git branch -D 
  • git branch --v lists the local branches verbosely
  • grep "\[gone\]" finds all the branches whose remote branch is gone
  • awk '{print $1}' outputs only the name of the matching local branches
  • xargs git branch -D deletes all the matching local branches

This should work on MacOS as well as *nix environments.

like image 37
Brent Worden Avatar answered Sep 21 '22 12:09

Brent Worden