Git Cheat Sheet: Essential Commands for Every Developer

Git is the most widely used version control system in the world. Whether you're a solo developer or part of a large team, knowing the essential Git commands saves time and prevents mistakes. This cheat sheet covers every command you'll need for daily development work, from basic operations to advanced workflows.

Bookmark this page as your quick reference. For formatting your Git documentation and README files, try our Markdown Editor.

Setup & Configuration

Before your first commit, configure your identity. These settings are stored globally and attached to every commit you make.

git config --global user.name "Your Name"
git config --global user.email "[email protected]"
git config --global init.defaultBranch main
git config --global core.editor "code --wait"
git config --list # View all settings

Creating Repositories

git init # Initialize new repo in current directory
git init my-project # Create new directory with repo
git clone https://github.com/user/repo.git # Clone remote repo
git clone --depth 1 <url> # Shallow clone (latest commit only)

Basic Workflow Commands

The core Git workflow revolves around staging changes, committing them, and syncing with a remote repository.

git status # Check working tree status
git add file.txt # Stage a specific file
git add . # Stage all changes
git add -p # Stage changes interactively (patch mode)
git commit -m "message" # Commit with message
git commit -am "message" # Stage tracked files + commit
git commit --amend # Modify the last commit

Branching & Merging

Branches let you work on features, fixes, and experiments in isolation. They're lightweight in Git — create them freely.

git branch # List local branches
git branch -a # List all branches (local + remote)
git branch feature-x # Create new branch
git checkout feature-x # Switch to branch
git checkout -b feature-x # Create + switch in one command
git switch feature-x # Modern way to switch (Git 2.23+)
git switch -c feature-x # Modern create + switch
git merge feature-x # Merge branch into current
git merge --no-ff feature-x # Merge with merge commit
git branch -d feature-x # Delete merged branch
git branch -D feature-x # Force delete unmerged branch

Remote Operations

git remote -v # List remotes
git remote add origin <url> # Add remote
git fetch origin # Download remote changes
git pull # Fetch + merge
git pull --rebase # Fetch + rebase (cleaner history)
git push origin main # Push to remote
git push -u origin main # Push + set upstream tracking
git push --force-with-lease # Safe force push

Viewing History

git log # Full commit log
git log --oneline # Compact log
git log --oneline --graph # Visual branch graph
git log -5 # Last 5 commits
git log --author="Name" # Filter by author
git log -p file.txt # Show changes to a file
git diff # Unstaged changes
git diff --staged # Staged changes
git diff main..feature-x # Compare branches
git blame file.txt # Line-by-line author info
git show abc1234 # Show a specific commit

Undoing Changes

Git provides multiple ways to undo changes depending on where you are in the workflow:

git checkout -- file.txt # Discard unstaged changes
git restore file.txt # Modern discard (Git 2.23+)
git restore --staged file.txt # Unstage a file
git reset HEAD~1 # Undo last commit (keep changes)
git reset --hard HEAD~1 # Undo last commit (discard changes)
git revert abc1234 # Create commit that undoes a commit
git clean -fd # Remove untracked files/dirs

Stashing Work

git stash # Stash current changes
git stash -m "description" # Stash with message
git stash list # List all stashes
git stash pop # Apply + remove latest stash
git stash apply stash@{2} # Apply specific stash
git stash drop stash@{0} # Delete a stash

Advanced Commands

git rebase main # Rebase current branch onto main
git rebase -i HEAD~3 # Interactive rebase (squash, reorder)
git cherry-pick abc1234 # Apply specific commit to current branch
git tag v1.0.0 # Create lightweight tag
git tag -a v1.0.0 -m "msg" # Create annotated tag
git bisect start # Binary search for buggy commit
git reflog # View all HEAD movements (recovery tool)

.gitignore Patterns

A .gitignore file tells Git which files to exclude from tracking:

node_modules/ # Ignore directory
*.log # Ignore all .log files
!important.log # Exception — do track this
.env # Ignore environment files
dist/ # Ignore build output
**/*.tmp # Ignore .tmp files in any subdirectory

Use our Markdown Editor to write clean, well-formatted README files for your Git repositories.

Frequently Asked Questions

What is the difference between git pull and git fetch?

git fetch downloads changes from the remote repository without modifying your working directory. git pull does a fetch followed by a merge (or rebase with --rebase). Fetch is safer because it lets you review changes before integrating them.

How do I undo the last git commit?

Use git reset HEAD~1 to undo the last commit while keeping your changes in the working directory. Add --hard to also discard the changes. If the commit was already pushed, use git revert instead to create a new commit that undoes it.

What does git stash do?

Git stash temporarily saves your uncommitted changes so you can switch branches or pull updates without committing half-done work. Use git stash to save and git stash pop to restore. You can have multiple stashes and apply them selectively.

How do I resolve merge conflicts in Git?

When Git can't auto-merge, it marks the conflicting sections in the file with <<<<<<<, =======, and >>>>>>> markers. Edit the file to keep the correct code, remove the markers, then git add the file and git commit. Many editors like VS Code have built-in merge conflict tools.

What is git rebase and when should I use it?

Rebase moves your branch's commits on top of another branch, creating a linear history. Use it to keep feature branches up-to-date with main before merging. Avoid rebasing commits that have been pushed and shared with others, as it rewrites history.

Related Tools

Markdown Editor JSON Formatter