3

I am cleaning up my repo, I find there are many branches created 2 years ago and left without creating any pull request to merge to master. Those branches are safe to delete. but I need to find those branches manually, is there any way I can get a list of those branches using the command line?

  • 2
    As far as I know, it's not possible with git (because a pull request is a GitHub concept, and git doesn't know about them). With that being said, I believe GitHub exposes this information in [their API](https://developer.github.com/v3/), and you can probably use that from the command line (with curl or similar) to get the information you need. – mkasberg Apr 09 '19 at 19:15
  • 1
    Would this help? https://stackoverflow.com/questions/12276001/find-unmerged-git-branches – Eskapp Apr 09 '19 at 21:02

1 Answers1

1

As @Eskapp mentioned, you can list the merges branches and then delete them:

git branch -d branch1 branch2 branch3 ... # For local branches 
git push origin :branch1 :branch2 :branch3 ... # for the remote ones, note the ":" before the branch names used to delete them, cf "man git-push"

Then, the rest will have to be done manually with a mix of commands. I would do the following:

  • git log --oneline | grep branchname to look for merge commits (messages similar to "Merge branchName into master", in case some branches have been rebased. Make sure all the commits are findable in master (based on their messages)
  • Check the remaining pull requests and cross reference with the list of branches to see which ones are linked to pull requests (to be done manually, or using github api)
  • Also, if you use a ticketing system, and if your branches have ticket number in their name, look at the tickets, if the tickets are closed, maybe the branches will be fine to be deleted.

In overall it can be a lot of manual work, depending on which ones come out from the first check, but it will be worth it. And it will allow you to carry on on a clean state for the future, to delete the branches as they get merged.

padawin
  • 4,230
  • 15
  • 19