Skip to content

Instantly share code, notes, and snippets.

@paulresdat
Last active August 1, 2019 21:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paulresdat/227433e87b2f9137ea806dc1a4d687c4 to your computer and use it in GitHub Desktop.
Save paulresdat/227433e87b2f9137ea806dc1a4d687c4 to your computer and use it in GitHub Desktop.
Handy automated bash commands for git
#!/usr/bin/bash
git-diff() {
correct=$(git diff 2>&1)
if [[ $correct = *"Not a git repository"* ]]; then
echo $correct
return
fi
if [ $# -eq 0 ]; then
read loginput < <(git log)
echo "$loginput"
IFS=' ' read -r -a array <<< "$loginput"
git diff "${array[1]}"
else
fileName=$1
shift;
find ./ -name "$fileName" -print | while read f; do
git diff "$@" $f
done
fi
}
git-blame() {
# this finds my file in my working directory and git blames it for me WITHOUT
# having to get the full directory path!
find ./ -name "$1" -print | while read f; do
echo ""
echo "=====BLAMING $f"
if [ $# -eq 2 ]; then
git blame "$f" $2
elif [ $# -eq 3 ]; then
git blame "$f" -L"$2",+"$3"
else
shift;
git blame "$f" "$@"
fi
done
}
git-blamer() {
# same git blame except you pass in a regular expression
find ./ -regextype posix-extended -regex "$1" -print | while read f; do
echo ""
echo "=====BLAMING $f"
if [ $# -eq 2 ]; then
git blame "$f" $2
elif [ $# -eq 3 ]; then
git blame "$f" -L"$2",+"$3"
else
shift;
git blame "$f" "$@"
fi
done
}
git-reset() {
if [ $# -ne 0 ]; then
for var in "$@"
do
git checkout -- "$var"
done
fi
}
git-show() {
if [ $# -ne 0 ]; then
git show "$@"
else
correct=$(git diff 2>&1)
if [[ $correct = *"Not a git repository"* ]]; then
echo $correct
return
fi
read loginput < <(git log)
echo "$loginput"
IFS=' ' read -r -a array <<< "$loginput"
if [ $# -eq 0 ]; then
git show "${array[1]}"
else
echo "Not supported"
fi
fi
}
git-tree() {
if [ $# -ne 0 ]; then
git diff-tree --no-commit-id --name-only -r "$@"
else
# first check in valid git directory
correct=$(git diff 2>&1)
if [[ $correct = *"Not a git repository"* ]]; then
echo $correct
return
fi
read loginput < <(git log)
echo "$loginput"
IFS=' ' read -r -a array <<< "$loginput"
if [ $# -eq 0 ]; then
git diff-tree --no-commit-id --name-only -r "${array[1]}"
else
if [ $1 -eq "-c" ]; then
shift
git diff-tree --no-commit-id --name-only -r "$@"
else
git diff-tree "$@" "${array[1]}"
fi
fi
fi
}
git-test() {
CUR=`git rev-parse --abbrev-ref HEAD`
MONTH=`date +%b | tr '[A-Z]' '[a-z]'`
YEAR=`date +%y`
TEST=test_$MONTH$YEAR
git checkout $TEST
}
git-test-branch() {
local TEST=''
if [ $# -lt 1 ]
then
local MONTH=`date +%b | tr '[A-Z]' '[a-z]'`
local YEAR=`date +%y`
TEST=test_$MONTH$YEAR
else
TEST=$1
fi
echo "$TEST"
}
git-intranet-merge-test-branch() {
set -e
local CUR=`git rev-parse --abbrev-ref HEAD`
# Use current month/year for branch name if not given
local TEST=$(git-test-branch)
git checkout $TEST
git pull origin $TEST
git merge $CUR
git push origin $TEST
}
git-shorthands() {
# shorthands so handy that I want to keep them
alias gpf='git push --force-with-lease'
alias gs='git status'
alias gb='git branch'
# run this:
# $ complete | grep git
# for the correct complete command
alias g='git'
complete -o bashdefault -o default -o nospace -F __git_wrap__git_main g
alias printmerge='echo $(git-test-branch)'
alias merge-into-test='git-intranet-merge-test-branch'
}
git-aliases() {
type git > /dev/null 2>&1
if [ $? -eq 0 ]
then
local git_conf="git config --global"
$git_conf rebase.autosquash true
$git_conf color.status always
$git_conf rerere.enabled true
$git_conf alias.coo '!f() {
echo $(git branch -l | grep $1 | wc -l) branches found;
git checkout $(git branch -l --sort=-committerdate | grep $1 | head -1 | sed '"'"'s|^\\*||'"'"');
}; f'
$git_conf alias.gp "grep --break --heading"
$git_conf alias.co "checkout"
$git_conf alias.cr "checkout --"
$git_conf alias.newbranch "checkout -b"
$git_conf alias.newb "checkout -b"
$git_conf alias.l "log --graph --abbrev-commit --decorate --date=relative --format=format:'%C(bold red)%h%C(reset) - %C(bold green)(%ar)%C(reset) %C(white)%an - %s%C(reset)%C(bold yellow)%d%C(reset)'"
$git_conf alias.ll "log --stat"
$git_conf alias.m "merge"
$git_conf alias.mb '!git branch --merged | egrep -v "(^\*|master|release|test.*)"'
$git_conf alias.mbd '!git branch --merged | egrep -v "(^\*|master|release|test.*)" | xargs git branch -d'
$git_conf alias.checkoutpull '!gop() {
git coo "$@"
git pull -p
}; gop'
$git_conf alias.cp "checkoutpull"
$git_conf alias.pl '!gpl() {
if [ $# -eq 0 ]; then
git pull -p
else
git pull "$@"
fi
}; gpl'
$git_conf alias.f "fetch -p"
$git_conf alias.p "push"
$git_conf alias.c "commit"
$git_conf alias.cm "commit -m"
$git_conf alias.ca "commit --amend"
$git_conf alias.filereset '!greset() {
if [ $# -ne 0 ]; then
for var in "$@"
do
git checkout -- "$var"
done
fi
}; greset'
$git_conf alias.fr "filereset"
$git_conf alias.a '!ga() {
if [ $# -eq 0 ]; then
git add .
else
git add "$@"
fi
}; ga'
$git_conf alias.d 'diff'
$git_conf alias.pu '!gpu() {
currentbranch=$(git rev-parse --abbrev-ref HEAD 2>&1)
git push --set-upstream origin "$currentbranch"
}; gpu'
$git_conf alias.go '!go() {
if [ $# -ne 0 ]; then
if [ $1 = "test" ]; then
TEST=$(git-test-branch)
git checkout "$TEST"
else
git checkout "$@"
fi
fi
}; go'
$git_conf alias.sb '!gsb() {
git branch | grep "$@"
}; gsb'
$git_conf alias.wow '!echo "
wow
such program
▄ ▄ many data
▌▒█ ▄▀▒▌
▌▒▒█ ▄▀▒▒▒▐
▐▄▀▒▒▀▀▀▀▄▄▄▀▒▒▒▒▒▐ very much like linux
▄▄▀▒░▒▒▒▒▒▒▒▒▒█▒▒▄█▒▐ so program
▄▀▒▒▒░░░▒▒▒░░░▒▒▒▀██▀▒▌
▐▒▒▒▄▄▒▒▒▒░░░▒▒▒▒▒▒▒▀▄▒▒▌
▌░░▌█▀▒▒▒▒▒▄▀█▄▒▒▒▒▒▒▒█▒▐
▐░░░▒▒▒▒▒▒▒▒▌██▀▒▒░░░▒▒▒▀▄▌ Wow so git
▌░▒▄██▄▒▒▒▒▒▒▒▒▒░░░░░░▒▒▒▒▌
▌▒▀▐▄█▄█▌▄░▀▒▒░░░░░░░░░░▒▒▒▐
▐▒▒▐▀▐▀▒░▄▄▒▄▒▒▒▒▒▒░▒░▒░▒▒▒▒▌
▐▒▒▒▀▀▄▄▒▒▒▄▒▒▒▒▒▒▒▒░▒░▒░▒▒▐ so much lockd
▌▒▒▒▒▒▒▀▀▀▒▒▒▒▒▒░▒░▒░▒░▒▒▒▌
▐▒▒▒▒▒▒▒▒▒▒▒▒▒▒░▒░▒░▒▒▄▒▒▐
▀▄▒▒▒▒▒▒▒▒▒▒▒░▒░▒░▒▄▒▒▒▒▌ all the KLOC
▀▄▒▒▒▒▒▒▒▒▒▒▄▄▄▀▒▒▒▒▄▀ wow
▀▄▄▄▄▄▄▀▀▀▒▒▒▒▒▄▄▀
▒▒▒▒▒▒▒▒▒▒▀▀"'
fi
}
git-aliases
git-shorthands
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment