Skip to content

Instantly share code, notes, and snippets.

@Guiorgy
Last active February 23, 2024 11:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Guiorgy/5ccaa26f507c327bc78f7e18d425280f to your computer and use it in GitHub Desktop.
Save Guiorgy/5ccaa26f507c327bc78f7e18d425280f to your computer and use it in GitHub Desktop.
Useful git aliases
#!/bin/bash
# Set the default branch name to main
git config --global init.defaultBranch main
# Alias for listing all defined aliases
git config --global alias.alias '! git config --get-regexp ^alias\. | sed -e s/^alias\.// -e s/\ /\ =\ /'
# Usage: git alias
# Alias for short/packed pretty logs
git config --global alias.slog '! git log --graph --pretty=format:"%C(auto)%h%d%Creset %C(cyan)(%ci)%Creset %C(green)%cn <%ce>%Creset %s"'
# Usage: git slog
# Alias for the currently active branch
git config --global alias.active-branch '! git symbolic-ref --short HEAD'
# Usage: git active-branch
# Alias for the currently active branch including its remote
git config --global alias.active-remote-branch '!activeRemoteBranch() { git branch -vv | sed -n '"'"'s/^\\*.*\\[\\(.*\\):.*\\].*$/\\1/p'"'"'; }; activeRemoteBranch'
# Usage: git active-branch
# Alias for creating an empty branch
git config --global alias.checkout-empty '!checkoutEmpty() { git checkout --orphan ${1-empty}; git rm -rf .; git commit --allow-empty -m "${2-initial commit}"; }; checkoutEmpty'
# Usage: git checkout-empty <branch-name> <initial-commit-message>
# Alias for undoing commits
git config --global alias.undo '!undo() { git reset --hard @{${1-1}}; }; undo'
git config --global alias.undo-soft '!undoSoft() { git reset --soft @{${1-1}}; }; undoSoft'
# Usage: git undo <number-of-commits>
# git undo-soft <number-of-commits>
# Alias to rename the default branch to main
git config --global alias.mv-main '!moveMain() { git branch -m ${1-master} main; git push -u origin main }; moveMain'
# Usage: git mv-main <branch-name>
# Alias for removing pushed commits
git config --global alias.undo-push '!undoPush() { git reset --soft @{${1-1}}; git push origin +${2-$(git active-branch)} --force; }; undoPush'
# Usage: git undo-push <number-of-commits> <branch-name>
# Alias for forced pulling (after a rebase)
git config --global alias.pull-force '!pullForced() { git reset ${1-$(git active-remote-branch)} --soft; }; pullForced'
# Usage: git pull-force <branch-name>
# Alias for rebase with the long argument
git config --global alias.rebase-it '! git rebase -i --committer-date-is-author-date'
# Usage: git rebase-it
# Alias to set all commit dates to author dates
git config --global alias.fix-date '! git filter-branch --env-filter '"'"'export GIT_COMMITTER_DATE="$GIT_AUTHOR_DATE"'"'"
# Usage git fix-date
# Alias for removing branches that have already been merged to main by default
git config --global alias.branch-clean '!branchClean() { git branch --merged ${1-main} | grep -v "${1-main}$" | xargs -r git branch -d; }; branchClean'
# Usage: git branch-clean <name-of-branch>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment