RSS Amplifier

Breder.org · Mar 23, 2025

Bash aliases for git

0
Sign in to vote or save

breder.org

· tools · linux

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^; }

Read next

Hi, I'm Victor -- a Brazilian computer engineer at Microsoft with 7+ years of industry experience architecting and building highly polished software. I enjoy deep technical dives, hard problems, and working across all layers of the stack. My work centers on robust, cloud-based, distributed systems and web-based applications that serve millions of users.

Read the original on breder.org

Comments

Nothing yet. Say the first thing.

    Sign in to join the conversation.