Published March 27, 2026 · 16 min read

Git Commands Cheat Sheet for Beginners

Git is the version control system used by virtually every software project on the planet. Whether you are working solo or on a team of hundreds, these are the commands you will use daily. We manage over 225 Git repositories and 16,000+ commits across our projects, so we know which commands matter and which ones are just noise.

This cheat sheet is organized by workflow, not alphabetically. You will learn commands in the order you actually use them.

Table of Contents

  1. Initial Setup
  2. Basic Workflow (Daily Commands)
  3. Branching
  4. Working with Remotes
  5. Undoing Things
  6. Stashing
  7. Inspecting History
  8. Advanced Commands
  9. Useful Aliases
  10. Common Workflows

Initial Setup

Run these once when you first install Git:

git config --global user.name "Your Name"
git config --global user.email "you@example.com"
# Set VS Code as default editor (optional)
git config --global core.editor "code --wait"
# Set default branch name to main
git config --global init.defaultBranch main

Starting a New Project

# Create a new repository
git init

# Clone an existing repository
git clone https://github.com/user/repo.git

# Clone into a specific folder
git clone https://github.com/user/repo.git my-folder

Basic Workflow (The Commands You Use 90% of the Time)

This is your daily loop: check status, stage changes, commit, push.

# See what has changed
git status

# See the actual changes (diff)
git diff

# Stage specific files
git add filename.js

# Stage all changes
git add .

# Commit with a message
git commit -m "Add user authentication feature"

# Push to remote
git push

Writing Good Commit Messages

Your commit messages should explain why you made a change, not what you changed (the diff shows that). Good: "Fix login timeout for users with slow connections." Bad: "Updated auth.js."

The conventional format is:

type(scope): description

feat: Add dark mode toggle
fix: Resolve 404 on user profile page
docs: Update API documentation
refactor: Simplify payment processing logic
test: Add unit tests for cart service

Branching

Branches let you work on features without affecting the main codebase. This is the core of collaborative Git workflows.

# List all branches
git branch

# Create a new branch
git branch feature-login

# Switch to a branch
git checkout feature-login
# Or the newer way:
git switch feature-login

# Create AND switch in one command
git checkout -b feature-login
# Or:
git switch -c feature-login

# Delete a branch (after merging)
git branch -d feature-login

# Force delete (even if not merged)
git branch -D feature-login

Merging

# Merge a branch into your current branch
git merge feature-login

# Merge with a commit message (no fast-forward)
git merge --no-ff feature-login

Rebasing

Rebasing replays your commits on top of another branch, creating a cleaner history. Use it to keep feature branches up to date with main.

# Rebase current branch onto main
git rebase main

# If conflicts occur, resolve them then:
git rebase --continue

# Abort a rebase if things go wrong
git rebase --abort

Rule of thumb: Never rebase branches that others have pulled. Rebase your own feature branches. Merge shared branches.

Working with Remotes

# Show remote repositories
git remote -v

# Add a remote
git remote add origin https://github.com/user/repo.git

# Fetch changes from remote (does not merge)
git fetch

# Pull changes (fetch + merge)
git pull

# Pull with rebase instead of merge
git pull --rebase

# Push and set upstream tracking
git push -u origin feature-login

# Push all branches
git push --all

Undoing Things

Everyone makes mistakes. Here is how to fix them without panicking.

# Unstage a file (keep changes)
git restore --staged filename.js

# Discard changes in a file
git restore filename.js

# Undo the last commit (keep changes staged)
git reset --soft HEAD~1

# Undo the last commit (keep changes unstaged)
git reset HEAD~1

# Completely undo the last commit (discard changes)
git reset --hard HEAD~1

# Create a new commit that undoes a previous commit
git revert abc1234

# Amend the last commit message
git commit --amend -m "New message"

Important: git reset --hard permanently deletes changes. Use --soft or --mixed (default) to keep your work. git revert is safer for shared branches because it does not rewrite history.

Stashing

Stashing saves your uncommitted changes so you can switch branches without committing half-finished work.

# Stash current changes
git stash

# Stash with a description
git stash push -m "Work in progress on login form"

# List all stashes
git stash list

# Apply the most recent stash (keep it in stash list)
git stash apply

# Apply and remove from stash list
git stash pop

# Apply a specific stash
git stash apply stash@{2}

# Delete all stashes
git stash clear

Inspecting History

# View commit history
git log

# Compact one-line format
git log --oneline

# With graph visualization
git log --oneline --graph --all

# Show changes in a specific commit
git show abc1234

# See who changed each line of a file
git blame filename.js

# Search commit messages
git log --grep="login"

# See commits that changed a specific file
git log -- filename.js

Advanced Commands

Cherry-Pick

Apply a specific commit from another branch to your current branch.

git cherry-pick abc1234

Bisect

Binary search through commits to find which one introduced a bug.

git bisect start
git bisect bad # Current commit is broken
git bisect good abc1234 # This commit was working
# Git checks out middle commits. Test each one:
git bisect good # or git bisect bad
# When done:
git bisect reset

Clean

Remove untracked files from the working directory.

# Preview what would be deleted
git clean -n

# Delete untracked files
git clean -f

# Delete untracked files and directories
git clean -fd

Useful Aliases

Save time by shortening commands you use frequently:

git config --global alias.s "status"
git config --global alias.co "checkout"
git config --global alias.br "branch"
git config --global alias.cm "commit -m"
git config --global alias.lg "log --oneline --graph --all"
git config --global alias.last "log -1 HEAD"
git config --global alias.unstage "restore --staged"

Now git s shows status, git lg shows a visual log, and git cm "message" commits with a message.

Common Workflows

Feature Branch Workflow

git checkout main
git pull
git checkout -b feature/user-profiles
# ... make changes ...
git add .
git commit -m "feat: Add user profile page"
git push -u origin feature/user-profiles
# Create pull request on GitHub
# After review and merge, clean up:
git checkout main
git pull
git branch -d feature/user-profiles

Hotfix Workflow

git checkout main
git pull
git checkout -b hotfix/fix-login-crash
# ... fix the bug ...
git add .
git commit -m "fix: Resolve crash on login with empty email"
git push -u origin hotfix/fix-login-crash
# Create PR, merge immediately

Quick Summary Table

TaskCommand
Create repogit init
Clone repogit clone URL
Check statusgit status
Stage filesgit add .
Commitgit commit -m "msg"
Pushgit push
Pullgit pull
New branchgit checkout -b name
Mergegit merge branch
Undo commitgit reset --soft HEAD~1
Stashgit stash
View loggit log --oneline

Level Up Your Dev Workflow

650+ free developer tools including code formatters, diff viewers, and project scaffolders. No signup.

Browse Free Tools Side Hustle Guide

Related Resources