Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use git push prune to delete remote branches

Tags:

git

I am wondering if git push --prune can clean up remote branches just as git remote prune origin does in the opposite direction. Here is what the documentation says it does:

--prune
    Remove remote branches that don’t have a local counterpart. For example a remote branch tmp will be removed if a local branch with the same name doesn’t exist any more.

But I am confused as to what is meant by counterpart in the above. Let me explain what I am trying to do by an example.

Lets create a remote-tracking branch like follows:

$ git checkout -b my-feature-branch && git push -u origin my-feature-branch
...
Branch 'my-feature-branch' set up to track remote branch 'my-feature-branch' from 'origin'.

So my interpretation is that my-feature-branch is the local counterpart that the documentation refers to.

Now delete this local counterpart as follows:

$ git branch -D my-feature-branch
Deleted branch my-feature-branch (was f7404b0).

I am now expecting git push --prune to delete the remote branch if I run it as follows:

$ git push --prune origin
Everything up-to-date

However, as you can see, even though the local counterpart is gone, git push --prune refuses to delete the remote branch. While I know I can use git push -d origin my-feature-branch to delete the remote branch, I am trying to understand if git push --prune can do the same.

like image 222
user2199684 Avatar asked Oct 16 '25 03:10

user2199684


1 Answers

Git needs to know what to compare. Specify where to find refs.

git push --prune origin refs/heads/*:refs/heads/*

Or

git push --prune origin refs/heads/*

Another method is to push an empty ref

git push origin :my-feature-branch
like image 87
EncryptedWatermelon Avatar answered Oct 18 '25 20:10

EncryptedWatermelon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!