Git Cheatsheet
Quick reference guide for common Git commands and workflows
Getting Started
Configuration
git config --global user.name "Your Name"
Set your username globally for all repositories
git config --global user.email "your.email@example.com"
Set your email globally for all repositories
git config --global core.editor "code --wait"
Set VS Code as your default editor
git config --list
View all configuration settings
Creating Repositories
git init
Initialize a new local repository
git clone https://github.com/user/repo.git
Clone an existing repository
Basic Workflow
Making Changes
git status
Show changed files in working directory
git add .
Stage all changed files
git commit -m "Commit message"
Commit staged changes with a message
git restore --staged [file]
Unstage a file while keeping changes
Viewing History
git log
Show commit history
git show [commit]
Show changes in a specific commit
git diff
Show unstaged changes
git blame [file]
Show who changed each line in a file
Branching & Merging
Branches
git branch
List all local branches
git branch [name]
Create a new branch
git checkout [branch]
Switch to a branch
git checkout -b [name]
Create and switch to a new branch
git branch -d [name]
Delete a branch
Merging & Rebasing
git merge [branch]
Merge a branch into current branch
git rebase [branch]
Reapply commits on top of another branch
git mergetool
Launch merge conflict resolution tool
Remote Repositories
Working with Remotes
git remote -v
List all remote repositories
git remote add [name] [url]
Add a new remote repository
git fetch [remote]
Download changes without merging
git pull [remote] [branch]
Fetch and merge remote changes
git push [remote] [branch]
Upload local changes to remote
Advanced Commands
Undoing Changes
git restore [file]
Discard changes in working directory
git reset [commit]
Reset current branch to a specific commit
git revert [commit]
Create a new commit that undoes a previous commit
git clean -fd
Remove untracked files and directories
Tags
git tag
List all tags
git tag [name]
Create a lightweight tag
git tag -a [name] -m "message"
Create an annotated tag
git push [remote] [tagname]
Push a specific tag to remote
git push [remote] --tags
Push all tags to remote
Cleaning Up
git gc
Cleanup unnecessary files and optimize local repository
git prune
Remove unreachable objects
git reflog expire --expire=now --all
Clean up reflog entries
Git Workflows
Make changes in your working directory
Stage changes with
git add
Commit changes with
git commit
Push changes with
git push
Create a new feature branch with
git checkout -b feature-name
Make and commit changes on the feature branch
Switch back to main branch with
git checkout main
Pull latest changes with
git pull origin main
Merge feature branch with
git merge feature-name
Delete the feature branch with
git branch -d feature-name
Main branches:
main
(production) and develop
(integration)
Feature branches:
feature/*
branched from develop
Release branches:
release/*
branched from develop
Hotfix branches:
hotfix/*
branched from main
Merge features to
develop
, releases to both main
and develop
Quick Reference
Common Mistakes
- Forgot to
git add
before commit - Committing to wrong branch
- Force pushing to shared branches
- Not pulling before pushing
- Large commits instead of small, focused ones
Best Practices
- Commit often, perfect later, publish once
- Write clear, concise commit messages
- Keep commits small and focused
- Test before you push
- Use branches for features/bugfixes
Useful Resources
- Pro Git Book: git-scm.com/book
- GitHub Docs: docs.github.com
- Git Documentation: git-scm.com/docs
- Interactive Learning: learngitbranching.js.org
How to use this Git Cheatsheet:
- Browse commands by category (Basic Workflow, Branching, Remotes, etc.)
- Click the copy button next to any command to copy it to your clipboard
- Review the different workflow tabs to understand common Git strategies
- Refer to the quick reference section for best practices and resources
All interactions happen in your browser - we never store or transmit your data