Skip to content

Instantly share code, notes, and snippets.

@hhenrichsen
Last active September 2, 2021 17:32
Show Gist options
  • Save hhenrichsen/6d0a734040e90aca3a7b6abed1306eee to your computer and use it in GitHub Desktop.
Save hhenrichsen/6d0a734040e90aca3a7b6abed1306eee to your computer and use it in GitHub Desktop.
#!/bin/bash
get_id () {
PATTERN=$@
COMMIT_ID=$(gx l | grep "$PATTERN" | grep -v "fixup!" | cut -f1 -d' ' | head -n1)
echo "$COMMIT_ID"
shift
}
amend_all () {
git commit -a --amend --no-edit
}
current_branch () {
git branch --show-current
}
CURRENT_BRANCH="$(current_branch)"
BASE_TAG="$(current_branch)-base"
check_tag () {
git tag | grep $1
}
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
aa|amend-all)
amend_all
shift
;;
l|log)
git --no-pager log --decorate=short --pretty=oneline master..$(gx cb)
shift
;;
lg|log-graph)
git log --decorate=short --pretty=oneline --graph
shift
;;
id|get-id)
get_id $2
shift
shift
;;
us|unstage)
git restore --staged $2
shift
shift
;;
sb|switch-base)
OLD=$2
NEW=$3
git rebase --onto $NEW $OLD $(gx current-branch)
shift
shift
shift
;;
nb|new-branch)
BRANCH_NAME=$2
git tag "$2-base"
git checkout -b $2
shift
shift
;;
fxrb|fixup-rebase)
shift
;;
sf|stage-fixup)
COMMIT_ID=$(gx get-id $2)
git --no-pager show --stat --oneline --name-only $COMMIT_ID | tail -n +2 | xargs -d '\n' git add
shift
shift
;;
fx|fixup)
git commit --fixup=$(gx get-id $2)
shift
shift
;;
fixup-all)
IDS=$(gx since-base)
for id in $IDS; do
if [[ $(git show-branch --no-name $id) == fixup!* ]]; then
continue
fi
gx fixup-autofile $id
done
shift
;;
pull-rebase)
TARGET=$2
git fetch -p
git pull --rebase origin $TARGET
git tag -f "$CURRENT_BRANCH-base" origin/$TARGET
shift
shift
;;
fxa|fixup-autofile)
COMMIT_ID=$(gx get-id $2)
gx sf $COMMIT_ID
git commit --fixup=$COMMIT_ID
shift
shift
;;
cb|current-branch)
echo $CURRENT_BRANCH
shift
;;
since-base)
git --no-pager log --pretty=format:"%h" "$(gx cb)-base"..$(gx cb)
echo
shift
;;
pf|push-force)
git push --force-with-lease
shift
;;
dm|diff-master)
git diff master
shift
;;
db|diff-branch)
git diff $2
shift
;;
do|diff-origin)
git diff origin/$(gx current-branch)
shift
;;
ro|origin)
git reset --soft origin/$(gx current-branch)
shift
;;
roh|origin-hard)
git reset --hard origin/$(gx current-branch)
shift
;;
rh|reset-head|head)
git reset --hard HEAD
shift
;;
cs|count-since)
BRANCH=$(git branch --show-current)
PARENT=$2
COMMIT_COUNT=$(git rev-list --count $BRANCH ^$PARENT)
echo "$COMMIT_COUNT"
shift
shift
;;
b|back)
git checkout HEAD^
shift
;;
rb|reset-back)
git reset --soft HEAD^
shift
;;
rbh|reset-back-hard)
git reset --hard HEAD^
shift
;;
br|branch)
PATTERN=$2
BRANCH_NAME=$(git --no-pager branch | grep "$PATTERN")
if [ -n "$BRANCH_NAME" ]; then
git checkout $BRANCH_NAME
else
echo "No branch found."
fi
shift
shift
;;
brd|branch-delete)
PATTERN=$2
BRANCH_NAME=$(git --no-pager branch | grep "$PATTERN")
if [ -n "$BRANCH_NAME" ]; then
git branch -d $BRANCH_NAME
else
echo "No branch found."
fi
shift
shift
;;
arb|auto-rebase)
BRANCH=$(git branch --show-current)
PARENT=$2
COMMIT_COUNT=$(gx count-since $PARENT)
echo "$COMMIT_COUNT commits on $BRANCH since $PARENT"
echo "Executing 'git rebase -i --autosquash HEAD~$COMMIT_COUNT'"
shift
shift
read -p "Press any key to execute."
git rebase -i --autosquash HEAD~$COMMIT_COUNT
;;
s|status)
git status # Because I keep on typing 'gx status'
shift
;;
hfb|how-far-behind)
git --no-pager show $(git merge-base origin/master $2) --pretty=format:'%ar' --no-patch
echo ""
shift
shift
;;
*)
echo "Unknown command 'gx $1'"
shift
;;
esac
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment