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
Options: --depth 1 (shallow clone), --branch [name] (specific branch)

Basic Workflow

Making Changes

git status
Show changed files in working directory
git add .
Stage all changed files
Alternatives: git add [file] (specific file), git add -p (interactive)
git commit -m "Commit message"
Commit staged changes with a message
Options: -a (stage and commit), --amend (modify last commit)
git restore --staged [file]
Unstage a file while keeping changes

Viewing History

git log
Show commit history
Options: --oneline (compact view), --graph (with branch graph), -p (with changes), -n [number] (limit commits)
git show [commit]
Show changes in a specific commit
git diff
Show unstaged changes
Options: --staged (staged changes), [commit1] [commit2] (between commits)
git blame [file]
Show who changed each line in a file

Branching & Merging

Branches

git branch
List all local branches
Options: -a (all branches), -r (remote branches), -v (with last commit)
git branch [name]
Create a new branch
git checkout [branch]
Switch to a branch
Alternatives: git switch [branch] (newer alternative)
git checkout -b [name]
Create and switch to a new branch
git branch -d [name]
Delete a branch
Options: -D (force delete unmerged branch)

Merging & Rebasing

git merge [branch]
Merge a branch into current branch
Options: --no-ff (always create merge commit), --squash (combine commits)
git rebase [branch]
Reapply commits on top of another branch
Options: -i (interactive rebase), --continue (after resolving conflicts), --abort (cancel rebase)
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
Options: --rebase (rebase instead of merge)
git push [remote] [branch]
Upload local changes to remote
Options: -u (set upstream), --force (force push), --tags (push tags)

Advanced Commands

Undoing Changes

git restore [file]
Discard changes in working directory
git reset [commit]
Reset current branch to a specific commit
Modes: --soft (keep changes staged), --mixed (default, keep changes unstaged), --hard (discard all changes)
git revert [commit]
Create a new commit that undoes a previous commit
git clean -fd
Remove untracked files and directories
Options: -n (dry run), -x (include ignored files)

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