Skip to content

Instantly share code, notes, and snippets.

@Guiorgy
Last active April 14, 2026 17:03
Show Gist options
  • Select an option

  • Save Guiorgy/5ccaa26f507c327bc78f7e18d425280f to your computer and use it in GitHub Desktop.

Select an option

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 to initialize a repository and print information that might be useful
git config --global alias.create '!create() { git init "$@" && echo "Root Directory: $(pwd)" && echo "Total Repo Size: $(du -sh . 2>/dev/null | awk "{print \$1}")" && echo ".git Path: $(git rev-parse --git-dir 2>/dev/null)" && echo ".git Directory Size: $(du -sh .git 2>/dev/null | awk "{print \$1}")" && echo "Repo Format Version: $(git config core.repositoryformatversion)" && echo "Object Hashing Format: $(git rev-parse --show-object-format 2>/dev/null || echo "sha1 (pre-v2.31 Git)")" && echo "Initial Branch: $(git symbolic-ref --short HEAD 2>/dev/null || echo "HEAD detached (no commits)")"; }; create'
# Usage: git create
# Alias to get the hashing algorithm used
git config --global alias.hash 'rev-parse --show-object-format'
# Usage: git hash
# 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 %C(bold yellow)%G?%Creset %s"'
# Usage: git slog
# Alias for full pretty logs
git config --global alias.flog '! git log --graph --pretty=format:"Commit: %C(auto)%H%d%Creset%nAuthor: %C(green)%an <%ae>%Creset %C(cyan)%ai%Creset%nCommiter: %C(green)%cn <%ce>%Creset %C(cyan)%ci%Creset%nSignature: %C(bold yellow)%G?%Creset %C(green)%GS%Creset %GK%n%n%w(0,4,4)%s%n%n%-b%n"'
# Usage: git flog
# Alias for listing logs of the commits to be pulled
git config --global alias.plog '! git log HEAD..@{u}'
# Usage: git plog
# Alias for cloning a single branch only
git config --global alias.clone-branch '!cloneBranch() { git clone ${1} --branch ${1-main} --single-branch; }; cloneBranch'
# Usage: git clone-branch <url> <branch-name>
# Alias for cloning the HEAD commit only
git config --global alias.clone-head '!cloneHead() { git clone --depth 1 ${1} --branch ${1-main}; }; cloneHead'
# Usage: git clone-head <url> <branch-name>
# Alias for cloning the HEAD commit of the main branch only for submodules
git config --global alias.clone-submodules '!cloneSubmodules() { git clone --recurse-submodules --shallow-submodules --depth 1 --single-branch; }; cloneSubmodules'
# Usage: git clone-submodules <url>
# 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 amending the last commit while keeping the old commit date
git config --global alias.fixup '!amend() { GIT_COMMITTER_DATE="$(git log -1 --format=%cD)" git commit --amend --no-edit; }; amend'
# Usage: git fixup
# Alias for amending the last commit while updating the commit date
git config --global alias.amend '! git commit --amend --no-edit'
# Usage: git amend
# 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 forced pushing with lease (will fail if remote already has newer commits)
git config --global alias.push-force '!psuhForced() { git push ${1-} --force-with-lease; }; psuhForced'
# Usage: git push-force <remote>
# 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 for fetching GitHub upstream pull requests
git config --global alias.fetch-pr '!fetchPR() { git fetch upstream refs/pull/${1}/head:pr/${1}; }; fetchPR'
# Usage: git fetch-pr <pr-number>
# Alias for pulling GitHub upstream pull requests
git config --global alias.pull-pr '!pullPR() { git pull upstream pull/${1}/head; }; pullPR'
# Usage: git pull-pr <pr-number>
# 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 to sign all commits without modifying dates
git config --global alias.sign-history '!signHistory() { git filter-branch --commit-filter '"'"'export GIT_COMMITTER_DATE="$GIT_COMMITTER_DATE"; export GIT_AUTHOR_DATE="$GIT_AUTHOR_DATE"; git commit-tree -S "$@"'"'"' -- --all; }; signHistory'
# Usage: git sign-history
# 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>
# Alias to get human readable name based on an available ref
git config --global alias.revision '! git describe --long --always --dirty=* --exclude=* --abbrev=40'
# Usage: git revision
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment