Git Shortcuts
Git commands can be repetitive and often times we only need 10% of the commands to be able to conduct most of the work. You can use the following “git shortcuts” to increase your productivity.
# Add
alias ga='echo git add;git add'
alias gap='echo git add -p;git add -p'
alias gaa='echo git add -A;git add -A'
alias gau='echo git add -u;git add -u'
# Seleche 'modified' files and adds them to git
alias gam='git status && git add -u && git status'
# Bisect
alias gbig='echo git bisect good;git bisect good'
alias gbib='echo git bisect bad;git bisect bad'
alias gbir='echo git bisect reset;git bisect reset'
# Branch
alias gb='echo git branch;git branch'
alias gbm='echo git branch -m (renaming current branch);git branch -m'
# Checkout
alias gco='echo git checkout;git checkout'
alias gcom='echo git checkout master;git checkout master'
# Cherry Pick
alias gcp='echo git cherry-pick;git cherry-pick'
alias gcpa='echo git cherry-pick --abort;git cherry-pick --abort'
alias gcpc='echo git cherry-pick --continue;git cherry-pick --continue'
# Clean
alias gc='echo git clean;git clean'
alias gcf='echo git clean -fdx;git clean -fdx'
alias gcff='echo git clean -ffdx;git clean -ffdx'
# Commit
alias gca='echo git commit --amend;git commit --amend'
alias gcan='echo git commit --amend --no-edit;git commit --amend --no-edit'
# Diff
alias gd='echo git diff;git diff'
alias gdh='echo git diff HEAD;git diff HEAD'
alias gdc='echo git diff --cached;git diff --cached'
# Fetch
alias gf='echo git fetch;git fetch'
alias gfp='echo git fetch --prune;git fetch --prune'
# Grep
alias gg='echo git grep;git grep'
alias ggr='echo git grep --recurse-submodules;git grep --recurse-submodules'
# Log
alias gl='echo git log;git log'
# Pull
alias gpl='echo git pull --rebase; git pull --rebase;'
alias gplo='echo git pull origin;git pull origin'
alias gplom='echo git pull origin master;git pull origin master'
alias gploh='echo git pull origin head;git pull origin head'
# Push
alias gpsonv='echo git push origin --no-verify; git push origin --no-verify'
alias gpsofnv='echo git push origin -f --no-verify; git push origin -f --no-verify'
alias gpso='echo git push origin;git push origin'
alias gpsof='echo git push origin -f;git push origin -f'
alias gpsoh='echo git push origin head;git push origin head'
alias gpsohf='echo git push origin head -f;git push origin head -f'
# Reset
alias grh='echo git reset --hard;git reset --hard'
alias grhh='echo git reset --hard HEAD; git reset --hard HEAD'
# Rebase
alias gr='echo git rebase;git rebase'
alias gri='echo git rebase -i;git rebase -i'
alias grm='echo git rebase master;git rebase master'
alias grc='echo git rebase --continue;git rebase --continue'
alias gra='echo git rebase --abort;git rebase --abort'
# Reflog
alias grefl='echo git reflog;git reflog'
# Show
alias gsh='echo git show HEAD;git show HEAD'
# Stash
alias gst='echo git stash;git stash'
alias gstpo='echo git stash pop;git stash pop'
alias gstps='echo git stash push;git stash push'
alias gsts='echo git stash show;git stash show'
alias gstl=' echo git stash list;git stash list'
alias gstc='echo git stash clear;git stash clear'
alias gstsap='echo git stash save -p;git stash save -p'
# Status
alias gs='echo git status;git status'
# Submodules
alias gsu='echo_and_run git submodule update'
alias gsui='echo_and_run git submodule update --init'
alias gsuir='echo_and_run git submodule update --init --recursive'
alias gsrh='echo_and_run git submodule foreach git reset --hard --recurse-submodules'
alias gscf='echo_and_run git submodule foreach --recursive git clean -fdx'
alias gscff='echo_and_run git submodule foreach --recursive git clean -ffdx'
# Update a PR by adding modified files, commit, squash, and then push to git with force flag
alias gupr="echo 'git [add modified]';echo 'git commit --amend --noedit';echo 'git push origin head -f';gam;git commit --amend --no-edit;git push origin head -f;git status"
# Create and checkout a new branch for an issue
gcob() {
if [[ $# -ne 2 ]]; then
# display usage
echo "Usage: gcob <issue-number> \"<issue-description>\""
else
echo "git checkout -b user/$SIBROS_EMAIL_USERNAME/$1-$2"
git checkout -b user/$SIBROS_EMAIL_USERNAME/$1-$2
fi
}
#Create and checkout a new branch for an epic or feature
alias gcobe='gcobf'
gcobf() {
if [[ $# -ne 2 ]]; then
# display usage
echo "Usage: gcobf <issue-number> \"<feature-description>\""
else
echo "git checkout -b feature/$1-$2"
git checkout -b feature/$1-$2
fi
}
# Checkout a branch by issue number (regardless if its under your user or not)
# This will checkout the first branch it finds that has user/.*/<Issue no>. It first searches local branches, then remote branches.
# If there is no local branch, but there is a remote branch, it will setup a remote tracking branch and check it out
gcoi() {
if [[ $# -ne 1 ]]; then
# display usage
echo "Usage: gcoi <issue-number>"
else
git fetch
# tries to find in local branch list
local branch_name=$(git branch | awk -F ' +' '! /\(no branch\)/ {print $2}' | grep -E -e "user/.*/#?$1" | head -n1)
if [ -z "$branch_name" ]; then # if can't find local branch, search remotes
branch_name=$(git branch -r | awk -F ' +' '! /\(no branch\)/ {print $2}' | grep -E -e "user/.*/#?$1" | head -n1 | awk '{split($0,branch_name,"origin/");print(branch_name[2])}')
fi
if [ -z "$branch_name" ]; then
echo -e "\033[0;31mBranch with issue #$1 does not exist on local nor remote!"
else
echo "Checking out $branch_name..."
git checkout $branch_name
fi
fi
}
# Checkout a release branch by issue/epic number
# This will checkout the first branch it finds that has feature/<Epic no>. It first searches local branches, then remote branches.
# If there is no local branch, but there is a remote branch, it will setup a remote tracking branch and check it out
alias gcoie='gcoif'
gcoif() {
if [[ $# -ne 1 ]]; then
# display usage
echo "Usage: gcoif <issue-number>"
else
git fetch
# tries to find in local branch list
local branch_name=$(git branch | awk -F ' +' '! /\(no branch\)/ {print $2}' | grep -E -e "feature/[&#]?$1" | head -n1)
if [ -z "$branch_name" ]; then # if can't find local branch, search remotes
branch_name=$(git branch -r | awk -F ' +' '! /\(no branch\)/ {print $2}' | grep -E -e "feature/[&#]?$1" | head -n1 | awk '{split($0,branch_name,"origin/");print(branch_name[2])}')
fi
if [ -z "$branch_name" ]; then
echo -e "\033[0;31mBranch for issue &$1 does not exist on local nor remote!"
else
echo "Checking out $branch_name..."
git checkout $branch_name
fi
fi
}
unalias gcm 2>/dev/null
# Commits and automatically prepends "closes #<Issue No.>: "
gcm() {
if [[ $# -ne 1 ]]; then
# display usage
echo "Usage: gcm \"Commit Msg\""
echo "Automatically Prepends \"closes #<Issue No.>: \""
else
# Todo: #2607 - remove support for parsing old branch name conventions
local issue_number=$(git rev-parse --abbrev-ref HEAD | cut -d/ -f3 | awk '{split($0, issue_num,"-");print(issue_num[1]);}' | tr -d '#&')
if [ -z "$issue_number" ]; then
echo -e "\033[0;31mCould not extract issue number from branch name!"
else
echo "git commit -m \"closes #$issue_number: $1\""
git commit -m "closes #$issue_number: $1"
fi
fi
}
# Performs the starting sequence of a git bisect 'git bisect start; git bisect bad; git bisect good <commit hash>
gbi() {
git bisect start
git bisect bad
git bisect good $1
}
unalias gcb 2>/dev/null
# Purge local/remote branches that are already merged into master
# gcb stands for "git clean branches". This is not an actual git command, but it describes what this command does
gcb() {
echo "Cleaning merged branches"
git fetch
gcom
gpl
echo -e "\033[0;31m"
# From: https://github.com/not-an-aardvark/git-delete-squashed
# Deletes branches that were squashed before merged
git for-each-ref refs/heads/ "--format=%(refname:short)" | while read branch; do mergeBase=$(git merge-base master $branch) && [[ $(git cherry master $(git commit-tree $(git rev-parse $branch^{tree}) -p $mergeBase -m _)) == "-"* ]] && git branch -D $branch; done
git remote prune origin # delete deleted remotes
git branch --merged origin/master | grep -v "\* master" | xargs -n 1 git branch -d # delete merged locals that didn't squash commits
echo -e "\033[0m"
gco -
}
#select a specific stash to apply to working branch
gstpos () {
if [[ $# -ne 1 ]]; then
# display usage conditions
echo "Usage: pop specific stash number (must exist in stash list)"
else
echo "Applying stash@{$1} contents"
git stash pop stash@{$1}
fi
}
SIBROS TECHNOLOGIES, INC. CONFIDENTIAL