Skip
Arish's avatar

12. Git Best Practices - Staging and Aliases


Git Best Practices

Let's learn professional tips for staging changes and creating aliases to speed up your workflow.

Staging Best Practices

Stage Files Intentionally

Don't blindly stage everything. Review what you're adding:

bash
1# See what changed
2git status
3
4# See detailed changes
5git diff
6
7# Stage intentionally
8git add specific-file.js

Stage in Logical Groups

Group related changes together:

bash
1# Good: Related changes in one commit
2git add src/auth/login.js src/auth/logout.js
3git commit -m "Add authentication functions"
4
5# Bad: Unrelated changes together
6git add login.js styles.css README.md  # Different concerns!

Interactive Staging

Stage specific parts of a file:

bash
1# Stage interactively
2git add -p filename.js
3
4# Options:
5# y - stage this hunk
6# n - skip this hunk
7# s - split into smaller hunks
8# q - quit

Review Before Committing

bash
1# See what's staged
2git diff --staged
3
4# Or use the shorter form
5git diff --cached

Creating Git Aliases

Aliases let you create shortcuts for common commands.

Basic Aliases

bash
1# Status shortcut
2git config --global alias.st status
3
4# Now you can use:
5git st

Common Aliases

bash
1# Shorthand for common commands
2git config --global alias.co checkout
3git config --global alias.br branch
4git config --global alias.ci commit
5git config --global alias.unstage 'reset HEAD --'
6
7# Usage:
8git co main
9git br feature-branch
10git ci -m "Message"
11git unstage file.txt

Logging Aliases

bash
1# Beautiful log
2git config --global alias.lg "log --oneline --graph --all --decorate"
3
4# Last commit
5git config --global alias.last "log -1 HEAD"
6
7# Show recent activity
8git config --global alias.recent "log --oneline -10"

Useful Workflow Aliases

bash
1# Add all and commit
2git config --global alias.ac '!git add -A && git commit -m'
3# Usage: git ac "My commit message"
4
5# Amend last commit
6git config --global alias.amend 'commit --amend --no-edit'
7
8# Undo last commit (keep changes)
9git config --global alias.undo 'reset HEAD~1 --mixed'
10
11# Show all aliases
12git config --global alias.aliases 'config --get-regexp alias'

View Your Aliases

bash
1# List all aliases
2git config --get-regexp alias
3
4# Or if you created the aliases alias:
5git aliases

Add these to your global Git config:

bash
1# Essential aliases
2git config --global alias.s status
3git config --global alias.a add
4git config --global alias.c commit
5git config --global alias.co checkout
6git config --global alias.br branch
7git config --global alias.p push
8git config --global alias.pl pull
9
10# Enhanced aliases
11git config --global alias.lg "log --oneline --graph --all"
12git config --global alias.ll "log --pretty=format:'%h %ad | %s%d [%an]' --date=short"
13git config --global alias.last "log -1 HEAD --stat"
14git config --global alias.diff-staged "diff --staged"
15
16# Workflow aliases
17git config --global alias.save "stash save"
18git config --global alias.pop "stash pop"
19git config --global alias.wip '!git add -A && git commit -m "WIP"'

Best Practices for Aliases

1. Keep Them Memorable

bash
1# Good - easy to remember
2alias.st = status
3alias.co = checkout
4
5# Bad - hard to remember
6alias.x = status
7alias.y = checkout

2. Document Complex Aliases

For complex aliases, add comments in your .gitconfig:

ini
1[alias]
2    # Show beautiful log with graph
3    lg = log --oneline --graph --all --decorate
4    
5    # Find commits by message
6    find = "!f() { git log --oneline --grep=\"$1\"; }; f"

3. Test Before Adding

bash
1# Test the command first
2git log --oneline --graph --all
3
4# If it works, make it an alias
5git config --global alias.lg "log --oneline --graph --all"

Staging Workflow Example

Here's a professional staging workflow:

bash
1# 1. Check status
2git status
3
4# 2. Review changes
5git diff
6
7# 3. Stage related changes
8git add src/feature/*.js
9
10# 4. Review staged changes
11git diff --staged
12
13# 5. Commit with good message
14git commit -m "Add user profile feature"
15
16# 6. Push to remote
17git push origin main

Summary

Staging Best Practices

  • ✅ Stage files intentionally
  • ✅ Group related changes
  • ✅ Use interactive staging for precision
  • ✅ Review before committing

Alias Benefits

  • ✅ Save time on repetitive commands
  • ✅ Reduce typing errors
  • ✅ Customize Git to your workflow
  • ✅ Create complex command shortcuts

You're now equipped with professional Git practices!