A quick reference for Git โ essential commands for daily development. Bookmark this page.
Setup and Config
# Set identity
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
# Set default branch name
git config --global init.defaultBranch main
# Enable credential caching
git config --global credential.helper cache
# Show current config
git config --listBasic Workflow
# Clone a repository
git clone https://github.com/user/repo.git
git clone --depth 1 https://github.com/user/repo.git # Shallow clone
# Check status
git status
git status -s # Short format
# Stage changes
git add file.txt
git add . # Stage all
git add -p # Interactive staging (patch mode)
# Commit
git commit -m "feat: add new feature"
git commit -am "fix: quick fix" # Stage tracked + commit
git commit --amend # Amend last commit
# Push
git push origin main
git push -u origin feature-branch # Set upstream
git push --force-with-lease # Safe force pushBranching
# List branches
git branch # Local
git branch -r # Remote
git branch -a # All
# Create and switch
git checkout -b feature/new-feature
git switch -c feature/new-feature # Modern syntax
# Switch branches
git checkout main
git switch main # Modern syntax
# Delete branch
git branch -d feature/done # Safe delete (merged only)
git branch -D feature/abandoned # Force delete
git push origin --delete feature/done # Delete remote branchMerging and Rebasing
# Merge branch into current
git merge feature-branch
git merge --no-ff feature-branch # Always create merge commit
# Rebase current onto main
git rebase main
git rebase -i HEAD~5 # Interactive rebase (squash, edit, reorder)
# Abort merge or rebase
git merge --abort
git rebase --abort
# Cherry-pick a commit
git cherry-pick abc1234Stash
# Stash current changes
git stash
git stash -m "work in progress on feature X"
# List stashes
git stash list
# Apply and drop
git stash pop # Apply latest and remove
git stash apply # Apply latest but keep
git stash apply stash@{2} # Apply specific stash
# Drop a stash
git stash drop stash@{0}
git stash clear # Drop allHistory and Diff
# View log
git log --oneline
git log --graph --oneline --all
git log --author="Name" --since="2026-01-01"
git log -p file.txt # Show changes to a file
# Diff
git diff # Unstaged changes
git diff --staged # Staged changes
git diff main..feature # Between branches
git diff HEAD~3..HEAD # Last 3 commits
# Find who changed a line
git blame file.txtUndo and Reset
# Discard unstaged changes
git checkout -- file.txt
git restore file.txt # Modern syntax
# Unstage a file
git reset HEAD file.txt
git restore --staged file.txt # Modern syntax
# Undo last commit (keep changes)
git reset --soft HEAD~1
# Undo last commit (discard changes)
git reset --hard HEAD~1
# Revert a commit (creates new commit)
git revert abc1234Tags
# Create tags
git tag v1.0.0
git tag -a v1.0.0 -m "Release version 1.0.0"
# Push tags
git push origin v1.0.0
git push origin --tags
# List and delete tags
git tag -l "v1.*"
git tag -d v1.0.0
git push origin --delete v1.0.0Tips and Tricks
- Use
git reflogto recover lost commits - Use
.gitignorepatterns:*.log,node_modules/,.env - Set
git config --global pull.rebase truefor cleaner history - Use
git bisectto find the commit that introduced a bug - Use commit message conventions:
feat:,fix:,docs:,chore: