One Liner: Cleaning Up Old Git Branches

I am very lazy about deleting local git branches. I just checked and I have 100+ branches! WTF. That makes it a real pain to find the one I was working on yesterday. So first, let me get rid of the ones that I’ve already merged into master.

git branch --merged | egrep -v '(master|develop)' | xargs git branch 

But I still have 58 branches left! Lets try sorting by committer date:

$ git for-each-ref --sort=committerdate refs/heads/ --format='%(refname:short) "%(committerdate:relative)"'

This command gives me a list of branches sorted by commit date. The output looks something like:

BL-1302567-dnp-global-dns "10 months ago"
BL-1311324-scheduled-scale-down "8 months ago"
hackweek-dnp-deploy "4 months ago"
BL-1305305-dnp-secretsmanager "3 months ago"
BL-1311060-dnp-deploy-v2.6.1 "3 months ago"
dnp-breakfix "3 months ago"
BL-1307167-sas-api-elb "2 months ago"
BL-1307165-patch "9 weeks ago"
BL-1307166-sas-dns "8 weeks ago"
BL-1307441-product-project "8 weeks ago"
sas-deploy-fix "8 weeks ago"
... 

Now I can use this to find branches where the top commit is older than a few months

git for-each-ref --sort=committerdate refs/heads/ --format='%(refname:short) "%(committerdate:relative)"' | egrep -v "(master|develop)" | egrep "([4-9]|[1][0-2]) months ago" | xargs -n2 bash -c 'git branch -D $0'

Here I used egrep to remove the master and develop branches as a precaution, then again to select only branches that are 4 “months ago” or older. Then just pass the output to xargs and you’re all set.

Use ZenHub.io to organize your github issues into a scum board

Just a quick endorsement. I’ve been very much enjoying using zenhub.io for work lately. I’m managing the development of “Terminus” which is Pantheon’s command line utility. Zenhub.io is a chrome extension that allows you turn the normal issue queue via into a pseudo scrum board.

zenhub.io turns your normal issue queue into a scrumboard

zenhub.io turns your normal issue queue into a scrumboard

Getting rid of .git untracked file errors with git clean

The following untracked working tree files would be overwritten by checkout:

Nothing drives me crazier than trying checkout a git branch and getting this error.

$ git checkout develop
error: The following untracked working tree files would be overwritten by checkout:
	tests/fixtures/5e1c60f7048c2630c76c7e268ed7ad55
Please move or remove them before you can switch branches.
Aborting

This usually happens to me when something in my application creates cache files and the git ignore wasn’t configured. So there are several different ways to solve this depending on what you want to do.

If you want to keep the files just commit them like the warning prompts you to. In this case:

git add tests/fixtures/
git commit -m "Adding fixture files" tests/fixtures/

But sometimes you don’t want to keep them so you delete the file with:

rm -rf tests/fixtures/5e1c60f7048c2630c76c7e268ed7ad55

But then you just get the same error for another file in the same directory. You can’t delete the whole directory because there are some files in it that ARE committed and need to be kept.

What I learned today is that you can use git’s ‘clean’ function to remove these.

git clean -f tests/fixtures/

The -f flag here will force delete all of the untracked files in that directory. Alternatively if you want to be more careful you can use -i for an interactive prompt:

$ git clean -i tests/
Would remove the following items:
  tests/test-input-helper.php   tests/test-site-workflow.php
*** Commands ***
    1: clean                2: filter by pattern    3: select by numbers    4: ask each             5: quit
    6: help
What now> 5
Bye.