Here are my indispensible git command-line tricks:
gh pr checkout NNNN- Check out a numbered PR to your working treegl3- See the last 3 commitsgco -- Check out the last branch you were ongco branch-name- Check out a local branch by full namegbm DateInput- Shows git branches matching a patterngco $(gbm DateInput)- Check out the DateInput branchglb- "Git last branches" - print a list of the most recent branches you've checked outgro- "Git reset origin" - Does a reset to the remote branch, not merging with local editsgwip- Create a commit instantly, bypassing commit hooks. Append message optionally.gcam- Git commit all changes, with message.gundo- Undo the last commit, keeping working tree the same.gadd- Merge the current changes with the previous commit.gra- Git rebase: abortgrc- Git rebase: continuegrebase main- Rebase this branch on top of latest main
Requirements:
ghcommand line tool (brew install gh)
Aliases:
# gl3
alias gl2="git log -2"
alias gl3="git log -3"
# gco
alias gco="git checkout -q"
# gbm
git_branch_matching() {
# pipefail: a lack of match in grep will fail the pipe
(set -o pipefail; find .git/refs/heads | grep $1 | sed -e 's/\.git\/refs\/heads\///g') ||
git branch -r | grep $1 | sed -e 's/origin\///g'
}
alias gbm=git_branch_matching
# glb
alias glb="git for-each-ref --sort=-committerdate --count=10 --format='%(refname:short)' refs/heads/"
# gro
alias gro='git fetch; git merge-base --is-ancestor HEAD @{upstream} && git pull || git reset --hard @{upstream};'
# gwip
gwip() {
msg=$(echo WIP $1)
git add . && git commit -a -m "${msg}" --no-verify
}
# gcam
alias gcam="git add . && git commit -a -m"
# gundo
alias gundo="git reset HEAD^"
# gadd
alias gadd="git add . ; git commit --no-verify --amend --no-edit"
# rebase
alias gra="git rebase --abort"
alias grc='git rebase --continue'
function grebase {
git fetch; git checkout $1 && git reset --hard @{upstream} && git clean -f && git checkout - && git rebase $1
}