Git
Git CLI Shortcuts & Aliases
Essential Git CLI commands, shortcuts, and recommended aliases for faster daily workflow with version control.
Essential Commands
| Command | Action |
|---|---|
git init | Initialize a new repository |
git clone <url> | Clone a remote repository |
git status | Show working tree status |
git add . | Stage all changes |
git add -p | Stage changes interactively (patch mode) |
git commit -m "msg" | Commit with message |
git commit --amend | Amend last commit |
git push | Push to remote |
git pull | Fetch and merge from remote |
git fetch | Fetch from remote without merging |
git diff | Show unstaged changes |
git diff --staged | Show staged changes |
git diff HEAD~1 | Show changes from last commit |
Branching
| Command | Action |
|---|---|
git branch | List local branches |
git branch -a | List all branches (including remote) |
git branch <name> | Create new branch |
git checkout <branch> | Switch to branch |
git checkout -b <name> | Create and switch to new branch |
git switch <branch> | Switch to branch (modern) |
git switch -c <name> | Create and switch (modern) |
git branch -d <name> | Delete branch (safe) |
git branch -D <name> | Force delete branch |
git merge <branch> | Merge branch into current |
git rebase <branch> | Rebase current onto branch |
git cherry-pick <hash> | Apply specific commit |
History & Inspection
| Command | Action |
|---|---|
git log --oneline | Compact commit history |
git log --graph --oneline | Visual branch graph |
git log -p | Show patches with commits |
git log --author="name" | Filter by author |
git log --since="2 weeks ago" | Filter by date |
git show <hash> | Show commit details |
git blame <file> | Show line-by-line authorship |
git reflog | Show reference log (undo history) |
git shortlog -sn | Contributor summary |
Undoing Changes
| Command | Action |
|---|---|
git restore <file> | Discard working directory changes |
git restore --staged <file> | Unstage a file |
git reset HEAD~1 | Undo last commit (keep changes) |
git reset --hard HEAD~1 | Undo last commit (discard changes) |
git revert <hash> | Create new commit undoing a commit |
git stash | Stash working changes |
git stash pop | Apply and remove last stash |
git stash list | List all stashes |
git stash apply stash@{n} | Apply specific stash |
git stash drop | Remove last stash |
git clean -fd | Remove untracked files and directories |
Remote Operations
| Command | Action |
|---|---|
git remote -v | Show remote URLs |
git remote add <name> <url> | Add remote |
git push -u origin <branch> | Push and set upstream |
git push origin --delete <branch> | Delete remote branch |
git pull --rebase | Pull with rebase |
git fetch --prune | Fetch and remove stale remote branches |
git remote prune origin | Clean up stale remote references |
Recommended Aliases
Add these to ~/.gitconfig under [alias]:
| Alias | Command | Action |
|---|---|---|
git s | status -sb | Short status with branch |
git co | checkout | Checkout shortcut |
git br | branch | Branch shortcut |
git ci | commit | Commit shortcut |
git ca | commit --amend --no-edit | Amend without editing message |
git lg | log --oneline --graph --decorate | Pretty log graph |
git last | log -1 HEAD | Show last commit |
git unstage | reset HEAD -- | Unstage files |
git undo | reset HEAD~1 --mixed | Undo last commit |
git wip | commit -am "WIP" | Quick work-in-progress commit |
git aliases | config --get-regexp alias | List all aliases |
git df | diff --stat | Diff summary |
git conflicts | diff --name-only --diff-filter=U | List conflicted files |
Useful Config Settings
| Command | Action |
|---|---|
git config --global init.defaultBranch main | Set default branch to main |
git config --global pull.rebase true | Default pull to rebase |
git config --global rerere.enabled true | Remember conflict resolutions |
git config --global push.autoSetupRemote true | Auto set upstream on push |
git config --global fetch.prune true | Auto prune on fetch |
git config --global core.autocrlf input | Line ending handling (macOS/Linux) |