Productive and easy way to clear your local git branches
It's recommended for you who:
- Usually struggle in clearing your local git branches.
- Anyone that doesn't have the habit of deleting the branches constantly and gets crazy managing a lot of branches.
- Prefer using a terminal rather than a Git GUI Client.
I implemented this code once ago, as a tiny and ugly script only to help me to be more productive. And I realized that some colleagues were looking for something like this and I've published it to be available to anyone else to use and contribute.
Recommended node version equal to or greater than v16
You don't need to install it globally, if the idea is to use it sometimes, you can just execute it into your git project, so that you will execute the latest stable version.
$ npx clear-branches@latest
Or install it on your machine:
yarn:
$ yarn global add clear-branches
npm:
$ npm i -g clear-branches
output
Now clear-branches is executable anywhere you want, if you try into your project you will see the list of branches it will delete and a confirmation message:
$ clear-branches
feat/component
feat/feature
fix/functions
? Are you sure you want to delete listed branches above? › (y/N)
Considering a project with these branches:
feat/component
feat/feature
fix/functions
main
release
It force ignored branches by default to be considered to clear.
By default, clear-branches ignores these branches.
main
master
release
develop
*The current branch is ignored and is not possible to force delete it
Using clear-branches --force=<branch>[,<branch>],
you will be able not to ignore any of these branches.
I.e.:
$ clear-branches --force=main
feat/component
feat/feature
fix/functions
main # <-- now main is in the list to deleted
? Are you sure you want to delete listed branches above? › (y/N)
It ignores the branches you don't want to delete.
I.e.:
$ clear-branches --ignores=feat/component,feat/feature
fix/functions
? Are you sure you want to delete listed branches above? › (y/N)
In this example, feat/component
and feat/feature
won't be deleted.
It ignores all branches that match a regex.
I.e.:
$ clear-branches --ignores=feat\/\\w+
fix/functions
? Are you sure you want to delete listed branches above? › (y/N)
_As both feat/component
and feat/feature
match the feat\/\\w+
regex, they won't be deleted.
It's needed to escape if you need to use
\
, like\\w+
With this option, you can select the branch you want to delete.
I.e.:
$ clear-branches -i --force=release,main
? Choose the branches you want to delete: ›
Instructions:
↑/↓: Highlight option
←/→/[space]: Toggle selection
a: Toggle all
enter/return: Complete answer
◉ feat/component
◉ feat/feature
◯ fix/functions
◯ release
◯ main
After pressing Enter:
$ clear-branches -i --force=release,main
✔ Choose the branches you want to delete: › feat/component, feat/feature
? Are you sure you want to delete listed branches above? › (y/N)
To comfortable start contributing to this project, it is encouraged you go for this list of good start issues:
More labels