0

Is there a script to delete all the branches created by me?

I found questions on stackoverflow and on internet about listing the branches by author, like this

Is there a script to list git branches created by me?

But I could not find anything like delete them.

Is there any way?

I also found one this post for unused branch bulk delete but not by author

Is there a way to bulk delete git branches?

Pawan Nogariya
  • 8,330
  • 12
  • 52
  • 105
  • When you say "delete a branch", do you simply mean that you want to delete the ref, or are you also trying to remove commits? – William Pursell Apr 04 '22 at 13:14
  • 1
    Note : the first answer in the linked question is wrong. It does **not** list branches created by X, it lists all branches whose *last commit* is from author X, which is completely different. Metadata about who created a branch is not stored anywhere in git. – Romain Valeri Apr 04 '22 at 13:15
  • @WilliamPursell - To be honest I am not quite sure about the difference of the two. I basically wants to delete all my created branches because they are merged to the parent branch and are of no use now, so basically the action of the `--delete` command – Pawan Nogariya Apr 04 '22 at 13:17
  • [Related](https://stackoverflow.com/q/7726949/184546). – TTT Apr 04 '22 at 20:53

1 Answers1

1

Git has no idea who created some branch name, so no, this is not possible in general.

I basically wants to delete all my created branches because they are merged to the parent branch

Branch names do not store parent information either. You can, however, ask whether the tip commit found via some branch name is contained within the set of commits found via some other branch name, using git branch --merged for instance:

git fetch origin
git branch --merged origin/main

will list out any of your (local) repository branch names that identify a commit that is contained within the commit-set found by origin's main (as last updated by git fetch, hence the git fetch step first).

William Pursell asks, in a comment:

When you say "delete a branch", do you simply mean that you want to delete the ref, or are you also trying to remove commits?

Your answer is:

To be honest I am not quite sure about the difference of the two.

The difference here is the key, because Git is not really about branches, but rather about commits. If you are going to use Git at all, you must learn about commits. This will also make Git's branching model make some sense. Rather than repeat it all here, though, I'll just refer you to this answer.

torek
  • 448,244
  • 59
  • 642
  • 775