Skip to content

Instantly share code, notes, and snippets.

@LazyRen
Last active November 3, 2023 02:33
Show Gist options
  • Save LazyRen/89e3faaf518c137530d6d80ed5a9773a to your computer and use it in GitHub Desktop.
Save LazyRen/89e3faaf518c137530d6d80ed5a9773a to your computer and use it in GitHub Desktop.
git-related alias.
alias ga="git add"
alias gaa="git add --all
"
alias gb="git branch"
alias gba="git branch -a"
# Delete all local branches except for the current & master
alias gbd="git branch | grep -v '$(git branch --show-current)\|master' | xargs git branch -D"
alias gc="git commit"
alias gca="git commit --amend"
alias gcm="git commit -m"
alias gco="git checkout"
alias gcp="git cherry-pick"
alias gcpa='git cherry-pick --abort'
alias gcpc="git cherry-pick --continue"
alias gr="git rebase"
alias gra="git rebase --abort"
alias grc="git rebase --continue"
alias gd="git diff"
alias gds='git diff --staged'
# Show the diff between latest stash and local working tree:
alias gdst='git diff stash@{0}' # = git stash show -l
# Show the diff between latest stash and HEAD:
alias gdsth='git diff stash@{0} HEAD'
# Show the diff between latest stash and its original parent commit:
alias gdstp='git diff stash@{0}^ stash@{0}' # = git stash show -p
alias glog="git log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr)%C(bold blue)<%an>%Creset' --abbrev-commit"
alias gm="git merge"
alias gp="git push"
alias gpl="git pull"
alias gs="git status"
run_checkbot() {
python $(git rev-parse --show-toplevel)/tools/checkbot/run_checkbot.py --commit HEAD --check CheckClangFormat
python $(git rev-parse --show-toplevel)/tools/checkbot/run_checkbot.py --commit HEAD --check CheckPylint
}
checkbot() {
python $(git rev-parse --show-toplevel)/tools/checkbot/run_checkbot.py
}
# Gerrit
gpg() {
if [ -z "$1" ]; then
BRANCH_NAME="$(git for-each-ref --format='%(upstream:short)' "$(git symbolic-ref -q HEAD)" | cut -d '/' -f2)"
else
BRANCH_NAME="$1"
fi
git push origin HEAD:refs/for/$BRANCH_NAME
}
gpgwip() {
if [ -z "$1" ]; then
BRANCH_NAME="$(git for-each-ref --format='%(upstream:short)' "$(git symbolic-ref -q HEAD)" | cut -d '/' -f2)"
else
BRANCH_NAME="$1"
fi
git push origin HEAD:refs/for/$BRANCH_NAME%wip
}
gpgready() {
if [ -z "$1" ]; then
BRANCH_NAME="$(git for-each-ref --format='%(upstream:short)' "$(git symbolic-ref -q HEAD)" | cut -d '/' -f2)"
else
BRANCH_NAME="$1"
fi
git commit --amend --no-edit
git push origin HEAD:refs/for/$BRANCH_NAME%ready
}
grep_changed() {
git diff-index -U -G \"$@\" HEAD;
}
# Checkout parent/older commit:
function git_checkout_parent() {
git checkout HEAD~$1
return 0
}
# Checkout child/newer commit:
function git_checkout_child() {
children=$(git log --all --ancestry-path ^HEAD --format=format:%H | cat)
if [[ -z $children ]]; then
echo -n 'This commit does not have any children\nHEAD remains at '
git log -1 --oneline | cat
return 1
else
# Take the first child, or the one specified by the input arg:
child=$(echo $children | tail -n "${1:-1}" | head -n 1)
# If the child to checkout is at the branch's tip ...
if [[ "$(echo $children | grep '' -c)" -le "${1:-1}" ]]; then
branch=$(git branch --contains $child | xargs)
# ... and there is only 1 branch with that commit ...
if ! [[ $branch =~ ' ' ]]; then
# ... checkout the branch itself instead of the specific hash:
child=$branch
fi
fi
fi
git checkout $child
return 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment