Skip to content

Instantly share code, notes, and snippets.

@dskindell
Last active March 22, 2019 17:42
Show Gist options
  • Save dskindell/c88b64e8444346757f8e5ccfcc9e024d to your computer and use it in GitHub Desktop.
Save dskindell/c88b64e8444346757f8e5ccfcc9e024d to your computer and use it in GitHub Desktop.
Personal Git configs/aliases/scripts
[user]
name = David Skindell
[core]
editor = vim
excludesfile = ~/.gitignore_global
pager = less -FX
[color]
ui = True
[push]
default = simple
[include]
path = ~/.git_aliases
[filter "lfs"]
required = true
clean = git-lfs clean -- %f
smudge = git-lfs smudge -- %f
process = git-lfs filter-process
#!/bin/bash
# by http://github.com/jehiah
# this prints out some branch status (similar to the '... ahead' info you get from git status)
# example:
# $ git branch-status
# dns_check (ahead 1) | (behind 112) origin/master
# master (ahead 2) | (behind 0) origin/master
red='\033[0;31m'
nc='\033[0m'
green='\033[1;32m'
git for-each-ref --format="%(refname:short) %(upstream:short)" refs/heads | \
while read local remote
do
[ -z "$remote" ] && continue
git rev-list --left-right ${local}...${remote} -- 2>/dev/null >/tmp/git_upstream_status_delta || continue
LEFT_AHEAD=$(grep -c '^<' /tmp/git_upstream_status_delta)
RIGHT_AHEAD=$(grep -c '^>' /tmp/git_upstream_status_delta)
echo -e "$local ${green}(ahead $LEFT_AHEAD)${nc} | ${red}(behind $RIGHT_AHEAD)${nc} $remote"
done
#!/bin/bash
# This fetches and checks-out a gerrit review branch
# There are 2 froms:
#
# Form 1 (1 argument)
# $ git checkout-review change-num/patchset
# Checks out the specified review in a detached HEAD state
# $ git checkout-review 176/1
#
# Form 2 (2 arguments)
# $ git checkout-review change-num/patchset <branch-name>
# Checks out the specified review in a locally created branch named <branch-name>
# $ git checkout-review 176/1 bug/essms-123-terse-name
#
remote_name=`git remote | head -n1`
remote_branch=`git rev-parse --abbrev-ref --symbolic-full-name @{u} 2>/dev/null`
if [ ! -z "$remote_branch" ]; then
remote_name=`echo $remote_branch | cut -d/ -f1`
fi
full_change_id=$1
change_id=$(echo $full_change_id | cut -f 1 -d "/")
patchset=$(echo $full_change_id | cut -f 2 -d "/")
change_dir=${change_id:(-2)}
if [ ${#change_dir} -lt 2 ]; then
change_dir=${change_id:(-1)}
change_dir="0${change_dir}"
fi
if [ "$2" != "" ]; then
git fetch $remote_name refs/changes/$change_dir/$change_id/$patchset:$2 && git checkout $2
elif [ "$1" != "" ]; then
git fetch $remote_name refs/changes/$change_dir/$change_id/$patchset && git checkout FETCH_HEAD
else
echo "Missing arguments:"
echo "Usage: git checkout-review #/# [new-local-branch-name]"
exit 1
fi
#!/bin/bash
# Compare the patches made by 2 seperate commits.
# This is useful for comparing cherry-pick commits
diff <(git show $1) <(git show $2)
exit $?
#!/bin/bash
red='\033[0;31m'
nc='\033[0m'
green='\033[1;32m'
purple='\033[1;35m'
main() {
REMOTES="$@";
if [ -z "$REMOTES" ]; then
REMOTES=$(git remote);
fi
REMOTES=$(echo "$REMOTES" | xargs -n1 echo)
CLB=$(git branch -l|awk '/^\*/{print $2}');
echo "$REMOTES" | while read REMOTE; do
git remote update $REMOTE
git remote show $REMOTE -n \
| awk '/merges with remote/{print $5" "$1}' \
| while read line; do
RB=$(echo "$line"|cut -f1 -d" ");
ARB="refs/remotes/$REMOTE/$RB";
LB=$(echo "$line"|cut -f2 -d" ");
ALB="refs/heads/$LB";
NBEHIND=$(( $(git rev-list --count $ALB..$ARB 2>/dev/null) +0));
NAHEAD=$(( $(git rev-list --count $ARB..$ALB 2>/dev/null) +0));
if [ "$NBEHIND" -gt 0 ]; then
if [ "$NAHEAD" -gt 0 ]; then
echo -e " Branch $LB is $NBEHIND commit(s) behind and $NAHEAD commit(s) ahead of $REMOTE/$RB. ${red}Could not be fast-forwarded.${nc}";
elif [ "$LB" = "$CLB" ]; then
echo -e " Branch $LB was $NBEHIND commit(s) behind of $REMOTE/$RB. ${purple}Fast-forward merge performed.${nc}";
git merge -q $ARB;
else
echo -e " Branch $LB was $NBEHIND commit(s) behind of $REMOTE/$RB. ${purple}Resetting local branch to remote.${nc}";
git branch -l -f $LB -t $ARB >/dev/null;
fi
else
echo -e " Branch $LB is up to date. ${green}No changes made.${nc}";
fi
done
done
}
main $@
#!/bin/bash
# This fetches and cherry-picks a gerrit review branch
# There is 1 from:
#
# Form 1 (1 argument)
# $ git pick-review change-num/patchset
# Cherry picks the specified review into the current branch
# $ git pick-review 176/1
#
remote_name=`git remote | head -n1`
remote_branch=`git rev-parse --abbrev-ref --symbolic-full-name @{u} 2>/dev/null`
if [ ! -z "$remote_branch" ]; then
remote_name=`echo $remote_branch | cut -d/ -f1`
fi
full_change_id=$1
change_id=$(echo $full_change_id | cut -f 1 -d "/")
patchset=$(echo $full_change_id | cut -f 2 -d "/")
change_dir=${change_id:(-2)}
if [ ${#change_dir} -lt 2 ]; then
change_dir=${change_id:(-1)}
change_dir="0${change_dir}"
fi
if [ "$1" != "" ]; then
git fetch $remote_name refs/changes/$change_dir/$change_id/$patchset && git cherry-pick FETCH_HEAD
else
echo "Missing arguments:"
echo "Usage: git pick-review #/#"
exit 1
fi
#!/bin/bash
# This pushes commits to gerrit code review.
#
# Form 1 (1 argument)
# $ git post-gerrit [private|wip|ready]
#
# Form 2 (2 argument)
# $ git post-review [private|wip|ready] <remote-branch>
#
# Form 3 (3 arguments)
# $ git post-review [private|wip|ready] <local-branch> <remote-branch>
local_branch=`git rev-parse --abbrev-ref HEAD`
remote_name=`git remote | head -n1`
remote_branch=`git rev-parse --abbrev-ref --symbolic-full-name @{u} 2>/dev/null`
if [ ! -z "$remote_branch" ]; then
remote_name=`echo $remote_branch | cut -d/ -f1`
fi
if [ -z "${remote_branch}" ]; then
remote_branch=$local_branch
else
remote_branch=${remote_branch#${remote_name}/}
fi
if [ "$3" != "" ]; then
local_branch=$2
remote_branch=$3
elif [ "$2" != "" ]; then
remote_branch=$2
fi
git push $remote_name $local_branch:refs/for/$remote_branch%$1
#!/bin/bash
# This fetches and merges a gerrit review branch
# There are 3 froms:
#
# Form 1 (1 argument)
# $ git merge-review change-num/patchset
# Merges the specified review into the current branch
# $ git merge-review 176/1
#
# Form 2 (2 arguments)
# $ git merge-review change-num/patchset <branch-name>
# Checks out the specified review in a locally created branch named <branch-name> and then merges into current branch
# $ git merge-review 176/1 bug/essms-123-terse-name
#
# Form 3 (3 arguments)
# $ git merge-review change-num/patchset <from-branch-name> <to-branch-name>
# Checks out the specified review in a locally created branch named <from-branch-name> and merges into <to-branch-name>
# $ git merge-review 176/1 bug/essms-123-terse-name devlop
#
remote_name=`git remote | head -n1`
remote_branch=`git rev-parse --abbrev-ref --symbolic-full-name @{u} 2>/dev/null`
if [ ! -z "$remote_branch" ]; then
remote_name=`echo $remote_branch | cut -d/ -f1`
fi
full_change_id=$1
change_id=$(echo $full_change_id | cut -f 1 -d "/")
patchset=$(echo $full_change_id | cut -f 2 -d "/")
change_dir=${change_id:(-2)}
if [ ${#change_dir} -lt 2 ]; then
change_dir=${change_id:(-1)}
change_dir="0${change_dir}"
fi
if [ "$3" != "" ]; then
git checkout $3 && git fetch $remote_name refs/changes/$change_dir/$change_id/$patchset:$2 && git merge $2
elif [ "$2" != "" ]; then
git fetch $remote_name refs/changes/$change_dir/$change_id/$patchset:$2 && git merge $2
elif [ "$1" != "" ]; then
git fetch $remote_name refs/changes/$change_dir/$change_id/$patchset && git merge FETCH_HEAD
else
echo "Missing arguments:"
echo "Usage: git merge-review #/# [new-local-branch-name] [merge-to-branch]"
exit 1
fi
#!/bin/bash
# This creates a topic/ branch based from the last tag point.
# The new branch name will using the following format:
# <arg0>/<current-branch-name>/<arg1>
# If a 3rd param is given, the branch created will be:
# <arg0>/<arg2>/<arg1>
# Where the tag point is taken from <arg2>
# example:
#
# $ git rev-parse --abbrev-ref HEAD
# master
#
# $ git tag
# v2.70.7.0
# v2.70.8.0
# v2.70.9.0
#
# $ git describe
# v2.70.9.0-65-3ef1245
#
# $ git topic-branch bug essa-1234-bad-logic
#
# $ git branch
# *master
# bug/master/essa-1234-bad-logic
#
# $ git checkout bug/master/essa-1234-bad-logic
#
# $ git describe
# v2.70.9.0
if [ -z "$1" ]; then
echo "Missing topic type [bug|story|exp|wip|etc...]"
exit 1
fi
if [ -z "$2" ]; then
echo "Missing branch name <id>-<terse-name>"
exit 1
fi
upstream_branch_name=`git rev-parse --abbrev-ref HEAD`
if [ ! -z "$3" ]; then
upstream_branch_name=$3
fi
lkgr_tag=`git describe --abbrev=0 $upstream_branch_name`
new_branch_name="$1/$upstream_branch_name/$2"
echo git branch $new_branch_name $lkgr_tag
git branch $new_branch_name $lkgr_tag
git branch -u $upstream_branch_name $new_branch_name
[alias]
co = checkout
cos = !git checkout && git submodule update --recursive
sb = show-branch
ec = config --global -e
subup = submodule update --init --recursive
up = !git pull --rebase --prune $@ && git submodule update --init --recursive
cob = checkout -b
cm = !git add -A && git commit -m
lf = log --pretty=fuller --decorate
lff = log --pretty=fuller --decorate --patch
ls = log --pretty=format:"%C(yellow)%h%Cred%d\\ %Creset%s%Cblue\\ [%cn]" --decorate
ll = log --pretty=format:"%C(yellow)%h%Cred%d\\ %Creset%s%Cblue\\ [%cn]" --decorate --numstat
lds = log --pretty=format:"%C(yellow)%h\\ %ad%Cred%d\\ %Creset%s%Cblue\\ [%cn]" --decorate --date=short
ld = log --pretty=format:"%C(yellow)%h\\ %ad%Cred%d\\ %Creset%s%Cblue\\ [%cn]" --decorate --date=relative
lgs = log --graph --oneline --decorate --color
lgsl = log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit
lgsla = log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --all
lts = log --oneline --decorate --tags --no-walk
dl = "!git ll -1"
dlc = diff --cached HEAD^
dfc = diff --cached
la = "!git config -l | grep alias | cut -c 7-"
last = log -1 --stat
cp = cherry-pick
cl = clone
ci = commit --verbose
st = status -sb
br = branch
unstage = reset HEAD --
dc = diff --cached
refresh = commit --amend --no-edit
reword = commit --verbose --amend
back = !git reset --hard HEAD~$1
cm-ref1 = !git add -A && git commit -C HEAD@{1}
last-tag = describe --abbrev=0
prune-tags = !git tag -l | xargs git tag -d && git fetch -t
merge-log = !git log $1^..$1
fixup = !git add . && git commit -m \"fixup: something\"
# The following require helper scripts
fbs = !git fetch --all && git branch-status
fup = !git fetch --prune $@ && git ffwd-update
bug-branch = topic-branch bug
story-branch = topic-branch story
exp-branch = topic-branch exp
cr = post-review
post-ready = post-gerrit ready
post-review = post-gerrit ready
post-draft = post-gerrit private
post-private = post-gerrit private
post-wip = post-gerrit wip
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment