The Hook (The "Byte-Sized" Intro)
git log --oneline --graph --all --decorate is 42 characters. With an alias, it becomes git lg. Aliases turn long, hard-to-remember commands into short, memorable ones. Top developers have 10-20 aliases that save them thousands of keystrokes per day. Here are the ones worth setting up.
📖 What are Aliases?
Git aliases are custom shortcuts stored in your config that map short names to longer commands.
Conceptual Clarity
Essential aliases:
| Alias | Expands To | Purpose |
|---|---|---|
st | status -sb | Short status |
co | checkout | Switch branches |
br | branch | List branches |
ci | commit | Create commit |
lg | log --oneline --graph --all --decorate | Pretty graph |
unstage | reset HEAD -- | Unstage files |
last | log -1 HEAD --stat | Last commit |
amend | commit --amend --no-edit | Quick amend |
Alias types:
| Type | Syntax | Example |
|---|---|---|
| Simple | git config --global alias.co checkout | git co |
| With flags | git config --global alias.lg "log --oneline --graph" | git lg |
| Shell command | git config --global alias.cleanup '!git branch --merged | grep -v main | xargs git branch -d' | git cleanup |
Real-Life Analogy
Aliases are keyboard shortcuts for Git. Just like Ctrl+C copies text instead of typing Edit → Copy, git co checks out a branch instead of typing git checkout.
Visual Architecture
Why It Matters
- Speed: Save thousands of keystrokes daily.
- Consistency: Always use the right flags without remembering them.
- Complex commands: Wrap multi-step operations in one alias.
- Personalization: Customize Git to match your workflow.
Code
# ─── Set up essential aliases ───
git config --global alias.st "status -sb"
git config --global alias.co "checkout"
git config --global alias.br "branch"
git config --global alias.ci "commit"
git config --global alias.lg "log --oneline --graph --all --decorate"
git config --global alias.unstage "reset HEAD --"
git config --global alias.last "log -1 HEAD --stat"
git config --global alias.amend "commit --amend --no-edit"
# ─── Shell aliases (prefix with !) ───
git config --global alias.cleanup '!git branch --merged | grep -v main | xargs git branch -d'
# ─── Use them ───
git st # Short status
git lg # Pretty graph
git last # Last commit details
git amend # Quick amend
# ─── List all aliases ───
git config --get-regexp aliasKey Takeaways
- Aliases map short names to long commands — set once, use forever.
- Shell aliases (prefix
!) can run arbitrary shell commands. git config --get-regexp aliaslists all your aliases.- Start with 5-8 essential aliases and add more as patterns emerge.
Interview Prep
-
Q: What are Git aliases and how do you create them? A: Aliases are custom shortcuts stored in Git config. Create them with
git config --global alias.<name> "<command>". For example,alias.co checkoutlets you typegit coinstead ofgit checkout. -
Q: What is the difference between a regular alias and a shell alias? A: Regular aliases expand to Git commands. Shell aliases (prefixed with
!) run arbitrary shell commands, enabling multi-step operations like!git branch --merged | xargs git branch -d. -
Q: How do you list all configured aliases? A:
git config --get-regexp aliasshows all aliases and their expansions.