Skip to content

Instantly share code, notes, and snippets.

@kane-c
Last active August 18, 2016 06:28
Show Gist options
  • Save kane-c/51407defea1e60580c29 to your computer and use it in GitHub Desktop.
Save kane-c/51407defea1e60580c29 to your computer and use it in GitHub Desktop.
Handy git shortcut scripts
#!/bin/sh -e
# Semantic versioning automatic bump and optional push
# First, add this to your `.gitconfig`
# [alias]
# max-tag = !echo $((echo $(git config --get bump.prefix)0.0.0 && git tag -l "$(git config --get bump.prefix || echo)*.*.*") | (git config --get bump.prefix && sed "s/$(git config --get bump.prefix)//g" || cat) | sort -s -t . -k 1,1n -k 2,2n -k 3,3n | tail -n 1)
# Then add this file (chmod +x) to your PATH.
# Optionally add a tag prefix (e.g. `v`) globally or per repo:
# git config --global --add bump.prefix v
# Ideas for future additions:
# * Flag for whether to prompt for confirmation (currently always does)
# * Flag for setting a tag message, or whether to prompt for one (currently always just uses the tag as the message)
PREFIX=$(git config --get bump.prefix || echo "")
PUSH=0
TAG=
usage() {
echo "Usage: git bump [major|minor|revision] [--push|-p]" > /dev/stderr
exit 1
}
bump_patch() {
TAG=$(git max-tag | awk -F . '{print $1 "." $2 "." ($3 + 1)}')
}
for var in "$@"; do
if [ "$var" = "-p" ] || [ "$var" = "--push" ]; then
PUSH=1
else
if [ "$TAG" = "" ]; then
case "$var" in
patch)
bump_patch
;;
minor)
TAG=$(git max-tag | awk -F . '{print $1 "." ($2 + 1) "." 0}')
;;
major)
TAG=$(git max-tag | awk -F . '{print ($1 + 1) "." 0 "." 0}')
;;
*)
usage
;;
esac
else
usage
fi
fi
done
if [ "$TAG" = "" ]; then
bump_patch
fi
TAG="$PREFIX$TAG"
echo "Tagging $(git rev-parse --short HEAD) ($(git name-rev --name-only HEAD)) as $TAG"
git --no-pager log -1 --pretty=%B
read -n1 -rsp $'Press any key to continue or Ctrl+C to exit...'
git tag -m "$TAG" "$TAG"
if [ "$PUSH" = "1" ]; then
git push origin "$TAG"
fi
#!/usr/bin/env sh
# Handy shortcut for a common series of git commands:
# 1. Stash, if there are uncommitted changes
# 2. Switch to the target branch
# 3. Pull
# 4. Delete the previous branch
# 5. Pop if stashed in #1
#
# This is the commonly used approach to updating a repo after the branch you
# were working on is merged
MASTER_BRANCH=$(git config --get gitflow.branch.master) || echo 'master'
TARGET_BRANCH=$(git config --get gitflow.branch.develop) || echo "$MASTER_BRANCH"
CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
if [ "$CURRENT_BRANCH" = "$TARGET_BRANCH" ] || [ "$CURRENT_BRANCH" = "$MASTER_BRANCH" ]; then
echo 'You are not on a branch; exiting'
exit
fi
git diff-index --quiet HEAD --
IS_DIRTY=$?
if [ "$IS_DIRTY" = 1 ]; then
git stash
fi
git checkout "$TARGET_BRANCH"
git pull
git branch -d "$CURRENT_BRANCH" || true
if [ "$IS_DIRTY" = 1 ]; then
git stash pop
fi
#!/usr/bin/env sh
# Handy shortcut for a common series of git commands:
# 1. Stash, if there are uncommitted changes
# 2. Switch to the main branch (e.g. master or develop)
# 3. Pull
# 4. Switch back to the current branch
# 5. Rebase it
#
# This is the commonly used approach to re-align your WIP branch with the main
# one.
MASTER_BRANCH=$(git config --get gitflow.branch.master) || echo 'master'
TARGET_BRANCH=$(git config --get gitflow.branch.develop) || echo "$MASTER_BRANCH"
CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
if [ "$CURRENT_BRANCH" = "$TARGET_BRANCH" ] || [ "$CURRENT_BRANCH" = "$MASTER_BRANCH" ]; then
echo 'You are not on a branch; exiting'
exit
fi
git diff-index --quiet HEAD --
IS_DIRTY=$?
if [ "$IS_DIRTY" = 1 ]; then
git stash
fi
git checkout "$TARGET_BRANCH"
git pull
git checkout "$CURRENT_BRANCH"
if ! git rebase "$TARGET_BRANCH"; then
echo 'Rebase failed. Fix up conflicts then continue.'
if [ "$IS_DIRTY" = 1 ]; then
echo 'Don''t forget to git stash pop'
fi
exit 1
fi
if [ "$IS_DIRTY" = 1 ]; then
git stash pop
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment