DEV Community

Conlin Durbin
Conlin Durbin

Posted on

Your favorite `git` aliases?

What git aliases do you use? Why do you like them?

Top comments (16)

Collapse
 
eljayadobe profile image
Eljay-Adobe • Edited

An alias to show my aliases:

alias.alias config --get-regexp ^alias\.


An alias to make a branch, with a date suffix:

alias.mkbr !f(){ local args="$*"_; local gituser=$(git config user.email); gituser=${gituser%@*}; if [ "$args" = "_" ]; then args=""; fi; args=${gituser:-${USER}}/$args$(date "+%Y-%b-%d"); args=$(echo "$args" | tr ' [A-Z]' '_[a-z]'); git checkout -b "$args"; };f


An alias to delete a branch:

alias.rmbr !f(){ git branch -d "${1}"; git push origin --delete "${1}"; };f


An alias to go to master:

alias.master checkout master


An alias to get coffee:

alias.coffee !f(){ if [ $(date -j +%a) = 'Fri' ]; then echo 'Friday! Time for beer!'; else echo 'Time for a coffee break. Go get a cup of joe.'; fi };f


An alias for a quick status:

alias.st status -s -uno


An alias to do a diff with my configured difftool:

alias.d difftool


An alias to use kdiff3 to display my changes in my branch from master:

alias.d3master difftool --tool kdiff3 --dir-diff origin/master..


A bash shell alias to do a git log of just the branch's first parent:

alias log=git log --pretty=oneline --graph --max-count=$(( $LINES / 2 )) --first-parent --use-mailmap --date=format:%Y-%b-%d\ %H:%M --pretty=format:'\''%C(red)%h%C(reset) -%C(yellow)%d%C(reset) %C(green)%cd %C(bold blue)%aN%C(reset) %s'\'' --abbrev-commit

Collapse
 
moopet profile image
Ben Sinclair • Edited

I'd suggest dropping the -j on your date command. It doesn't need to be there because you're not setting a date, and it's not compatible with GNU date, so it won't work on 99% of servers.

Collapse
 
hozefaj profile image
Hozefa

Here is my list of git alias

# git aliases
alias gc='git checkout'
alias gcm='git commit -m'
alias gs='git status'
alias ga='git add --all'
alias gp='git pull --rebase'
alias gb='git branch -vv'
alias gr='git remote -v'
alias grt='git reset --hard'
alias gpub='git push origin publish'
alias grp='git rebase publish'
alias gdev='git push origin dev'
alias gd='git branch -D'
alias gm='git merge'
alias gmx='git merge -X theirs'
alias gh='git push hozefa'
alias gk='gitk &'
alias gda='git branch | grep -v "develop" | grep -v "release" | xargs git branch -D'
alias gf='git fetch'
alias gl='git log -3'
# cleans all branches locally that have already been merged.
alias gcmb="git branch --merged | grep -Ev '(^\*|develop)' | xargs git branch -d"
Collapse
 
wuz profile image
Conlin Durbin • Edited

I really like these:

get = !git pull --ff-only - lets me run git get to only pull fast forward changes

who = shortlog -n -s --no-merges - this is more of a fun one. Shows a list of who has committed to the project and how many commits they have made

A complicated one

fco="!f() { git branch -a -vv --color=always --format='%(refname)' | sed "s_refs/heads/__" | sed "s_refs/remotes/__" | fzf --query="$@" --height=40% --ansi --tac --color=16 --border | awk '{print $1}' | xargs git co; }; f"

This requires fzf to be installed but allows you to fuzzy find and check out a git branch. Really useful when you don't remember the branch name exactly but have an idea of what it might be.

Collapse
 
ryanmaynard profile image
Ryan Maynard

Here are mine:


alias gb='git branch'
alias gs='git status'
alias ga='git add'
alias gc='git commit'
alias gl='git log'
alias glo='git log --pretty=oneline'
alias glu="git log --pretty=format:'%h %ad | %s%d [%an]' --graph --date=short"
alias glg="git log --decorate --graph --abbrev-commit --date=relative"
alias gh='git checkout'
alias gt='git tag'
alias grs='git reset'
alias grv='git revert'
alias gm='git merge'
alias gd='git diff'
alias gp='git push'
alias gpo='git push origin'
alias gob='git checkout -b'
alias gh='cat .git/HEAD'
alias grgu='git remote get-url'

alias gbb="git for-each-ref --sort=-committerdate refs/heads/ --format='%(HEAD) %(color:yellow)%(refname:short)%(color:reset) - %(color:red)%(objectname:short)%(color:reset) - %(authorname) (%(color:green)%(committerdate:relative)%(color:reset))'"
alias gbbb="git for-each-ref --sort=-committerdate refs/heads/ --format='%(HEAD) %(color:yellow)%(refname:short)%(color:reset) - %(color:red)%(objectname:short)%(color:reset) - %(contents:subject) - %(authorname) (%(color:green)%(committerdate:relative)%(color:reset))'"


Collapse
 
mattiaslundberg profile image
Mattias Lundberg

Shorthand for using git (bash/zsh alias):

alias g=git

Undo the last commit:

undo = reset --soft HEAD^

Get a summary of commits since yesterday (useful during standups):

standup = !"git log --reverse --branches --since=$(if [[ "Mon" == "$(date +%a)" ]]; then echo "friday"; else echo "yesterday"; fi) --author=$(git config --get user.email) --format=format:'%C(cyan) %ad %C(yellow)%h %Creset %s %Cgreen%d' --date=local"

Push to origin:

pp = push origin

Force push to origin:

ppf = push origin --force-with-lease

Pretty log:

lol = log --pretty=format:"%C(yellow)%h\\ %Cblue%G?%Cred%d\\ %Creset%s%Cgreen\\ (%cr)%Cblue\\ [%cn]" --decorate --graph

Collapse
 
wh0am1 profile image
Carlos Aguilar

This is a must have on my .bashrc:

alias gc="git clone"
alias gp="git push"
alias gl="git pull"
alias gf="git fetch"
alias gd="git diff"
alias gco="git checkout"
alias gsb="git status -sb"
alias gba="git branch -a"
alias grv="git remote -v"
alias gaa="git add --all"
alias ga="git add"
alias gcmsg="git commit -m"
alias gm="git merge"
alias gri="git rebase -i"
alias gcount="git shortlog -sn"
alias glol="git log --graph --pretty='%Cred%h%Creset -%C(auto)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset'"
alias glog="git log --oneline --decorate --graph"

These are from the oh-my-zsh git plugin:
github.com/robbyrussell/oh-my-zsh/...

Collapse
 
yechielk profile image
Yechiel Kalmenson • Edited

Not strictly a git alias, but I have a bash alias gclone <repo> that clones the repo and cds into the directory.

gclone() {
    git clone "$1" && cd "$(basename "$1" .git)"
}
Collapse
 
deciduously profile image
Ben Lovy • Edited

This is disgusting and I don't recommend it, but I compile my Linux kernels from source when they are released on my Gentoo system:

alias kernclean='make clean && make distclean && make mrproper'
alias dokern='cd /usr/src/linux && kernclean && make && mount /boot && make install && grub-mkconfig -o /boot/grub/grub.cfg && emerge -avt @module-rebuild && umount /boot && cd -'

Must be run as root, and assums the user has already utilized eselect kernel to chose their desired kernel version. This symlinks the source to usr/src/linux.

This was my "first draft" and now its years later and I've been using it about once a week without touching it. I have a similar shindig for cleanup of old kernel artifacts but I couldn't quite pack it into an alias, it's got its own function.

I keep feeling like i should revisit this whole setup but it...keeps working fine.

Collapse
 
nickytonline profile image
Nick Taylor
Collapse
 
jef profile image
Jef LeCompte

alias gpc='git push --set-upstream origin "$(git-branch-current 2> /dev/null)"'

if you don't have anything, this would suffice alone. i always forget how to push my new branch to remote...