Git Aliases

Building on Your Foundation

In the Getting Started section, you learned four essential aliases that form the foundation of efficient Git workflows:

git config --global alias.st status
git config --global alias.ci commit
git config --global alias.br branch
git config --global alias.sw switch

These shortcuts help with basic navigation and commits. Now that you’ve mastered everyday Git operations—viewing changes, managing stashes, and undoing work—it’s time to add aliases that accelerate these common tasks.

Want to learn more about configuration? Check out Git Configuration for a comprehensive guide to how aliases work under the hood and advanced configuration options.


Workflow Aliases for Daily Use

Viewing Changes Efficiently

The diff command shows changes between your working tree and commits. Two aliases cover the most common scenarios:

# Compare working tree to last commit
git config --global alias.df diff

# Compare staged changes to last commit
git config --global alias.dfs 'diff --staged'

Practical application:

# Make changes to files
vim src/auth.py

# Quick check what changed
git df

# Stage changes
git add src/auth.py

# Review what you're about to commit
git dfs

Why these matter: During development, you’ll check diffs dozens of times daily. Reducing git diff --staged to git dfs saves keystrokes and mental overhead, letting you focus on reviewing actual changes rather than command syntax.


Stash Operations

Stashing temporarily shelves work when you need to switch contexts. These aliases streamline the most common stash workflows:

# Stash with descriptive message
git config --global alias.save 'stash push -m'

# Retrieve most recent stash
git config --global alias.pop 'stash pop'

Practical application:

# Mid-feature, urgent bug report arrives
git save "WIP: implementing user authentication"

# Switch to hotfix branch
git sw hotfix/critical-bug

# Fix bug, commit, push...

# Return to feature work
git sw feature/auth
git pop

Why these matter: The standard git stash command creates generic messages like “WIP on feature-branch,” making it difficult to remember what each stash contains. The save alias enforces descriptive messages, turning your stash stack into a readable work-in-progress log.


Quick History Review

Understanding recent changes helps maintain context during development:

# Show last commit details
git config --global alias.last 'log -1 HEAD'

Practical application:

# After committing, verify message and changes
git last

# Output shows:
commit abc1234...
Author: Your Name <[email protected]>
Date:   Mon Oct 28 14:23:45 2024

    feat: add token validation to auth middleware

    diff --git a/src/auth.py b/src/auth.py
    ...

Why this matters: The last alias provides immediate confirmation that your commit captured what you intended. It’s particularly useful after complex staging operations or when you want to reference the commit SHA for other operations.


Undoing Mistakes Safely

The everyday Git section covered various undo operations. This alias addresses the most common scenario—removing files from staging:

# Remove files from staging area
git config --global alias.unstage 'reset HEAD --'

Practical application:

# Accidentally stage wrong file
git add config/secrets.yml  # Oops!

# Remove from staging (keeps working tree changes)
git unstage config/secrets.yml

# File remains modified but not staged
git st
# Output:
 M config/secrets.yml

Why this matters: The command git reset HEAD -- <file> is unintuitive—it sounds destructive but safely removes files from the index. The unstage alias provides semantic clarity: you’re reversing a git add operation, not resetting your work.


Refining Commits

Sometimes you commit too early and realize you need to add one more change:

# Add changes to previous commit without editing message
git config --global alias.amend 'commit --amend --no-edit'

Practical application:

# Commit feature
git ci -m "feat: implement rate limiting"

# Realize you forgot to update a test
vim tests/test_rate_limit.py
git add tests/test_rate_limit.py

# Add to previous commit
git amend

# Previous commit now includes the test

Why this matters: During rapid development, you’ll frequently make small corrections immediately after committing. The amend alias makes this pattern frictionless—no editor opens, no message rewriting, just seamless commit refinement.

Warning: Only amend commits that haven’t been pushed to shared branches. Amending rewrites history and will cause conflicts for collaborators who’ve already pulled your commits.


Viewing and Managing Aliases

List All Configured Aliases

As your alias collection grows, you’ll occasionally forget exact syntax:

# View all aliases
git config --global --get-regexp alias

# Output:
alias.st status
alias.ci commit
alias.br branch
alias.sw switch
alias.df diff
alias.dfs diff --staged
alias.save stash push -m
alias.pop stash pop
alias.last log -1 HEAD
alias.unstage reset HEAD --
alias.amend commit --amend --no-edit

Removing Obsolete Aliases

If an alias no longer serves your workflow:

# Remove specific alias
git config --global --unset alias.name

Direct .gitconfig Editing

For bulk alias management, edit ~/.gitconfig directly:

[alias]
    # Basic shortcuts (from Getting Started)
    st = status
    ci = commit
    br = branch
    sw = switch

    # Viewing changes
    df = diff
    dfs = diff --staged

    # Stash operations
    save = stash push -m
    pop = stash pop

    # History and undoing
    last = log -1 HEAD
    unstage = reset HEAD --
    amend = commit --amend --no-edit

Pro Tip: Organize aliases by category in your .gitconfig using comments. This makes maintenance easier as your collection expands.


Your Growing Toolbox

You now have eleven carefully chosen aliases covering:

  • Basic operations: status, commit, branch, switch
  • Change inspection: diff, staged diff
  • Work management: stash save, stash pop
  • History review: last commit
  • Mistake correction: unstage, amend

These aliases align with everyday Git workflows you’ve already mastered. As you progress to advanced operations—interactive rebasing, cherry-picking, and reflog navigation—you’ll add more powerful aliases in the Advanced section.

Key principle: Aliases amplify efficiency only after you understand the underlying commands. Continue using full commands for new operations until they become second nature, then create shortcuts that match your workflow patterns.


Next Steps: