Logo

Command Palette

Search for a command to run...

Blog
PreviousNext

Essential Git Commands Every Developer Should Know

A concise Git cheatsheet covering repository setup, branching, rebasing, stashing, and common workflows.

Repository Setup

git init                        # Initialize repository
git clone <repo-url>            # Clone repository
git remote add origin <url>     # Add remote

Basic File Operations

git status                      # Check status
git add <file>                  # Add specific file
git add .                       # Add all files
git rm <file>                   # Remove file
git rm --cached <file>          # Unstage file

Committing

git commit -m "message"         # Commit staged
git commit -am "message"        # Add & commit
git commit --amend -m "new"     # Amend last commit

Branching

git branch                      # List branches
git branch <name>               # Create branch
git checkout <name>             # Switch branch
git checkout -b <name>          # Create & switch
git switch <name>               # Modern switch
git switch -c <name>            # Modern create & switch
git branch -d <name>            # Delete merged branch
git branch -D <name>            # Force delete

Remote Sync

git fetch                       # Fetch changes
git pull                        # Fetch + merge
git pull origin main            # Pull main
git push                        # Push changes
git push origin <branch>        # Push branch
git push -u origin <branch>     # Set upstream

Merging & Rebasing

git merge <branch>              # Merge branch
git merge --no-ff <branch>      # No fast-forward
git rebase <branch>             # Rebase branch
git rebase -i HEAD~3            # Interactive rebase

History

git log                         # Full log
git log --oneline               # Compact log
git log --graph                 # Graph view
git show <hash>                 # Show commit
git diff                        # Working vs staged
git diff --staged               # Staged vs last

Undoing

git reset <file>                # Unstage file
git reset                       # Unstage all
git checkout -- <file>          # Discard file changes
git reset --soft HEAD~1         # Keep staged
git reset --mixed HEAD~1        # Keep unstaged
git reset --hard HEAD~1         # Discard all
git revert <hash>               # Revert commit

Tagging

git tag v1.0.0                  # Create tag
git tag -a v1.0.0 -m "Release"  # Annotated tag
git push origin v1.0.0          # Push tag
git push origin --tags          # Push all tags

Stashing

git stash                       # Stash changes
git stash push -m "msg"         # Named stash
git stash pop                   # Apply & drop
git stash apply                 # Apply keep
git stash list                  # List stashes
git stash drop stash@{0}        # Drop stash

Config

git config --global user.name "Your Name"
git config --global user.email "you@email.com"
git config --list

Real-World Workflow Example

# 1. Start new feature
git checkout main
git pull origin main
git checkout -b feature/file-upload
 
# 2. Make changes
git add .
git commit -m "Add file upload functionality"
 
# 3. Push and create PR
git push -u origin feature/file-upload
 
# 4. After PR approval, cleanup
git checkout main
git pull origin main
git branch -d feature/file-upload

Pro Tips

  1. Use meaningful commit messages (Conventional Commits)
  2. Commit often (small, focused commits)
  3. Pull before push (always sync with remote)
  4. Use branches (don't work directly on main)
  5. Review before commit (git status, git diff)