Skip to content

Instantly share code, notes, and snippets.

@xml
Last active April 13, 2018 23:42
Show Gist options
  • Save xml/6e79bc44bef9c95ed9d6b11fb841ccb1 to your computer and use it in GitHub Desktop.
Save xml/6e79bc44bef9c95ed9d6b11fb841ccb1 to your computer and use it in GitHub Desktop.
Git: Catch Up Trunk and Rebase against Trunk
# Add these scripts to your .profile or .bash_profile or .zshrc, or wherever you shell-script
# (FWIW, I keep core scripts in my .profile, then import .profile into zsh or bash configs)
# PURPOSE:
# When developing, you want to develop a critical reflex/hygiene:
# keeping your working/feature branch updated against your trunk branch.
# But this takes time and attention, partly because your working branch can have an arbitrary name to remember/type.
# We want to "make the right thing the easy thing." (h/t: Mike Bland)
# These convenience scripts turn that process into a one-command operation.
# COMPLICATION:
# Some folks like to *rebase* onto trunk. Some folks like to *merge* from trunk.
# We do both! These scripts do both!
# COMPLICATION:
# Some folks' trunk is `master`. Some folks' trunk is `develop`.
# We do both! These scripts do both!
# COMPLICATION:
# You would like to rebase, but sometimes rebasing gets complicated when there are many conflicts!
# I often default to rebasing, then fall back to simple merge if rebasing isn't worth the time.
# Try `turt` (or `murm` or `durd`) first, but if the rebase fails, just `git rebase --abort`,
# then switch to `tumt` (or 'mumm' or 'dumd')
# COMPLICATION:
# Solving for all these options means some folks find these script names hard to remember.
# Please rename them to whatever your heart desires! Or take only the ones you want! Remix them!
# Shared on the MIT License, without restriction: https://opensource.org/licenses/MIT
# USE: REBASING:
# TO REBASE ONTO UPDATED TRUNK IF TRUNK IS MASTER:
# `turt master`
# (or `murm`, for branch-specific shorthand)
# TO REBASE ONTO UPDATED TRUNK IF TRUNK IS DEVELOP:
# `turt develop`
# (or `durd`, for branch-specific shorthand)
# USE: MERGING
# TO MERGE FROM TRUNK IF TRUNK IS MASTER:
# `tumt master`
# (or `mumm`, for branch-specific shorthand)
# TO MERGE FROM TRUNK IF TRUNK IS DEVELOP:
# `tumt develop`
# (or `dumd`, for branch-specific shorthand)
function tu() {
# _T_runk _U_pdate: general function for updating a trunk branch
# 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. return to current branch
echo "**tu**: $trunk updated, returning to $branch..."
git checkout $branch
else
echo "**tu**: $trunk branch does not exist. Exiting."
set -e
fi
}
function mu() {
# Branch-Specific Shorthand form of tu() for MASTER
# DEPENDS ON tu() !
tu master
}
function du() {
# Branch-Specific Shorthand form of tu() for DEVELOP
# DEPENDS ON tu() !
tu develop
}
function turt() {
# turt == Trunk Update, Rebase against Trunk
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
}
function murm() {
# murm == Master Update, Rebase against Master
# DEPENDS ON turt() !
turt master
}
function durd() {
# durd == Develop Update, Rebase Develop
# DEPENDS ON turt() !
turt develop
}
function tumt() {
# turt == Trunk Update, Merge Trunk
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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment