Git Clean-up

John Cox

--

This is a quick one.

First, the workflow I’ve been using with git/Github goes as follows:
- checkout and pull latest on master
- create a new branch and push it up
- run package manager stuff and/or db migrations
- do code
- push up changes
- create a pull request
- merge the pull request
- delete the remote branch (by clicking the button in Github UI)
- delete the local branch
- (repeat)

I felt a need to automate this as much as possible. It’s not that the process is terribly arduous. But it is repetitive. So I wrote a little script that I named git-cleanup. Here it is:

#!/bin/bash
git checkout master
git pull
bundle || true
rake db:migrate || true
yarn install || true
git branch | grep -v "master" | xargs git branch -D
if [ ! -z "$1" ]
then
git checkout -b "$1"
git push -u origin "$1"
fi

Note: I’ve been working in Rails and React lately, so the script reflects those technologies. It can easily be adapted to your stack.

WARNING: This script will delete all local branches other than master as a part of the process. BE CAREFUL.

Now that you have that script, add it to your path and you should be ready to roll. I dropped it into /usr/local/bin/git-cleanup on my Mac.

Usage…
To simply checkout master and delete all other branches: git-cleanup. If you want to create a new branch and push up to a remote branch, pass in a branch name as an argument: git-cleanup branchName

That’s it. Hope this is useful to someone.

--

--

No responses yet

Write a response