Breder.org

Bash aliases for git

As a software developer, one of the activities one does multiple times throughout a working day is interacting with the git version control system.

The most common commands can be abbreviated by using shell alias support, increasing agility and preventing typos.

Below are the aliases that I employ for my use cases.

#!/bin/bash
get-git-main() {
	IS_MAIN=$(git branch --list 'ma*'| grep 'main')
	if [ -n "$IS_MAIN" ]; then
		echo "main"
	else
		echo "master"
	fi
}

# cloning
gclone() { git clone --depth=10 $*; }

# status
gs() { git status -sb $*; }
gd() { git diff --word-diff=color $*; }
gds() { git diff --staged --word-diff=color $*; }
gdc() { git diff HEAD^ $*; }
gl() { git log --oneline --stat $*; }

# checkout/branching
gb() { git branch --all $*; }
gsw() { git switch $*; }
gnb() { git switch -c "$1"; }
gcom() { git switch "$(get-git-main)" $*; }
gmerge() { git merge $*; }

# committing
ga() { git add $*; }
gr() { git restore $*; }
grs() { git restore --staged $*; }
gcm() { git commit -m "$1"; }
gca() { git commit --amend --no-edit; }

# pull/push
gfom() { git fetch origin "$(get-git-main)" $*; }
gpom() { git pull origin "$(get-git-main)" $*; }
gp() { git push $*; }
gpf() { git push --force-with-lease $*; }
gpp() { git pull origin "$(get-git-main)" && git push; }

# stash
gst() { git stash $*; }
gstp() { git stash pop $*; }
gstl() { git stash list $*; }

# attention: can be destructive to uncommitted changes
gc() { git checkout $*; }
gclean() { git clean -fdx; }
gundo() { git reset --hard HEAD^; }