Git

Git CLI Shortcuts & Aliases

Essential Git CLI commands, shortcuts, and recommended aliases for faster daily workflow with version control.

Essential Commands

CommandAction
git initInitialize a new repository
git clone <url>Clone a remote repository
git statusShow working tree status
git add .Stage all changes
git add -pStage changes interactively (patch mode)
git commit -m "msg"Commit with message
git commit --amendAmend last commit
git pushPush to remote
git pullFetch and merge from remote
git fetchFetch from remote without merging
git diffShow unstaged changes
git diff --stagedShow staged changes
git diff HEAD~1Show changes from last commit

Branching

CommandAction
git branchList local branches
git branch -aList 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

CommandAction
git log --onelineCompact commit history
git log --graph --onelineVisual branch graph
git log -pShow 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 reflogShow reference log (undo history)
git shortlog -snContributor summary

Undoing Changes

CommandAction
git restore <file>Discard working directory changes
git restore --staged <file>Unstage a file
git reset HEAD~1Undo last commit (keep changes)
git reset --hard HEAD~1Undo last commit (discard changes)
git revert <hash>Create new commit undoing a commit
git stashStash working changes
git stash popApply and remove last stash
git stash listList all stashes
git stash apply stash@{n}Apply specific stash
git stash dropRemove last stash
git clean -fdRemove untracked files and directories

Remote Operations

CommandAction
git remote -vShow 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 --rebasePull with rebase
git fetch --pruneFetch and remove stale remote branches
git remote prune originClean up stale remote references

Add these to ~/.gitconfig under [alias]:

AliasCommandAction
git sstatus -sbShort status with branch
git cocheckoutCheckout shortcut
git brbranchBranch shortcut
git cicommitCommit shortcut
git cacommit --amend --no-editAmend without editing message
git lglog --oneline --graph --decoratePretty log graph
git lastlog -1 HEADShow last commit
git unstagereset HEAD --Unstage files
git undoreset HEAD~1 --mixedUndo last commit
git wipcommit -am "WIP"Quick work-in-progress commit
git aliasesconfig --get-regexp aliasList all aliases
git dfdiff --statDiff summary
git conflictsdiff --name-only --diff-filter=UList conflicted files

Useful Config Settings

CommandAction
git config --global init.defaultBranch mainSet default branch to main
git config --global pull.rebase trueDefault pull to rebase
git config --global rerere.enabled trueRemember conflict resolutions
git config --global push.autoSetupRemote trueAuto set upstream on push
git config --global fetch.prune trueAuto prune on fetch
git config --global core.autocrlf inputLine ending handling (macOS/Linux)