Git Helpers
Generally speaking, I aim to keep my merges to master pretty short. As a result, I found myself repeating a lot of small commands. Attached are a couple scripts I use to push through those tedious moments and keep things moving to the master branch.
These can also be found in this git repo.
destroy-branch.sh
I use this one after a branch I’ve been working on is merged into the master branch through the Github UI. Once I return to the command line, I’m usually still on the merged branch and want to get the latest so that I can create a new branch.
function get_current_branch() {
git branch | grep \* | cut -d ' ' -f2
}
BRANCH_TO_DELETE=$(get_current_branch)
git checkout master && git pull origin master && git branch -D "$BRANCH_TO_DELETE"
git-reconcile.sh
This one is handy when I need to refresh my working branch to pull in changes that have already been merged. It HAS gone sideways on me – I think I had nothing to stash and I popped something old on top of my changes. Still, it’s about 88% effective.
function get_branch() {
git branch | grep \* | cut -d ' ' -f2
}
ORIGINAL_BRANCH=$(get_branch)
echo $ORIGINAL_BRANCH
git stash save
git checkout master
git pull origin master
git checkout ${ORIGINAL_BRANCH}
git rebase master
git stash pop