Deepika Upadhyay
Deepika Upadhyay

Reputation: 73

How to delete remote branches that I created from a shared repo?

Purpose: want to clean all my stale branches from a shared remote which is used by a community as a trigger for Jenkins build.

A bit more context: I want to pull the branch created by me in a repository where all community members push their branches, as a cleanup I want to remove all my old pushed branches from the remote. Breakup of problem:

  1. fetch all from the shared remote repo.
  2. identify which are the branches created by me(didn't quite understood how to achieve this)
  3. do git push --delete for those branches

Upvotes: 0

Views: 835

Answers (3)

Deepika Upadhyay
Deepika Upadhyay

Reputation: 73

as mentioned by some answers here, we cannot know the author. But using the above command we can grep author for latest commit, verify that the branches are mine manually once by just doing:

git for-each-ref --format='%(color:cyan)%(authordate:format:%m/%d/%Y %I:%M %p)    %(align:25,left)%(color:yellow)%(authorname)%(end) %(color:reset)%(refname:strip=3)' --sort=authordate refs/remotes | grep "Firstname LastName"

and then delete those branches using:

 git push ci --delete $(git for-each-ref --format='%(color:cyan)%(authordate:format:%m/%d/%Y %I:%M %p)    %(align:25,left)%(color:yellow)%(authorname)%(end) %(color:reset)%(refname:strip=3)' --sort=authordate refs/remotes | grep Deepika | awk '{print $6}')

Upvotes: 0

Marek R
Marek R

Reputation: 37927

To ensure that your branch wasn't deleted by github/bitbucket/... (when pull request is merged this tool can delete branch on remote) do:

git remote prune origin

It will remove local references to remote branches which has been purged on remote.

If your branch is still there, then delete it manually:

git push --delete origin YourBranchName

Upvotes: 0

mimikrija
mimikrija

Reputation: 113

As Asteroids With Wings mentioned in the comments, there is no concept of "branch authored by you".

But in case there is a relatively strict naming convention in your community where you include username in the name of the branch, for example, all branches are named something like this: team/teamname/username/feature1234, then you can list all the remote branches which contain a specific string like this:

git branch -r --list *username*

This will list all the remote branches containing string username in their name. After inspecting that list, you would run git push -d origin <branch name(s)> which deletes the branch(es) you selected on the remote. Of course, you can write a bash script to do that for you.

Upvotes: 1

Related Questions