Clean up my git (Snack Pack #5)

blaketweeted

Blake Campbell

Posted on May 4, 2021

Clean up my git (Snack Pack #5)

Git Repo maintenance

If you've been working on any repo for an extended amount of time, you may have saved a million branches that have already been merged. It is time to clean all those old ones out.


The commands we'll be using.

# Removes references from branches that are no longer on the origin
git remote prune origin
Enter fullscreen mode Exit fullscreen mode

Note this next part will depend on your main branch's name. If you haven't yet, then you should rename master to main. See below for how to.

# Lists all the branches that have been merged into main and remove them
git branch --merged main | grep -v '^[ *]*main$' | xargs git branch -d
Enter fullscreen mode Exit fullscreen mode

Wrap it all up.

The last step is relatively easy. Combine it into your .zshrc or .bashrc. I'm sure there's a Windows and Linux equivalent, but I'm using macOS.

alias cleanUpMyGit="git remote prune origin ; git branch --merged master | grep -v '^[ *]*master$' | xargs git branch -d"
Enter fullscreen mode Exit fullscreen mode

Go ahead and restart your terminal, then try it out in a repo directory.

$ cleanUpMyGit
Enter fullscreen mode Exit fullscreen mode

Afterward, you should see a list of the branches removed by the command. Happy Coding!


References:

My snack pack reads are intended for a quick read without any fluff and provide actionable items.

πŸ’– πŸ’ͺ πŸ™… 🚩
blaketweeted
Blake Campbell

Posted on May 4, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Card Fight: A Python Terminal Game
python Card Fight: A Python Terminal Game

October 31, 2024

Complete Git Cheat sheet
webdev Complete Git Cheat sheet

September 25, 2024

NextJS + Opentelemetry
nextjs NextJS + Opentelemetry

August 17, 2024

GitHub Readme Template: For Personal Projects
programming GitHub Readme Template: For Personal Projects

September 9, 2024