Git Aliases and Configuration: Supercharge Your Workflow

Why Git Aliases Matter

Git aliases are custom shortcuts for Git commands. They transform long, complex commands into simple, memorable ones. Combined with proper Git configuration, aliases can save hours of typing and prevent errors. This guide covers everything from basic aliases to advanced scripting.

Core concept: Aliases live in your Git configuration. They can be simple command shortcuts or complex shell scripts. Once configured, they feel like native Git commands.

Git Configuration Levels

Git has three configuration levels:

  • System (--system) – All users on the machine (/etc/gitconfig)
  • Global (--global) – Your user account (~/.gitconfig or ~/.config/git/config)
  • Local (--local) – Current repository (.git/config)
# View all configurations
$ git config --list --show-origin

# Edit global config
$ git config --global --edit
# Or directly
$ git config --global user.name "Your Name"

Essential Git Aliases

Basic Aliases Setup

# Create your first aliases
$ git config --global alias.co checkout
$ git config --global alias.br branch
$ git config --global alias.ci commit
$ git config --global alias.st status
$ git config --global alias.unstage 'reset HEAD --'
$ git config --global alias.last 'log -1 HEAD'

Now you can use:

$ git co main # instead of git checkout main
$ git br # instead of git branch
$ git ci -m "message" # instead of git commit
$ git st # instead of git status

Productivity Aliases by Category

Log and History Aliases

Alias Command Description
git lg log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit Beautiful graph log
git lga log --graph --all --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit Graph log with all branches
git tree log --graph --oneline --decorate --all Simple tree view
# Set up these aliases
$ git config --global alias.lg "log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
$ git config --global alias.tree "log --graph --oneline --decorate --all"

Commit Aliases

Alias Command Description
git ci commit Short for commit
git amend commit --amend --no-edit Amend without editing message
git cim commit -m Commit with message

Branch and Remote Aliases

Alias Command Description
git br branch List branches
git bra branch -a List all branches (including remote)
git brm branch -m Rename branch
git brd branch -d Delete branch
git up remote update Update remotes

Diff and Status Aliases

Alias Command Description
git st status -sb Short status
git df diff Quick diff
git dfc diff --cached Diff staged changes
git who shortlog -s -- Who contributed

Advanced Alias Techniques

Shell Command Aliases

Prefix with ! to run shell commands:

# Open repository in browser
$ git config --global alias.browse '!open https://github.com/$(git config --get remote.origin.url | sed "s/.*://" | sed "s/\.git//")'

# Count lines of code
$ git config --global alias.loc '!git ls-files | xargs wc -l'

# Delete merged branches
$ git config --global alias.cleanup '!git branch --merged | grep -v "\\*\\|main\\|develop" | xargs -n 1 git branch -d'

Complex Aliases with Functions

# Create a branch and switch to it
$ git config --global alias.new '!git checkout -b'

# Better commit with Jira ticket detection
$ git config --global alias.cj '!f() { git commit -m "[JIRA-$(git symbolic-ref --short HEAD | grep -oE "JIRA-[0-9]+")] $1"; }; f'

Git Configuration Best Practices

Core Configuration

# Essential settings
$ git config --global core.editor "code --wait"
$ git config --global core.autocrlf input
$ git config --global core.safecrlf warn
$ git config --global core.ignorecase false
$ git config --global core.preloadindex true

Color Configuration

# Enable colors
$ git config --global color.ui auto
$ git config --global color.status.changed "yellow"
$ git config --global color.status.untracked "red"
$ git config --global color.status.added "green"
$ git config --global color.branch.current "green reverse"
$ git config --global color.diff.meta "cyan"

Merge and Diff Tools

# VS Code as diff/merge tool
$ git config --global diff.tool vscode
$ git config --global difftool.vscode.cmd "code --wait --diff $LOCAL $REMOTE"
$ git config --global merge.tool vscode
$ git config --global mergetool.vscode.cmd "code --wait $MERGED"

Useful Git Configuration Snippets

Auto-correct typos

$ git config --global help.autocorrect 30

Git will wait 0.3 seconds and run the corrected command.

Better push behavior

$ git config --global push.default current
$ git config --global push.autoSetupRemote true

push.default current pushes current branch to same name.
push.autoSetupRemote automatically sets up tracking.

Rebase by default on pull

$ git config --global pull.rebase true
$ git config --global rebase.autoStash true

Complete .gitconfig Example

# ~/.gitconfig
[user]
  name = Your Name
  email = your.email@example.com

[core]
  editor = code --wait
  autocrlf = input
  excludesfile = ~/.gitignore_global

[color]
  ui = auto

[alias]
  co = checkout
  br = branch
  ci = commit
  st = status -sb
  lg = log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit
  tree = log --graph --oneline --decorate --all
  unstage = reset HEAD --
  amend = commit --amend --no-edit
  who = shortlog -s --
  cleanup = !git branch --merged | grep -v "\\*\\|main\\|develop" | xargs -n 1 git branch -d

[push]
  default = current
  autoSetupRemote = true

[pull]
  rebase = true

[help]
  autocorrect = 30

Sharing Aliases with Your Team

Project-specific aliases

Store aliases in the repository:

# .gitconfig in project root
$ git config --local include.path ../.gitconfig

# Then create .gitconfig file with team aliases
[alias]
  deploy = !./scripts/deploy.sh

Alias Management Script

#!/bin/bash
# save-aliases.sh - Backup your aliases
git config --global --get-regexp alias > ~/git-aliases-backup.txt

# restore-aliases.sh
while read -r line; do
  git config --global $line
done < ~/git-aliases-backup.txt

Interactive Aliases with fzf

Combine aliases with fzf for interactive workflows:

# Interactive checkout branch
$ git config --global alias.cob '!git branch -a | grep -v HEAD | fzf | xargs git checkout'

# Interactive delete branch
$ git config --global alias.bd '!git branch | fzf --multi | xargs git branch -d'

Performance Optimization Aliases

# Quick garbage collection
$ git config --global alias.gcnow '!git gc --aggressive --prune=now'

# Repository size
$ git config --global alias.size 'count-objects -vH'

# Find large files
$ git config --global alias.large '!git rev-list --objects --all | git cat-file --batch-check="%(objecttype) %(objectname) %(objectsize) %(rest)" | grep "^blob" | sort -k3 -n -r | head -10'
How do you create a Git alias that runs a shell command?
  • Prefix the command with ! (e.g., alias.name = '!shell command')
  • Use shell: prefix (e.g., alias.name = 'shell:command')
  • Wrap in $() (e.g., alias.name = '$(command)')
  • Use exec: prefix (e.g., alias.name = 'exec:command')

Frequently Asked Questions

Where are Git aliases stored?

Global aliases are stored in ~/.gitconfig (or ~/.config/git/config). Local repository aliases are in .git/config. You can edit these files directly or use git config commands.

Can I use arguments in Git aliases?

Yes! Git automatically appends arguments to the alias. For more control, use shell functions: !f() { git command $1 $2; }; f. This allows argument manipulation and conditional logic.

How do I list all my aliases?

Use git config --get-regexp alias or create an alias: git config --global alias.aliases "config --get-regexp alias". Then just type git aliases.

Can I override existing Git commands with aliases?

Yes, but it's not recommended. If you create an alias named "commit", it will override the built-in commit command. Use unique names to avoid confusion.

How do I share aliases with my team?

Use a shared .gitconfig file in the repository and include it with include.path. Or create an installation script that sets up aliases. For IDE integration, consider using dotfiles repositories.

Previous: Performance Optimization Next: Advanced Rebase