0

I work with some rather messy developers, and our larger repos have close to 100 branches, most of which are abandoned. They usually don't cause issues but once in a while they get in the way. I work in my own fork, so I could locally set one-branch tracking for origin, but I'm afraid that could bite me in the butt eventually.

An in-between solution would be to only fetch branches with recent activity, e.g. newest commit is within the last 90 days or something. Is there any way to do that? (Git refspec doesn't seem to mention this, so maybe not.)

One thing I've poked around with was to git branch -rd old branches after fetching, but when it comes to scripting that gets complicated quickly (e.g. by adapting this to remote-tracking branches: Delete all branches that are more than X days/weeks old).

  • Your team needs a better maintenance approach to your repo. How are you handling merge requests? I've usually had a branch per feature/ticket. The remote branch should be deleted once the ticket is complete and the code is merged. The user can keep their branch locally, but I think it should be deleted. Always start a new ticket with a new, dedicated feature branch. – Adrian J. Moreno Aug 23 '23 at 16:42
  • Is it possible to prune those abandoned branches from the remote? That sounds like the better way forward. – knittl Aug 23 '23 at 19:53

1 Answers1

0

Useful Commands If You Are Interested in Deleting Branches One at a Time

I don't think automatically deleting branches is such a good idea, because you could lose pointers to useful blobs. I would recommend deleting branches manually whenever you don't need them anymore.

  • git branch -D <branch> deletes your local branch.
  • git push origin -d <branch> deletes a remote branch for everyone.
  • If you are not the person who deleted a branch, you should run git remote prune origin to prevent deleted branches from being displayed in all remote and local branches (displays when you run git branch -a).
Caleb
  • 205
  • 1
  • 8