DTTooleras

Git Commands Cheat Sheet: Every Command You Actually Need

A practical Git reference covering setup, branching, merging, rebasing, stashing, undoing mistakes, and advanced workflows. No fluff — just the commands you use daily.

DevToolsHub Team19 min read1,325 words

Git Configuration

Before anything else, set up your identity:

# Set your name and email (required for commits)
git config --global user.name "Your Name"
git config --global user.email "you@example.com"

# Set default branch name to main
git config --global init.defaultBranch main

# Enable colored output
git config --global color.ui auto

# Set VS Code as default editor
git config --global core.editor "code --wait"

# View all settings
git config --list

# View a specific setting
git config user.name

Starting a Repository

# Initialize a new repository
git init

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

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

# Clone a specific branch
git clone -b develop https://github.com/user/repo.git

# Shallow clone (only latest commit — faster for large repos)
git clone --depth 1 https://github.com/user/repo.git

Daily Workflow

Checking Status

# See what files have changed
git status

# Short format
git status -s

# Show changes in tracked files
git diff

# Show changes staged for commit
git diff --staged

# Show changes in a specific file
git diff path/to/file.js

Staging and Committing

# Stage a specific file
git add file.js

# Stage multiple files
git add file1.js file2.js

# Stage all changes
git add .

# Stage parts of a file interactively
git add -p file.js

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

# Stage all tracked files and commit in one step
git commit -am "Fix login bug"

# Amend the last commit (change message or add files)
git commit --amend -m "Better commit message"

# Amend without changing the message
git commit --amend --no-edit

Viewing History

# View commit log
git log

# Compact one-line format
git log --oneline

# Show a graph of branches
git log --oneline --graph --all

# Show last 5 commits
git log -5

# Show commits by a specific author
git log --author="Alice"

# Show commits that changed a specific file
git log -- path/to/file.js

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

# Show what changed in each commit
git log -p

# Show stats (files changed, insertions, deletions)
git log --stat

Branching

# List all local branches
git branch

# List all branches (including remote)
git branch -a

# Create a new branch
git branch feature/login

# Create and switch to a new branch
git checkout -b feature/login
# Or (newer syntax)
git switch -c feature/login

# Switch to an existing branch
git checkout main
# Or
git switch main

# Rename current branch
git branch -m new-name

# Rename a specific branch
git branch -m old-name new-name

# Delete a branch (safe — won't delete unmerged)
git branch -d feature/login

# Force delete a branch
git branch -D feature/login

# Delete a remote branch
git push origin --delete feature/login

Merging

# Merge a branch into current branch
git merge feature/login

# Merge with a commit message
git merge feature/login -m "Merge login feature"

# Merge without fast-forward (always creates a merge commit)
git merge --no-ff feature/login

# Abort a merge in progress
git merge --abort

# View merge conflicts
git diff --name-only --diff-filter=U

Resolving Merge Conflicts

When a conflict occurs, Git marks the conflicting sections:

<<<<<<< HEAD
Your changes
=======
Their changes
>>>>>>> feature/login
  1. Edit the file to resolve the conflict
  2. Remove the conflict markers
  3. Stage the resolved file: git add file.js
  4. Complete the merge: git commit

Rebasing

Rebasing replays your commits on top of another branch, creating a linear history:

# Rebase current branch onto main
git rebase main

# Interactive rebase (edit, squash, reorder commits)
git rebase -i HEAD~3

# Abort a rebase in progress
git rebase --abort

# Continue after resolving conflicts
git rebase --continue

# Skip a conflicting commit
git rebase --skip

Interactive Rebase Commands

pick   — Keep the commit as-is
reword — Keep the commit but edit the message
edit   — Pause to amend the commit
squash — Combine with previous commit (keep message)
fixup  — Combine with previous commit (discard message)
drop   — Remove the commit entirely

Merge vs Rebase

AspectMergeRebase
HistoryPreserves all historyCreates linear history
Merge commitsYesNo
Safe for shared branchesYesNo (rewrites history)
Best forFeature branches → mainKeeping feature branch up to date

Golden rule: Never rebase commits that have been pushed to a shared branch.

Stashing

Temporarily save uncommitted changes:

# Stash current changes
git stash

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

# Stash including untracked files
git stash -u

# List all stashes
git stash list

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

# Apply and remove from stash list
git stash pop

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

# Drop a specific stash
git stash drop stash@{0}

# Clear all stashes
git stash clear

# Show what's in a stash
git stash show -p stash@{0}

Undoing Things

Unstage Files

# Unstage a file (keep changes in working directory)
git restore --staged file.js
# Or (older syntax)
git reset HEAD file.js

Discard Changes

# Discard changes in a file (DANGEROUS — changes are lost)
git restore file.js
# Or
git checkout -- file.js

# Discard all changes in working directory
git restore .

Undo Commits

# Undo last commit, keep changes staged
git reset --soft HEAD~1

# Undo last commit, keep changes unstaged
git reset HEAD~1
# Or
git reset --mixed HEAD~1

# Undo last commit, discard all changes (DANGEROUS)
git reset --hard HEAD~1

# Create a new commit that undoes a previous commit (safe for shared branches)
git revert abc1234

# Revert without committing immediately
git revert --no-commit abc1234

Recover Lost Commits

# Show all recent HEAD movements (including deleted commits)
git reflog

# Restore a lost commit
git checkout abc1234
# Or create a branch from it
git branch recovered-work abc1234

Remote Repositories

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

# View remotes
git remote -v

# Fetch changes from remote (don't merge)
git fetch origin

# Pull changes (fetch + merge)
git pull origin main

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

# Push to remote
git push origin main

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

# Force push (DANGEROUS — overwrites remote history)
git push --force origin feature/login

# Safer force push (fails if remote has new commits)
git push --force-with-lease origin feature/login

Useful Aliases

Add these to your .gitconfig:

git config --global alias.st "status -s"
git config --global alias.co "checkout"
git config --global alias.br "branch"
git config --global alias.ci "commit"
git config --global alias.lg "log --oneline --graph --all"
git config --global alias.last "log -1 HEAD"
git config --global alias.unstage "restore --staged"
git config --global alias.undo "reset HEAD~1"

.gitignore Patterns

# Dependencies
node_modules/
vendor/
.venv/

# Build output
dist/
build/
.next/
out/

# Environment files
.env
.env.local
.env.*.local

# IDE files
.vscode/
.idea/
*.swp
*.swo

# OS files
.DS_Store
Thumbs.db

# Logs
*.log
npm-debug.log*

# Coverage
coverage/
.nyc_output/

Pro Tips

  1. Write good commit messages — Use imperative mood: "Add feature" not "Added feature"
  2. Commit often, push regularly — Small, focused commits are easier to review and revert
  3. Use branches for everything — Never commit directly to main
  4. Review before committing — Always run git diff --staged before committing
  5. Use .gitignore from the start — Don't commit files that shouldn't be tracked
  6. Learn git reflog — It's your safety net for recovering from mistakes
gitgit commandsgit cheat sheetgit tutorialgit branchgit mergegit rebasegit stash

Related articles

All articles

Practice with free tools

200+ free developer tools that run in your browser.

Browse all tools →