Skip to content

Instantly share code, notes, and snippets.

@jcdarwin
Last active May 31, 2022 07:16
Show Gist options
  • Select an option

  • Save jcdarwin/6e71222f2b04fd612b78 to your computer and use it in GitHub Desktop.

Select an option

Save jcdarwin/6e71222f2b04fd612b78 to your computer and use it in GitHub Desktop.
Use git aliases
################################################################################
# Once the following has been added to the .gitconfig and .extra, we can view
# all our git aliases by either using
#
# gla # List git aliases without comments
#
# galias # List git aliases with comments
#
# Refer to the following for more information:
# https://gist.github.com/mwhite/6887990
# http://durdn.com/blog/2012/11/22/must-have-git-aliases-advanced-examples/
################################################################################
################################################################################
# Add the following to .bash_profile, .extra or whatever gets sourced on
# opening a new terminal session.
################################################################################
### Aliases for git
# An alias to display all git aliases in our .gitconfig
alias galias="cat ~/.gitconfig | sed -n '/alias/,/apply/p'"
# https://gist.github.com/mwhite/6887990
# A function allowing us to call git aliases defined in our .gitconfig by using
# a 'g' prefix, rather than 'git', e.g.
# gla # the short version
# git la # instead of this longer version
# For linux:
# if [ -f /etc/bash_completion ] && ! shopt -oq posix; then
# . /etc/bash_completion
#fi
# For Mac OSX:
# brew install bash-completion
if [ -f $(brew --prefix)/etc/bash_completion ]; then
. $(brew --prefix)/etc/bash_completion
fi
function_exists() {
declare -f -F $1 > /dev/null
return $?
}
for al in `__git_aliases`; do
alias g$al="git $al"
complete_func=_git_$(__git_aliased_command $al)
function_exists $complete_fnc && __git_complete g$al $complete_func
done
################################################################################
# Ensure the following exist in the [alias] section of our ~/.gitconfig
################################################################################
############################################################################
# Aliases where we must specify the 'git' command, e.g. 'git dlc'
############################################################################
# Show a diff of the files modified in the last commit
dlc = diff --cached HEAD^
# Show the commits received by the latest git pull
# e.g. git new HEAD
new = !sh -c 'git log $1@{1}..$1@{0} "$@"'
############################################################################
# Aliases that we can use with a 'g' prefix, e.g. 'ga' => git a => git add .
# by using the bash func described at https://gist.github.com/mwhite/6887990
############################################################################
# Add the nominated file to the staging area¬
a = add
# Add all modified / new / deleted files to the staging area
aa = add .
# Display the branches and the most recent commit hash for each
b = branch -v
# Show files modified in the last commit
dl = "!git ll -1"
# Display all remotes verbosly
r = remote -v
# List out the git config
conf = config -l
# View abbreviated SHA, description, and history graph of the latest 20 commits
l = log --pretty=oneline -n 20 --graph --abbrev-commit
# list aliases
la = "!git config -l | grep alias | cut -c 7-"
# List commits showing changed files
ll = log --pretty=format:"%C(yellow)%h%Cred%d\\ %Creset%s%Cblue\\ [%cn]" --decorate --numstat
# List commits showing relative date / times
ld = log --pretty=format:"%C(yellow)%h\\ %ad%Cred%d\\ %Creset%s%Cblue\\ [%cn]" --decorate --date=relative
# List commits showing the date
lds = log --pretty=format:"%C(yellow)%h\\ %ad%Cred%d\\ %Creset%s%Cblue\\ [%cn]" --decorate --date=short
# View the current working tree status using the long format
s = status
# View the current working tree status using the short format
# s = status -s
# Show the diff between the latest commit and the current state
d = !"git diff-index --quiet HEAD -- || clear; git --no-pager diff --patch-with-stat"
# `git di $number` shows the diff between the state `$number` revisions ago and the current state
di = !"d() { git diff --patch-with-stat HEAD~$1; }; git diff-index --quiet HEAD -- || clear; d"
# Pull in remote changes for the current repository and all its submodules
p = !"git pull; git submodule foreach git pull origin master"
# Clone a repository including all submodules
c = clone --recursive
# Commit with message
cm = commit -m
# Add and commit all changes
ac = !git add . && git commit -am
# Add and commit all changes with a message
ca = !git add -A && git commit -av
# Checkout
co = checkout
# Checkout a particular branch
co = checkout -b
# Switch to a branch, creating it if necessary
go = "!f() { git checkout -b \"$1\" 2> /dev/null || git checkout \"$1\"; }; f"
# Show verbose output about tags, branches or remotes
tags = tag -l
branches = branch -a
remotes = remote -v
# Amend the currently staged files to the latest commit
amend = commit --amend --reuse-message=HEAD
# Credit an author on the latest commit
credit = "!f() { git commit --amend --author \"$1 <$2>\" -C HEAD; }; f"
# Interactive rebase with the given number of latest commits
reb = "!r() { git rebase -i HEAD~$1; }; r"
# Find branches containing commit
fb = "!f() { git branch -a --contains $1; }; f"
# Find tags containing commit
ft = "!f() { git describe --always --contains $1; }; f"
# Find commits by source code
fc = "!f() { git log --pretty=format:'%C(yellow)%h %Cblue%ad %Creset%s%Cgreen [%cn] %Cred%d' --decorate --date=short -S$1; }; f"
# Find commits by commit message
fm = "!f() { git log --pretty=format:'%C(yellow)%h %Cblue%ad %Creset%s%Cgreen [%cn] %Cred%d' --decorate --date=short --grep=$1; }; f"
# Remove branches that have already been merged with master
# a.k.a. ‘delete merged’
dm = "!git branch --merged | grep -v '\\*' | xargs -n 1 git branch -d"
# List contributors with number of commits
contributors = shortlog --summary --numbered
@qmachard
Copy link

Many thanks ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment