Skip to content

Instantly share code, notes, and snippets.

@rudifa
Created January 5, 2025 10:17
Show Gist options
  • Save rudifa/0197a0d3efacfbfe0792bea48829944a to your computer and use it in GitHub Desktop.
Save rudifa/0197a0d3efacfbfe0792bea48829944a to your computer and use it in GitHub Desktop.
git aliases
[alias]
aliases = config --get-regexp alias
br = branch
### commit
caam = commit -a --amend --no-edit ### commit tracked files squashing onto the last commit
caamn = commit --no-verify -a --amend --no-edit ### --no-verify ignores pre-commit hook
cam = commit -am ### add tracked files and commit with message
camn = commit --no-verify -am
calm = "!f() { git add .; git commit --message=${1}; }; f" ### add all tracked or untracked and commit with message
ci = commit
cin = commit --no-verify
cim = commit -m ### commit staged files with message
cimn = commit --no-verify -m
cia = commit --amend ### amend the last commit message
cian = commit --no-verify --amend
co = checkout
cpk = cherry-pick
diff1 = log -u -1 HEAD ### diff between the last 2 commits
diffp = "diff -- . ':!package-lock.json'" ### diff ignoring the long package-lock.json
fa = "fetch --all"
fab = "!f() { git fetch --all; git branch -a; }; f;" ### fetch all and list branches
### create a local .gitignore from the template, e.g. `git ignore cpp` expects `~/.gitignore_cpp` to exist and copies it to local .gitignore
ignore = "!f() { file="~/.gitignore_${1}"; if [ ! -f "${file}" ]; then echo no such file "${file}"; exit 1; fi; cp "${file}" ./.gitignore; }; f" ### OK
### list last N commits on current branch, e.g. `git lg 5`
lg = "!fn() { local N=""; \
if [[ ${1} =~ "^[0-9]+$" ]]; then N="-$1"; fi; \
git log --graph --pretty=format:\"%C(yellow)%h%Creset%C(bold)%d%Creset%C(cyan)(%ci)%Creset%C(green)(%an)%Creset%s\" $N; }; \
fn"
### list commits - all branches
lga = log --color --graph --all --oneline --decorate --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --
### list commits - formatting variants
lgc = "!fn() { git log --format=\"%h %an %ad %s\" -n 1 $1; }; fn" # print commit details
lgg = log --graph --pretty=format:"%C(yellow)%h%Creset%C(bold)%d%Creset%C(cyan)(%ci)%Creset%C(green)(%cn)%Creset%s"
lgh = log --pretty=format:'%h %ad %an %s' --date=short --
lgo = log --oneline
### list all file blobs
lst = ls-tree -r HEAD ###
rb = rebase
rbi = rebase -i
### squash onto $1 commit
sqc = "!f() { git reset --soft ${1} && git commit --edit -m\"$(git log --format=%s --reverse HEAD..HEAD@{1})\"; }; f"
### squash last $1 commits
sqn = "!f() { git reset --soft HEAD~${1} && git commit --edit -m\"$(git log --format=%s --reverse HEAD..HEAD@{1})\"; }; f"
### git trb <commit> ### tag and rebase on top of the commit
trb = "!f() { bra=$(git rev-parse --abbrev-ref HEAD); git tag -f ${bra}-BAK; git rebase ${1}; }; f"
### git trbi <commit> ### tag and rebase interactively on commit
trbi = "!f() { bra=$(git rev-parse --abbrev-ref HEAD); git tag -f ${bra}_BAK; git rebase -i ${1}; }; f"
### git tsqn <n> ### tag and squash last n commits
tsqn = "!f(){ bra=$(git rev-parse --abbrev-ref HEAD); git tag -f ${bra}_BAK; git reset --soft HEAD~${1} && git commit --edit -m\"$(git log --format=%s --reverse HEAD..HEAD@{1})\"; }; f"
### git tsqc <commit> ### tag and squash on top of the commit
tsqc = "!f(){ bra=$(git rev-parse --abbrev-ref HEAD); git tag -f ${bra}_BAK; git reset --soft ${1} && git commit --edit -m\"$(git log --format=%s --reverse HEAD..HEAD@{1})\"; }; f"
### git tsqc <commit> ### tag and squash on top of the commit --no-verify
tsqcn = "!f(){ bra=$(git rev-parse --abbrev-ref HEAD); git tag -f ${bra}_BAK; git reset --soft ${1} && git commit --no-verify --edit -m\"$(git log --format=%s --reverse HEAD..HEAD@{1})\"; }; f"
rev = remote -v ### list remote repos
rs = restore ### destroy local changes
rss = restore --staged
st = "status -u" ### -u includes untracked files
sw = "switch"
dump = cat-file -p ### dump the git object listed in `git lst`
unstage = reset HEAD -- ### unstage staged (added) files
# sample alias expressions which define a fn() and invoke it immediately, with arguments
example = "!fn() { echo Your arg was ${@}; }; fn" ### IMPORTANT: NO ; AFTER fn
btest = "!f() { sha=$(git rev-parse --short HEAD); git branch -f ${sha}_BAK; }; f"
ttest = "!f() { bra=$(git rev-parse --short HEAD); git tag ${bra}-BAK; }; f"
# note: use \" inside "..."
[core]
editor = "bbedit --wait --resume"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment