📚 Git Command Cheatsheet

6 categories, 50+ commands · Visual workflow · 8 common scenarios · Git config best practices

📊 Git Workflow 📁 Basic 🌿 Branch 🌐 Remote ↩️ Undo 📜 History 🔧 Scenarios ⚡ Advanced
📊

Git Four‑Area Workflow

📝 Working Directory
git add git checkout git restore
📌 Staging Area
git commit git reset git restore --staged
💻 Local Repository
git push git pull git fetch
☁️ Remote Repository
git clone git remote
📁

Basic

  • git init
    Initialize a local repository
  • git clone <url>
    Clone a remote repository
  • git add <file>
    Add file to staging
  • git add .
    Add all changes
  • git commit -m "message"
    Commit changes
  • git status
    Show repository status
🌿

Branch

  • git branch
    List local branches
  • git branch <name>
    Create a new branch
  • git checkout <branch>
    Switch branch
  • git checkout -b <branch>
    Create and switch branch
  • git merge <branch>
    Merge a branch
  • git branch -d <branch>
    Delete branch
🌐

Remote

  • git remote -v
    Show remote URLs
  • git remote add origin <url>
    Add a remote
  • git push origin <branch>
    Push to remote
  • git pull origin <branch>
    Pull from remote
  • git fetch origin
    Fetch remote updates
↩️

Undo

  • git restore <file>
    Discard working directory changes
  • git restore --staged <file>
    Unstage a file
  • git reset --soft HEAD^
    Undo commit, keep changes
  • git reset --hard HEAD^
    Undo commit, discard changes
  • git revert <commit>
    Safely undo (new commit)
📜

History

  • git log
    Show commit history
  • git log --oneline
    Compact history
  • git log --graph
    Visualize branch structure
  • git diff
    Show unstaged changes
  • git show <commit>
    Show commit details

Advanced

  • git stash
    Stash working changes
  • git stash pop
    Restore stashed changes
  • git rebase <branch>
    Rebase current branch
  • git cherry-pick <commit>
    Pick a specific commit
  • git bisect
    Binary search for bugs
🔧

8 Common Git Scenarios

😅 Wrong commit message
Just committed but the message is wrong, or you forgot a file.
Solution: # Fix the last commit message git commit --amend -m "new message" # Add a missing file to the last commit git add forgotten-file git commit --amend --no-edit
⏸️ Switch branches mid‑work
Working on a feature but need to switch to another branch urgently.
Solution: # Stash current work git stash save "feature in progress" # Switch to another branch git checkout bugfix # After fixing, return git checkout feature git stash pop
⚠️ Pushed the wrong commit
Accidentally pushed bad code to the remote.
Solution: # Safe way: revert (recommended) git revert HEAD git push origin main # Force overwrite (use with caution) git reset --hard HEAD^ git push --force origin main
🔀 Merge conflict
Conflicts appear when merging branches.
Solution: # See which files conflict git status # After manually resolving git add resolved-file git commit -m "Resolve merge conflict" # Or abort the merge git merge --abort
🔄 Deleted a branch by mistake
Accidentally deleted a branch that wasn't merged.
Solution: # Show all operations git reflog # Find the last commit of the deleted branch git checkout -b recovered-branch commit-id
📦 Squash multiple commits
You have several messy local commits and want one clean commit.
Solution: # Interactive rebase git rebase -i HEAD~3 # In the editor, change pick to squash (or s) # Save and edit the new commit message
🚫 Stop tracking a tracked file
Accidentally committed a config file; now you want Git to ignore it.
Solution: # Remove from Git but keep locally git rm --cached filename # Add to .gitignore echo "filename" >> .gitignore git add .gitignore git commit -m "Stop tracking file"
🔍 Recover lost changes
You ran `git reset --hard` and lost important modifications.
Solution: # View all reference logs git reflog # Find the lost commit id git checkout commit-id # Or create a branch to keep it git branch recovered-branch commit-id
⚙️

Git Global Configuration Quick Reference

Setting Command Description
User name git config --global user.name "Your Name" Set global username
User email git config --global user.email "email@example.com" Set global email
Default editor git config --global core.editor "code --wait" Set VS Code as default editor
Aliases git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.st status
Common command shortcuts
Line endings git config --global core.autocrlf input Handle cross‑platform newlines
List config git config --list Show all configuration
🎯

Git Workflow Best Practices Comparison

🌿 Git Flow

Ideal for large projects with strict releases

  • main – production
  • develop – development
  • feature/* – new features
  • release/* – release preparation
  • hotfix/* – emergency fixes

🚀 GitHub Flow

Simple, continuous deployment

  • main – always deployable
  • feature/* – branches
  • Pull Request – code review
  • Auto‑deploy after merge

📱 GitLab Flow

Environment branches + feature branches

  • main – main branch
  • pre‑production – staging
  • production – production
  • Environment‑specific deployments

📖 Git Command Cheatsheet: From Beginner to Pro

Git is the most popular distributed version control system. Whether you're a solo developer or part of a team, mastering Git is essential. The ng.cc Git command cheatsheet covers initialization, commits, branches, remotes, undo, and history – 6 core categories with 50+ of the most used commands. It also includes a visual workflow diagram and 8 real‑world scenario solutions.

📊 Visual four‑area workflow

See how files move between Working Directory, Staging, Local Repo, and Remote Repo – no more abstract concepts.

🔧 8 high‑frequency scenarios

Fix wrong commit messages, recover deleted branches, resolve conflicts, undo pushes – covers 90% of daily pain points.

⚡ One‑click copy

Every command has a copy button – grab and use instantly.

🎯 Workflow best practices

Compare Git Flow, GitHub Flow, and GitLab Flow to choose the right collaboration model for your team.

🎯 Git Command Categories Explained

📁 Basic

git init creates a repo, git clone copies a remote one, git add stages changes, git commit saves them, git status shows the current state – your daily bread and butter.

🌿 Branch

git branch lists/creates branches, git checkout switches, git merge combines work. Use feature branches to keep main clean.

🌐 Remote

git push uploads commits, git pull downloads and merges, git fetch only downloads. Always pull before pushing to avoid conflicts.

↩️ Undo

git restore (new in 2.23+) discards local changes; git reset undoes commits locally; git revert safely undoes pushed commits. Git can recover almost anything.

❓ Git FAQ

Q1: What's the difference between git pull and git fetch?
git pull = git fetch + git merge. fetch only downloads updates; pull immediately merges them. Safer to fetch first, inspect, then merge manually.
Q2: How can I change a pushed commit message?
If not yet pushed: git commit --amend. If already pushed, amend and then git push --force, but only if you're the only one using that branch. Avoid force‑pushing on shared branches.
Q3: What's the difference between reset, revert, and restore?
  • git reset: moves the HEAD pointer – for local undo.
  • git revert: creates a new commit that undoes an old one – safe for shared history.
  • git restore: discards changes in working directory or staging – doesn't alter history.
Q4: Why doesn't .gitignore work for already tracked files?
.gitignore only affects untracked files. If a file is already tracked, run git rm --cached filename to stop tracking it, then add to .gitignore.
Q5: How to resolve merge conflicts cleanly?
1. git status to see conflicted files.
2. Edit them, looking for <<<<<<<, =======, >>>>>>> markers. Keep the correct parts.
3. Remove the markers, git add the files.
4. git commit to finish. Using a visual tool (VS Code, etc.) makes it easier.
Q6: How does git reflog recover lost commits?
git reflog records every movement of HEAD (resets, rebases, branch deletions). As long as the commit existed locally, you can find its ID and restore it. Entries are kept for 90 days by default.

🔗 Related Tools

This tool is part of the ng.cc developer version control toolkit. You might also need:

⚡ All commands are processed locally in your browser – no data ever uploaded.