Git Cheatsheet
Remotes
Use this Git reference while you build software engineering projects, review code for technical interview prep, or polish examples for a software engineer resume.
Viewing Remotes
git remote # List remote names git remote -v # List remotes with fetch/push URLs git remote show <name> # Detailed info: branches, tracking, stale refs git remote show origin # Inspect the "origin" remote
Adding and Removing Remotes
git remote add <name> <url> # Add a remote git remote add origin https://github.com/user/repo.git git remote remove <name> # Remove a remote and its tracking branches git remote rm <name> # Alias for remove git remote rename <old> <new> # Rename a remote
Fetching
Downloads objects and refs — does NOT modify your working tree or local branches:
git fetch # Fetch from all tracked remotes git fetch <remote> # Fetch from a specific remote git fetch origin # Fetch from "origin" git fetch origin <branch> # Fetch a specific branch from origin git fetch --all # Fetch from every remote git fetch --prune # Remove stale remote-tracking refs git fetch --tags # Fetch all tags git fetch --no-tags # Don't auto-fetch tags git fetch --depth=1 origin main # Shallow fetch (latest only) git fetch origin main:main # Fetch and update local main directly
Pulling
Fetch + merge (or rebase) in one step:
git pull # Pull from tracked remote/branch git pull origin main # Pull from origin's main branch git pull --rebase # Rebase local commits on top of fetched git pull --rebase=interactive # Interactive rebase on pull git pull --ff-only # Fast-forward only; abort if not possible git pull --no-ff # Always create a merge commit git pull --squash # Squash incoming commits git pull --all # Pull from all remotes (rarely used) git pull --depth=1 # Deepen a shallow clone by 1
Pushing
git push # Push to tracked upstream git push origin <branch> # Push to origin/<branch> git push -u origin <branch> # Push and set upstream tracking git push --all origin # Push all local branches to origin git push --tags origin # Push all tags to origin git push --follow-tags # Push commits + only annotated tags reachable from them git push origin --delete <branch> # Delete a remote branch git push origin :<branch> # Older syntax to delete remote branch git push --force origin <branch> # Force push (DANGEROUS — rewrites remote history) git push --force-with-lease # Force push only if remote hasn't changed (safer) git push --force-with-lease=<branch>:<sha> # Force push only if remote is at <sha> git push --dry-run # Preview what would be pushed without doing it
Prefer
--force-with-leaseover--force. It prevents overwriting others' work.
Remote-Tracking Branches
Remote-tracking branches are local read-only references (origin/main, origin/feature). They update on fetch.
git branch -r # List all remote-tracking branches git checkout -b local origin/remote # Create local branch tracking remote git switch -c local origin/remote # Modern equivalent git log origin/main # View history of the remote's main git diff HEAD origin/main # Diff local HEAD vs remote main
Tracking Configuration
git branch -u origin/<branch> # Set upstream for current branch git branch --set-upstream-to=origin/main # Verbose form git branch --unset-upstream # Remove upstream git push -u origin <branch> # Set upstream on push
Working with Multiple Remotes
# Common in fork-based workflows git remote add upstream https://github.com/original/repo.git git fetch upstream git merge upstream/main # Sync your main with upstream git rebase upstream/main # Or rebase on upstream
Changing Remote URLs
git remote set-url origin <new-url> # Change fetch+push URL git remote set-url --push origin <url> # Change push URL only git remote set-url origin git@github.com:user/repo.git # Switch to SSH
Pruning Stale References
git remote prune origin # Delete stale remote-tracking refs git fetch --prune # Prune during fetch git fetch -p # Shorthand for --prune # Auto-prune on every fetch: git config --global fetch.prune true
Cloning a Remote
git clone <url> # Full clone git clone --depth 1 <url> # Shallow (latest commit only) git clone --branch <tag/branch> <url> # Clone specific branch or tag git clone --single-branch --branch main <url> # Only fetch main branch git clone --mirror <url> # Bare clone including all refs (backup) git clone --recurse-submodules <url> # Include submodules
Common URL Formats
| Protocol | Example |
|---|---|
| HTTPS | https://github.com/user/repo.git |
| SSH | git@github.com:user/repo.git |
| SSH (full) | ssh://git@github.com/user/repo.git |
| Local path | /home/user/repos/project.git |
| Local file | file:///home/user/repos/project.git |
Gotchas
git pull=git fetch+git merge(orgit rebaseifpull.rebase=true). Prefer doing them separately for more control.
After renaming a remote branch, update your local tracking:
git branch -u origin/<new> <local>.
git push --forceon a shared branch rewrites history for everyone. Always coordinate with the team or use--force-with-lease.