Skip to content

Instantly share code, notes, and snippets.

@deanrad
Last active April 26, 2024 19:36
Show Gist options
  • Save deanrad/01c92d85a75074076f59d1c8807f9775 to your computer and use it in GitHub Desktop.
Save deanrad/01c92d85a75074076f59d1c8807f9775 to your computer and use it in GitHub Desktop.
Git CLI aliases

Here are my indispensible git command-line tricks:

  • gh pr checkout NNNN - Check out a numbered PR to your working tree
  • gl3 - See the last 3 commits
  • gco - - Check out the last branch you were on
  • gco branch-name - Check out a local branch by full name
  • gbm DateInput - Shows git branches matching a pattern
  • gco $(gbm DateInput) - Check out the DateInput branch
  • glb - "Git last branches" - print a list of the most recent branches you've checked out
  • gro - "Git reset origin" - Does a reset to the remote branch, not merging with local edits
  • gwip - 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: abort
  • grc - Git rebase: continue
  • grebase main - Rebase this branch on top of latest main

Requirements:

  • gh command 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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment