Skip to content

Instantly share code, notes, and snippets.

@sytone
Last active January 10, 2024 16:57
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save sytone/03d9b90abb936830253f60f7f40a1a06 to your computer and use it in GitHub Desktop.
Save sytone/03d9b90abb936830253f60f7f40a1a06 to your computer and use it in GitHub Desktop.
Aliases I have in my global git config
[alias]
# Pull with prune
pp = pull --prune --autostash
# Standard checkout
co = checkout
# Edit the global configuration
ec = config --global -e
# Edit the alias file
ea = "!f() { code \"$HOME\\alias.inc\"; }; f"
# Edit the local repo configuration
ecl = config -e
# Perform a rebase of your local private branch, can refer to other repos as part of the command.
# Example: git up origin main
up = "!f() { CurrentBranch=$(git rev-parse --abbrev-ref HEAD);echo -e \"\\e[32m-- Rebasing $CurrentBranch with $@...\\e[0m\";git pull --rebase --prune $@; git submodule update --init --recursive; }; f"
# Merges the current branch with main by default. You can specify a separate branch by using git m <branch-name>
m = "!f() { CurrentBranch=$(git rev-parse --abbrev-ref HEAD); echo -e \"\\e[32m-- Merging $CurrentBranch with ${1-main}...\\e[0m\";git co ${1-main}; git fetch -p origin; git merge origin/${1-main}; git co $CurrentBranch; git merge ${1-main}; }; f"
# Checkout and create a new branch.
cob = checkout -b
# Create a new branch with static naming and switch to it.
cobm = "!f() { echo -e \"\\e[32m-- Creating new branch ${PERSONALBRANCHROOT-dev}/$USERNAME/$1 and pushing to upstream...\\e[0m\"; git checkout -b ${PERSONALBRANCHROOT-dev}/$USERNAME/$1; git push --set-upstream origin ${PERSONALBRANCHROOT-dev}/$USERNAME/$1; }; f"
# Add unstaged files and commit with message
# Example: git cm "My cool commit"
cm = !git add -A && git commit -m
# Add unstaged files, commit with message and push to remote. If no message is provided it adds 'Lazy commit and push' for you.
# Example: git cmp "My cool commit"
cmp = "!f() { msg=${1-Lazy commit and push}; echo \"Commiting with message: $msg\"; git add -A; git commit -m \"$msg\"; git push; }; f"
# When updating and working on code you may want to pause work and come back to it.
# save allows you to have a save point which undo can pull back out again.
# Example: git save; git up; git undo
save = !git add -A && git commit -m 'SAVEPOINT'
# Commit staged work as a WIP
wip = commit -am "WIP"
# undo the last commit.
undo = reset HEAD~1 --mixed
# amend the last commit and add files staged to it.
amend = commit -a --amend
# Want to start again, this will add you current changes and then reset the HEAD. You can always get back the files you abandoned.
wipe = !git add -A && git commit -qm 'WIPE SAVEPOINT' && git reset HEAD~1 --hard
# To get the WIPE changes back again.
unwipe = "f() { SavePoint=$(git reflog --pretty=format:'%h|%s' | grep 'WIPE SAVEPOINT'); IFS='|'; read -ra HASHES <<< $SavePoint; git checkout $HASHES; }; f"
# Roll back changes to a file.
rollback = checkout HEAD
# This will delete any branches already merged to main to keep you local repo clean.
bclean = "!f() { git checkout ${1-main} && git branch --merged ${1-main} | grep -v " ${1-main}$" | xargs git branch -D; }; f"
# This will delete any branches not avalible remotly. BE CAREFUL
bsuperclean = "!f() { git checkout ${1-main} && git branch --no-merged ${1-main} | grep -v " ${1-main}$" | xargs git branch -D; }; f"
# Run this after your PR is in to switch back to main and cleanup branches.
bdone = "!f() { git checkout ${1-main} && git up && git bclean ${1-main}; }; f"
# pretty format for the git commit history
logg = log --oneline --graph --color --decorate
# This will set the user and email to your personal id
pid = "!f() { \
echo \"Setting User email and name to $GIT_PERSONAL_EMAIL:$GIT_PERSONAL_USERNAME\"; \
git config user.email \"$GIT_PERSONAL_EMAIL\" && git config user.name \"$GIT_PERSONAL_USERNAME\" && git config user.email && git config user.name; \
}; f"
# This will set the user and email to your work id
wid = "!f() { \
echo \"Setting User email and name to $GIT_WORK_EMAIL:$GIT_WORK_USERNAME\"; \
git config user.email \"$GIT_WORK_EMAIL\" && git config user.name \"$GIT_WORK_USERNAME\" && git config user.email && git config user.name; \
}; f"
# Look up a authors details.
whois = "!sh -c 'git log -i -1 --pretty=\"format:%an <%ae>\n\" --author=\"$1\"' -"
# What was the last commit
whatis = show -s --pretty='tformat:%h (%s, %ad)' --date=short
# show a diagram of the commits and branches.
graphviz = "!f() { echo 'digraph git {' ; git log --pretty='format: %h -> { %p }' \"$@\" | sed 's/[0-9a-f][0-9a-f]*/\"&\"/g' ; echo '}'; }; f"
# list all these aliases if you forget what they do.
aliases = !git config --get-regexp 'alias.*' | cut -c 7- | sed 's/[ ]/ = /'
# show the full commit history with color formatting starting from the most recent commit
lg = log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative
# open a browser on windows to the git web site.
open = "!f() { REPO_URL=$(git config remote.origin.url); explorer ${REPO_URL%%.git}; }; f"
browse = !git open
# Shows the amount of changes by file to see what is touched the most.
churn = !git log --all --find-copies --find-renames --name-only --format='format:' "$@" | sort | grep -v '^$' | uniq -c | sort | awk 'BEGIN { print \"count\tfile\" } {print $1 \"\t\" $2}' | sort -g
# shows branches that have been merged to the remote.
remote-merged = "!f() { for branch in $(git branch -r --merged | grep -v HEAD); do echo -e $(git show --format=\"%ci %cr %an\" $branch | head -n 1) \\t$branch; done }; f | sort -r"
# shows branches that have been merged to the remote based on your username environment variable.
remote-merged-me = "!f() { for branch in $(git branch -r --merged | grep -v HEAD); do echo -e $(git show --format=\"%ci %cr %an\" $branch | head -n 1) \\t$branch; done }; f | grep $USERNAME | sort -r"
# shows branches that have been merged to the remote based on your own grep query.
remote-merged-query = "!f() { for branch in $(git branch -r --merged | grep -v HEAD | grep -i \"$1\"); do echo -e $(git show --format=\"%ci %cr %an\" $branch | head -n 1) \\t$branch; done }; f"
# list all the branches on remote and their age. Good for old branch cleanup.
remote-age = "!f() { for ref in $(git for-each-ref --sort=-committerdate --format=\"%(refname:short)\" refs/remotes); do git log -n1 $ref --pretty=format:'%Cgreen%cr%Creset | %C(yellow)%d%Creset | %C(bold cyan)%an%Creset%n'; done }; f"
remote-age-2-months = "!f() { for ref in $(git for-each-ref --sort=-committerdate --format=\"%(refname:short)\" refs/remotes); do git log -n1 $ref --before='2 months ago' --pretty=format:'%Cgreen%cr%Creset | %C(yellow)%d%Creset | %C(bold cyan)%an%Creset%n'; done }; f"
# limit the remote branches to yours only.
remote-age-alias = "!f() { for ref in $(git for-each-ref --sort=-committerdate --format=\"%(refname:short)\" refs/remotes); do git log -n1 $ref --pretty=format:'%Cgreen%cI%Creset | %Cgreen%cr%Creset | %C(yellow)%d%Creset | %Cgreen%an%Creset | %C(bold cyan)%ae%Creset%n'; done }; f"
# provide a more detailed vie of the branching status for the repo.
branch-status = "!f(){ \
echo 'Date | Date Delta | Ref | Name | Email | Ahead | Behind'; \
git for-each-ref --sort=-committerdate --format=\"%(refname:short)\" refs/remotes | while read remote; \
do \
CHANGES=$(git rev-list --left-right ${remote}...origin/main); \
LEFT_AHEAD=$(echo \"$CHANGES\" | grep -c '^<'); \
RIGHT_AHEAD=$(echo \"$CHANGES\" | grep -c '^>'); \
echo \"$(git log -n1 $remote --pretty=format:'%Cgreen%cI%Creset | %Cgreen%cr%Creset | %C(yellow)%d%Creset | %Cgreen%an%Creset | %C(bold cyan)%ae%Creset') | $LEFT_AHEAD | $RIGHT_AHEAD\"; \
done; \
}; f"
# revert the last changed pushed to main. Will screw your world up if other people are pulling.
oh-crap-abort = "!f() { git reset --hard HEAD~1; git push -f ${1-origin} ${2-main}; }; f"
# rename a folder in one action. Cood for changing case.
rename-folder = "!f() { mv $1 $1-tmp; git add -A; git commit -m \"renameaction\"; mv $1-tmp $2; git add -A; git commit --amend -m \"renamed $1 to $2\"; }; f"
# Integration with the Azure CLI devops plugin. https://devblogs.microsoft.com/devops/using-azure-devops-from-the-command-line/
#pr = "!f() { exec 'C:\\Program Files (x86)\\Microsoft SDKs\\Azure\\CLI2\\wbin\\az.cmd' repos pr \"$@\"; }; f"
#repo = "!f() { exec 'C:\\Program Files (x86)\\Microsoft SDKs\\Azure\\CLI2\\wbin\\az.cmd' repos \"$@\"; }; f"
# Native integration with python for the Azure CLI. Run git azurecliinstall first to ensure right bits are in place. Works on windows only.
#azurecliinstall = "!f() { export PYTHONIOENCODING=UTF-8; python -m pip install --disable-pip-version-check --user azure-cli; python -m azure.cli extension add --name azure-devops; }; f"
# Tries to make a new PR using the Azure CLI to get details.
#newpr = "!f() { PR_TITLE=$1; PYTHONIOENCODING=UTF-8 python -m azure.cli repos pr create --open --squash --delete-source-branch --title \"$PR_TITLE\"; }; f"
# Cleanup your repo and remove anything that should not be there.
nuke = !git clean -fdx && git reset HEAD~1 --hard && git pull
# Update the editor to be VS Code
editor-vscode = !git config --global core.editor "code --wait"
editor-vscode-nw = !git config --global core.editor "code --wait --new-window"
# Update the editor to be VS Code
editor-reset = !git config --global --unset core.editor
# General setup for the changes I run with.
setup = !git config pull.rebase false && git editor-vscode
# DESCRIPTION: Clone a GitHub repo using the repo owner and the name of the repo.
# EXAMPLE: git clone-gh scoopinstaller main
clone-gh = "!f() { git clone https://github.com/${1}/${2}.git ${1}/${2}; }; f"
# Example Powershell command called from GIT. Using powershell core.
alias-update = "!f() { exec pwsh -ExecutionPolicy Bypass -NoProfile -Command \"& { \
\\$gitConfig = Get-Content (Join-Path \\$env:userprofile '.gitconfig'); \
\\$aliasIncFile = Join-Path \\$env:userprofile 'alias.inc'; \
if((Test-Path \\$aliasIncFile)) {; \
Write-Host "Alias already installed!"; \
}; \
}\"; }; f"
# Shortcut to call the gitp powershell command in my profile.
p = "!f() { \
export QuietProfile=\"true\"; \
case $1 in \
newpr) \
exec pwsh -ExecutionPolicy Bypass -Command \"& { gitp newpr }\"; \
;; \
open) \
exec pwsh -ExecutionPolicy Bypass -Command \"& { gitp open }\"; \
;; \
help) \
exec pwsh -ExecutionPolicy Bypass -Command \"& { gitp help }\"; \
;; \
*) \
exec pwsh -ExecutionPolicy Bypass -Command \"& { gitp help }\"; \
;; \
esac \
}; f"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment