-2

I am just looking for a git command to bulk delete my unused remote branches.

My requirement is like , i want to delete a merged or unused branches for a particular user for past months.Lets say, A is an user created 10 branches on november and also he created 10 branches on december. Now he wants to delete november branches.

I just tried with git for-each-ref for that :

git for-each-ref --format='%(committerdate) %09 %(authorname) %09 % \ 
(refname)%09 %(committerdate:relative)' --sort=committerdate  |grep "Rk"

Any solutions would be appreciable

RathanaKumar
  • 115
  • 11
  • Can you describe more specifically "unused" in your context? What specific criteria should protect a branch from deletion here? *(Your command is (broken, for one thing, but also) purely output, it won't do a thing to your branches, by the way.)* – RomainValeri Dec 06 '19 at 08:20
  • Is this duplicate of https://stackoverflow.com/questions/6127328/how-can-i-delete-all-git-branches-which-have-been-merged – Vishwanath Dec 06 '19 at 08:25
  • Does this answer your question? [How can I delete all Git branches which have been merged?](https://stackoverflow.com/questions/6127328/how-can-i-delete-all-git-branches-which-have-been-merged) – Vishwanath Dec 06 '19 at 08:25
  • @RomainValeri I want to delete an unused branches which are not used for 1month from current date.For that i am expecting single git command. – RathanaKumar Dec 06 '19 at 09:51
  • @RathanaKumar "branches which are not used" is a useless description until we know what "used" means for you. Checking the branch out? Commiting on the branch? Pushing commits on remote version of the branch? Also, are you ok with deleting branches with unmerged commits on it provided they're "old" enough? – RomainValeri Dec 06 '19 at 10:05
  • @RomainValeri Assume,to develop a feature, am creating a branch to do some commits and merge, once the feature get closed ,the branch become unused. In that way, am creating a new branch for every new feature.Now the old branches will become unused right. Because of that am asking a single git command to clear out all the branches, instead of finding each branch and delete. – RathanaKumar Dec 07 '19 at 15:10
  • 1
    @RathanaKumar So the term you were searching for was *merged branches*, it'll be clearer. – RomainValeri Dec 07 '19 at 15:15
  • @RomainValeri yes probably !! – RathanaKumar Dec 07 '19 at 15:21

1 Answers1

1

Assuming a master branch as your stable one, a command for bulk deletion of merged branches could look like

git checkout master
git branch -d $(git for-each-ref --author=<name> --since="november" --before="december" --format="%(refname:short)" refs/heads)

There, no unmerged branch (even with given date/author characteristics) will be deleted since you're asking to delete every branch, but with the -d flag which won't delete unmerged branches.

Optionnally, you can add --merged=master to the for-each-ref command, it'll be a little less verbose, avoiding to output the denied deletions.


Note

Before firing the deletion itself, test the for-each-ref part alone, to be sure it outputs what you wanted to be deleted.

RomainValeri
  • 14,254
  • 2
  • 22
  • 39
  • Thanks for the effort and solution. but i need to delete past month branches.is it possible to do that in this command. – RathanaKumar Dec 07 '19 at 15:31
  • The date is irrelevant here, **every** branch will be deleted, if its tip (last commit made on the branch) is merged into `master`. – RomainValeri Dec 07 '19 at 15:35
  • Fine.. But my requirement is like , i want to delete an merged or unused branches for a particular user for past months.Lets say, A is user having created 10 branches on november and also he created 10 branches on december. Now he wants to delete november branches .does this make sense?? – RathanaKumar Dec 07 '19 at 15:39
  • @RathanaKumar Where is this in your question above? It would be easier to have a clear and complete question stated for everyone, rather than having to read every comment on the page to understand what's the real need. – RomainValeri Dec 07 '19 at 18:27
  • Thanks for the assistance.Now i have edited and stated my question . – RathanaKumar Dec 07 '19 at 18:36