DEV Community

Alexis Reigel
Alexis Reigel

Posted on • Edited on • Originally published at koffeinfrei.org

Git cleanup-branches

Additionally to the existing git squash and git fixup I added another piece in my custom git toolbox fitting in with the git fixup workflow:

git cleanup-branches

This one is especially useful if you work in a team and work with pull / merge requests, and need to once in a while clean up the branches in your local repository.

The script consists of 3 steps:

  1. Prune remote branches This means that all branches which don't exist in the remote repository anymore are also deleted locally
  2. Remove merged branches Branches that have been merged to the default branch (i.e. main or master) are deleted. This is useful if you previously merged a PR / MR.
  3. Remove branches from other authors This step removes branches that don't have any commits by you. This usually happens when you checked out a branch by someone else, possibly to review their work.

A sample session could look like this:

 ~/src/example β‘‚ main ? = ➜ git cleanup-branches
No local changes to save
Already on 'main'
Your branch is up to date with 'origin/main'.
πŸ₯ Pruning remote branches...
Fetching origin
From github.com:koffeinfrei/example
 - [deleted]            (none)    -> origin/fix/weird-stuff
 - [deleted]            (none)    -> origin/feat/nice-stuff

πŸ₯ Removing merged branches...
Deleted branch feature/feat/nice-stuff (was fee237764).

πŸ₯ Removing branches from other authors...
You don't seem to have any commits in the branch β‘‚ feature/docs/add-deployment-section

 * 91e8d233a 2022-07-05 Document deployment (feature/docs/prompt-for-sw-updates) [Gob Bluth]

Delete the branch 'feature/docs/prompt-for-sw-updates'? (y/n)? y
Deleted branch feature/docs/prompt-for-sw-updates (was 91e8d233a).

Already on 'main'
Your branch is up to date with 'origin/main'.
Enter fullscreen mode Exit fullscreen mode

The source code is available on GitHub.

Top comments (1)

Collapse
 
sloan profile image
Sloan the DEV Moderator

Hi there, we encourage authors to share their entire posts here on DEV, rather than mostly pointing to an external link.

Sharing your full posts helps ensure that readers don’t have to jump around to too many different pages, and it helps focus the conversation right here in the comments section on DEV.

To be clear, the DEV Terms state:

Posts must contain substantial content β€” they may not merely reference an external link that contains the full post.

Also, if you share your full post, you have the option to add a canonical URL directly to your post. This helps with SEO if you are reposting articles!