Reputation: 73
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:
Upvotes: 0
Views: 835
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
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
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