AskDB
·7 min read

Git Commands Cheat Sheet

Git is the most widely used version control system. This cheat sheet covers the commands you use daily as a developer.

Setup

git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git init
git clone <url>

Basic Workflow

git status              # Show changed files
git add .               # Stage all changes
git add file.txt        # Stage specific file
git commit -m "message" # Commit staged changes
git push                # Push to remote
git pull                # Pull from remote

Branching

git branch                  # List branches
git branch feature-x        # Create branch
git checkout feature-x      # Switch to branch
git checkout -b feature-x   # Create and switch
git branch -d feature-x     # Delete branch
git push -u origin feature-x # Push new branch

Merging and Rebasing

git merge feature-x         # Merge branch into current
git rebase main             # Rebase current onto main
git merge --abort           # Abort merge
git rebase --abort          # Abort rebase

Undoing Changes

git restore file.txt        # Discard unstaged changes
git restore --staged file   # Unstage file
git reset HEAD~1            # Undo last commit (keep changes)
git reset --hard HEAD~1     # Undo last commit (discard changes)
git revert <commit-hash>    # Create new commit that undoes changes

Stashing

git stash                   # Stash changes
git stash list              # List stashes
git stash pop               # Apply and remove latest stash
git stash apply             # Apply without removing
git stash drop              # Delete latest stash

Viewing History

git log                     # Full log
git log --oneline           # Compact log
git log --graph --oneline   # Visual branch graph
git diff                    # Unstaged changes
git diff --staged           # Staged changes
git show <commit>           # Show specific commit

Remote Operations

git remote -v               # List remotes
git remote add origin <url> # Add remote
git fetch                   # Fetch without merging
git push origin --delete branch # Delete remote branch

Useful Aliases

git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.st status
git config --global alias.lg "log --oneline --graph"

Tips

  • Commit often, push regularly
  • Use meaningful commit messages
  • Pull before pushing to avoid conflicts
  • Use branches for features and fixes
  • Never force push to shared branches