In the course of daily coding, you gradually pile up some little personal hacks you aren’t sure are worth publishing and sharing. The world of shell scripts in particular don’t seem to be nearly as commonly packaged and shared as, say, Javascript or Python. But, here are a couple of mine, for dealing with git repos, in case anyone ever finds them handy.
FWIW, I usually stash these in my ~/.profile
, as described in this post.
Sometimes, you’re working on a branch, and you just want to update trunk without much fuss and without leaving your current branch:
function tu() {
# tu =_T_runk _U_pdate: general function for updating a trunk branch while on another
# Defaults to master branch for basic git compatibility
# But takes alternate trunk branch name as only argument
trunk="master" && [[ $1 ]] && trunk=$1
echo "**tu**: trunk branch is defined as: $trunk"
# 1. cache current branch & take updates from default remote
branch=`git rev-parse --symbolic-full-name --abbrev-ref HEAD`
git fetch --prune
# 2. update trunk
echo "**tu**: Switching to trunk branch, $trunk..."
BRANCH_EXISTS=`git checkout $trunk`
# Just in case you don't actually have a branch with name, exit gracefully:
if [ $BRANCH_EXISTS ]; then
git pull
# 3. now that trunk is updated, return to previous branch
echo "**tu**: $trunk updated, returning to $branch..."
git checkout $branch
else
echo "**tu**: $trunk branch does not exist. Exiting."
set -e
fi
}
And, you can create shorthands for particular trunk branches you might be using:
function mu() {
# Branch-Specific Shorthand form of tu() for MASTER
# DEPENDS ON tu() !
tu master
}
(Which, in 2022, I probably oughta’ update to ‘Main’ branch. But… I’m playing catch-up here on sharing some old code.)
More commonly, and still useful, is the case where I’m mostly wanting to update my develop
branch:
function du() {
# Branch-Specific Shorthand form of tu() for DEVELOP
# DEPENDS ON tu() !
tu develop
}
OK, that’s maybe handy. Occasionally. But not something I obsess about. Why do I care? Ohhhh, right… REBASING.
I often want to do a quick rebase of my current branch onto the latest version of trunk. So, let’s put those building-blocks all together into a new more useful script which lets me do just that:
function turt() {
# turt == Trunk Update, Rebase against Trunk
# takes a single parameter: the name of your trunk branch, which defaults to master
localTrunk="master" && [[ $1 ]] && localTrunk=$1
# use first character of trunk name to customize output
first=${localTrunk:0:1}
echo "**${first}ur${first}**: First, update $localTrunk..."
tu $localTrunk
echo "**${first}ur${first}**: Now, rebase $branch onto $localTrunk..."
git rebase $trunk
}
And then to make it more specific to particular trunk branches you might be using:
function murm() {
# murm == Master Update, Rebase against Master
# DEPENDS ON turt() !
turt master
}
function durd() {
# durd == Develop Update, Rebase against Develop
# DEPENDS ON turt() !
turt develop
}
Of course, as much as I think most merges of trunk into a branch ought to be rebases instead, there’s a time and a place for merging in an updated trunk as well:
function tumt() {
# turt == Trunk Update, Merge Trunk
# takes a single parameter: the name of your trunk branch, which defaults to master
localTrunk="master" && [[ $1 ]] && localTrunk=$1
# use first character of trunk name to customize output
first=${localTrunk:0:1}
echo "**${first}um${first}**: First, update $localTrunk..."
tu $localTrunk
echo "**${first}um${first}**: Now, merge $localTrunk into $branch ..."
git merge $trunk
}
function mumm() {
# mumm == Master Update, Merge Master
# DEPENDS ON tumt() !
tumt master
}
function dumd() {
# durd == Develop Update, Merge Develop
# DEPENDS ON tumt() !
tumt develop
}