1

I have a git repo where I pushed my latest changes to a remote. In the last days I deleted several branches locally but couldn't remove them on the remote, since I didn't have any connection to the git server.

Is there a way to delete the remote branches, which don't exist anymore on my local repo? We talk about 200 branches, so a manual approach is not desired.

I found several answers to delete a local branch, that does not exist on the remote anymore via git fetch --prune, in my case I am looking for the other way around.

Any help is highly appreciated

Daniel Stephens
  • 2,371
  • 8
  • 34
  • 86
  • 1
    Does this answer your question? [How do I delete a Git branch locally and remotely?](https://stackoverflow.com/questions/2003505/how-do-i-delete-a-git-branch-locally-and-remotely) – phd Mar 22 '20 at 09:44
  • https://stackoverflow.com/search?q=%5Bgit%5D+prune+remote+branches+ – phd Mar 22 '20 at 09:44
  • 1
    https://stackoverflow.com/a/13437928/7976758: [`git push --prune origin`](https://git-scm.com/docs/git-push#Documentation/git-push.txt---prune) – phd Mar 22 '20 at 09:44
  • 1
    Upvote the linked answer, that would be enough. – phd Mar 22 '20 at 18:01
  • @phd but it doesn't answer my question. I was looking for the --prune option – Daniel Stephens Mar 22 '20 at 22:57
  • 1
    [This one](https://stackoverflow.com/a/13437928/7976758) starts with it. – phd Mar 23 '20 at 07:58

2 Answers2

1

If you want to remove multiple branches one single command you can try this,

git push origin --delete <branch1> <branch2> <branch3>

If you want the list of the remote branch you can try this command first. Then just copy-paste the list into the above command.

git branch -r
Anurodh Singh
  • 814
  • 5
  • 9
0

Current Answer

I didn't read the comments. the best is the answer at stackoverflow.com/a/13437928/7976758:

$ git push --prune origin

Previous Answer:

Stumbled onto this question, here's how I did it, it's not exactly automated but this should be a start.

Step 1: Delete local branches

  1. Get list of local branches
$ git --no-pager branch --sort=-committerdate   > branches_to_delete.txt 
  1. Delete the rows not to be deleted in file branches_to_delete.txt

  2. delete the branches at local

$ cat branches_to_delete.txt | xargs git branch -D

Step 2: Delete remote branches

  1. Get list of remote branches
$ git remote prune origin # delete all the local refs to already-deleted remote branches
$ git --no-pager branch -r --sort=-committerdate  > branches_to_delete.txt
  1. delete branches not to be deleted in file branches_to_delete.txt

  2. replace all "origin/" with "" (vim, emacs, vscode, sed, etc.) in file

  3. delete the branches at remote:

$ cat branches_to_delete.txt | xargs git push origin --delete
Viet Than
  • 164
  • 1
  • 10