Skip to content

Instantly share code, notes, and snippets.

@kujiy
Last active July 29, 2026 06:22
Show Gist options
  • Select an option

  • Save kujiy/558cc56f2c2921c71cd92b67d87ad5c8 to your computer and use it in GitHub Desktop.

Select an option

Save kujiy/558cc56f2c2921c71cd92b67d87ad5c8 to your computer and use it in GitHub Desktop.
Git / git-flow bash aliases & functions
###############################################################
# GIT / GIT FLOW ALIASES & FUNCTIONS
###############################################################
# git add --all and commit (with date message) and push
function gap {
regex="git version 1\.*"
if [[ `git --version` =~ $regex ]]; then
echo git version 1...
git add -A && git commit -m "`date`" && git push
else
echo git 2...
git add -A :/ && git commit -m "`date`" && git push
fi
}
alias gss="git status"
alias gd="git diff"
alias gdc="git diff --cached"
gdn() { git diff --no-index -- $1 $2; }
alias gb="git branch"
alias gbr="git branch -r"
# set upstream of the current branch to origin/<current branch>
function gbsu() {
git branch --set-upstream-to=origin/$(git rev-parse --abbrev-ref HEAD) $(git rev-parse --abbrev-ref HEAD)
}
# delete all local branches except protected ones (worktree branches are skipped)
function gbda() {
git for-each-ref --format='%(refname:short)' refs/heads \
| grep -Ev '^(main|master|develop|production|staging)$' \
| while read -r branch; do
if git worktree list --porcelain | grep -qx "branch refs/heads/$branch"; then
echo "skip worktree branch: $branch"
else
git branch -D "$branch"
fi
done
}
# remove all worktrees and their dedicated (+) branches
gwda() {
git worktree list --porcelain |
awk '/^worktree / {print $2}' |
grep -v "^$(git rev-parse --show-toplevel)$" |
while read -r wt; do
echo "Removing worktree: $wt"
git worktree remove --force "$wt"
done
git branch |
sed -n 's/^+ //p' |
while read -r br; do
echo "Deleting branch: $br"
git branch -D "$br"
done
}
alias gw="git switch "
alias gw-="git switch -"
alias gwm='git switch $(git_main_branch)'
alias gwd="git switch develop"
alias gwp="git switch production"
alias gwc="git switch -c "
alias gst="git stash"
alias gsta="git stash apply"
alias gstl="git stash list"
alias gstc="git stash clear"
gl() { [[ $1 = "" ]] && git log -15 || git log -$1 ; }
glo() { [[ $1 = "" ]] && git log -15 --oneline || git log -$1 --oneline ; }
alias gft="git fetch --tags"
alias gfp="git fetch --prune"
alias gpt="git push --tags"
alias gpu="git push"
alias gpua="git push --all && git push --tag"
alias gp="git pull"
alias gr="git reflog --date=iso"
alias gaa="git add -A :/"
alias gau="git add -u"
ga() { git add $@; }
gc() {
if [[ -z $@ ]]; then
git commit
else
git commit -m "$@";
fi
}
alias gca="git commit --amend"
# echo the primary integration branch name: main if it exists, else master
git_main_branch() {
if git show-ref --verify --quiet refs/heads/main; then
echo main
elif git show-ref --verify --quiet refs/heads/master; then
echo master
else
echo main
fi
}
# switch to main (or master), whichever exists
gcm() {
B=$(git_main_branch)
echo "Switching to '$B' branch."
git checkout "$B"
}
alias gmm='git merge $(git_main_branch)'
alias gcd="git checkout develop"
alias gmd="git merge develop"
alias gce="git commit --allow-empty -m 'Trigger Build'"
gcf() { git checkout feature/$1; }
gffs() { git flow feature start $1; }
gfff() { git flow feature finish -F $(git_flow_current_branch); }
# Pick up the first "one" feature
gcfo() { git checkout $(git branch | grep feature | tail -1); }
gffo() { git flow feature finish -F $(git branch | grep feature | tail -1 | sed s/feature\/// | sed s/*//); }
# git show only file list
gwf() { git diff-tree --no-commit-id --name-only -r $1; }
gch() { git checkout hotfix/$1; }
gfhs() { git flow hotfix start $1; }
gfhf() { git fetch --tags; git pull origin $(git_main_branch); git flow hotfix finish -F $(git_flow_current_branch); }
gcr() { git checkout release/$1; }
gfrs() { git flow release start $1; }
gfrf() { git flow release finish -F $(git_flow_current_branch); }
git_flow_current_branch(){ git rev-parse --abbrev-ref HEAD | cut -d/ -f 2; }
# copy the latest tag to the clipboard (macOS)
glt() {
LAST_TAG=$(git for-each-ref --format="%(refname:short)" --sort=taggerdate refs/tags | tail -1);
echo $LAST_TAG | xargs echo -n | pbcopy;
echo "LAST TAG: "$LAST_TAG;
}
# delete a tag locally and on origin
gut (){
git tag -d $1;
git push --progress origin :refs/tags/$1;
}
gbd (){
git branch -D $1;
}
gpo (){
git push origin $1;
}
# delete a remote branch
gpod (){
git push origin :$1;
}
# git remote add (defaults remote name to origin)
gra (){
if [[ "$1" == "" ]]; then
REMOTE=origin
else
REMOTE=$1
fi
git remote add $REMOTE $2;
}
# git remote set-url (defaults remote name to origin)
grsu (){
if [[ "$1" == "" ]]; then
REMOTE=origin
else
REMOTE=$1
fi
git remote set-url $REMOTE $2;
}
# get remote url (works on git v1.x too)
grgu (){
if [[ -z "$1" ]]; then
BRANCH=origin
else
BRANCH=$1
fi
git config --get remote.$BRANCH.url
}
alias gitrevert="git checkout -- $1"
alias gc--="git checkout -- $1"
# unstage
alias gitunstage="git reset HEAD $1"
alias gus="git reset HEAD $1"
alias grh="git reset HEAD $1"
alias gitcolor="git config --global color.ui auto"
alias gitfilemode="git config core.fileMode $1"
alias gs-="git switch -"
alias gsm-='git switch $(git_main_branch)'
alias gsd-="git switch develop"
alias gsc-="git switch -c $@"
alias gt="git tag "
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment