Skip to content

Instantly share code, notes, and snippets.

@Adam--
Last active March 20, 2025 14:04
Show Gist options
  • Save Adam--/924c023392703df2e7e143dd2f10925d to your computer and use it in GitHub Desktop.
Save Adam--/924c023392703df2e7e143dd2f10925d to your computer and use it in GitHub Desktop.
Git config
[alias]
# Lists all aliases
alias = config --get-regexp ^alias\\.
# Quote a command to allow it to be used as a git alias
quote-string = "!read -r l; printf \\\"!; printf %s \"$l\" | sed 's/\\([\\\"]\\)/\\\\\\1/g'; printf \" #\\\"\\n\" #"
# Gets information about a repo
url = config remote.origin.url
branch-name = rev-parse --abbrev-ref HEAD
# Pretty log with graph
l = !git status && git log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit -15
graph = l --all --first-parent -30
# Publishes a branch by pushing it to the origin
publish = !git push -u origin $(git branch-name)
# Restores a deleted file using the last committed version
restore = !git checkout $(git rev-list -n 1 HEAD -- "$1")^ -- "$1"
# Squashes a given number of commits starting at HEAD
squash = "!f(){ git reset --soft HEAD~${1} && git commit --edit -m\"$(git log --format=%B --reverse HEAD..HEAD@{1})\"; };f"
# Moves a local repo url to a new url
move = "!f() { originalUrl=$(git url);echo "Moving repo from $originalUrl to $1";git remote set-url origin $1; };f"
# Pretty log of incoming commits
incoming = !git fetch && git l ..origin/$(git branch-name)
# Force push with lease
pushf = push --force-with-lease
# Cleans up after finishing a git-flow branch
cleanup-flow = !git remote prune origin && git checkout develop && git pull --ff-only && git branch -d $1
# Cleans up after merging a PR for a branch
cleanup = "!f() { branchName=${1-$(git branch-name)}; echo "Cleaning up $branchName"; git remote prune origin; git checkout master; git pull --ff-only; git branch -d $branchName; git l; }; f"
# Deletes local branches that have already been merged with master
delete-merged-branches = "!git branch --merged | egrep -v \"(^\\*|master|develop)\" | xargs git branch -d #"
# Deletes local branches that have a missing remote, defaults to a -d, pass -D to force delete
delete-branches-missing-remote = "!git branch -vv | grep ': gone]'| grep -v \"\\*\" | awk '{ print $1 }' | xargs -r git branch ${1--d} #"
# Check if rebased and merged with master
check-rebase-merged = "!f() { branchName=${1-$(git branch-name)}; git fetch; git log --oneline --cherry origin/master...$branchName; };f"
# Quickly ammend the previous commit
amend = commit -a --amend --no-edit
# List branches, sorted by the last commit date, displaying the branch name, relative commit date,
# commit message, and author name
recent = "!f() { case \"$1\" in -a) refs='refs' ;; -r) refs='refs/remotes' ;; -l|'') refs='refs/heads' ;; *) echo 'Invalid option'; return 1 ;; esac; git for-each-ref --sort=-committerdate $refs --format='%(refname:short)|%(color:bold green)%(committerdate:relative)|%(color:magenta)%(authorname)%(color:reset)|%(contents:subject)' --color=always | awk -F'|' 'BEGIN{OFS=\"|\"}{ if ($1 ~ /^origin\\//) { $1 = \"\\033[33m\" $1 \"\\033[0m\" } else { $1 = \"\\033[37m\" $1 \"\\033[0m\" } print $0 }' | column -ts'|'; }; f"
# Recursively clean submodules. Must pass in clean options e.g. -fxd
clean-submodules = submodule foreach --recursive git clean
# Force full clean
clean-all-force = !git clean -ffdx && git submodule foreach --recursive git clean -ffdx
[core]
# Use micro as the default editor
# editor = micro
# Use VS Code as the default editor
editor = "code.cmd --wait"
excludesfile =
longpaths = true
[pull]
# Rebase instead of merging when pulling
rebase = true
[help]
autocorrect = 1
[rebase]
autosquash = true
instructionFormat = %s (%cr) <%an>
[rerere]
enabled = true
[push]
autoSetupRemote = true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment