2

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

6

2 Answers 2

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 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the response. Since we talk about 200 branches, I was looking for an automated way.
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 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.