Git Commands Cheat Sheet: Essential Commands Every Developer Must Know
Git is non-negotiable in modern software development. Whether you are working solo or on a team of hundreds, knowing Git well separates developers who feel in control from those who panic when something goes wrong.
This cheat sheet covers the commands you will reach for every day β plus the ones that save you when things break.
Configuration
Set your identity before your first commit:
bashgit 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 # Set VS Code as your editor git config --global core.editor "code --wait" # View all config git config --list
Starting a Repository
bash# Initialize a new repo in current directory git init # Clone an existing repo git clone https://github.com/user/repo.git # Clone into a specific folder git clone https://github.com/user/repo.git my-folder
The Basic Workflow
The three-stage process: working directory β staging area β repository.
bash# Check status of working directory and staging area git status # Stage a specific file git add filename.txt # Stage all changed files git add . # Stage parts of a file interactively git add -p filename.txt # Commit staged changes git commit -m "Add user authentication feature" # Stage and commit tracked files in one step git commit -am "Fix typo in README"
Writing good commit messages
code# Bad git commit -m "fix" git commit -m "changes" git commit -m "wip" # Good β imperative mood, specific, under 72 chars git commit -m "Fix null pointer exception in UserService" git commit -m "Add email validation to registration form" git commit -m "Refactor database connection pool settings"
Viewing History
bash# Full log git log # One line per commit git log --oneline # Graph view (great for branches) git log --oneline --graph --all # Log for a specific file git log --oneline -- path/to/file.txt # Show what changed in a commit git show abc1234 # Show changes between commits git diff HEAD~3 HEAD
Branching
bash# List all local branches git branch # List all branches (local + remote) git branch -a # Create a new branch git branch feature/login # Switch to a branch git switch feature/login # Create and switch in one step git switch -c feature/login # Delete a branch (safe β only if merged) git branch -d feature/login # Force delete (even if not merged) git branch -D feature/login # Rename current branch git branch -m new-name
Merging
bash# Merge a branch into the current branch git switch main git merge feature/login # Merge without fast-forward (preserves branch history) git merge --no-ff feature/login # Abort a merge in progress git merge --abort
Resolving merge conflicts
When Git cannot auto-merge, it marks conflicts in the file:
code<<<<<<< HEAD const greeting = "Hello"; ======= const greeting = "Hi there"; >>>>>>> feature/login
Edit the file to keep what you want, then:
bashgit add conflicted-file.js git commit
Rebasing
Rebase replays your commits on top of another branch β keeping history linear.
bash# Rebase current branch onto main git rebase main # Interactive rebase β rewrite, squash, reorder last 3 commits git rebase -i HEAD~3 # Abort a rebase in progress git rebase --abort # Continue after resolving a conflict git rebase --continue
When to merge vs rebase: Use
mergefor shared branches (main, develop) to preserve history. Userebaseon your own feature branches before merging to keep history clean.
Remote Repositories
bash# List remotes git remote -v # Add a remote git remote add origin https://github.com/user/repo.git # Fetch changes (does not merge) git fetch origin # Pull β fetch + merge git pull origin main # Pull with rebase (cleaner than merge) git pull --rebase origin main # Push to remote git push origin feature/login # Push and set upstream tracking git push -u origin feature/login # Delete a remote branch git push origin --delete feature/login
Undoing Changes
This is where Git really saves you.
bash# Discard changes in working directory (unstaged) git restore filename.txt # Discard all unstaged changes git restore . # Unstage a file (keep changes in working directory) git restore --staged filename.txt # Amend the last commit (message or staged changes) git commit --amend -m "Corrected commit message" # Undo last commit, keep changes staged git reset --soft HEAD~1 # Undo last commit, keep changes unstaged git reset --mixed HEAD~1 # Undo last commit, DISCARD changes (destructive) git reset --hard HEAD~1 # Safely undo a commit by creating a new one git revert abc1234
golden rule: Never use
git reset --hardor rewrite history on commits that have been pushed to a shared remote. Usegit revertinstead.
Stashing
Stash lets you save work-in-progress without committing.
bash# Stash current changes git stash # Stash with a description git stash push -m "half-finished login form" # List all stashes git stash list # Apply most recent stash (keep it in stash list) git stash apply # Apply most recent stash and remove from 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
Tags
bash# Create a lightweight tag git tag v1.0.0 # Create an annotated tag (recommended for releases) git tag -a v1.0.0 -m "Initial stable release" # List tags git tag # Push tags to remote git push origin --tags # Delete a tag git tag -d v1.0.0
Searching
bash# Search for a string in all files git grep "TODO" # Find which commit introduced a string git log -S "function getUserById" # Find which commit broke something (binary search) git bisect start git bisect bad # current commit is broken git bisect good v1.2.0 # this version was fine # Git checks out middle commit β you test, then: git bisect good # or git bisect bad # Repeat until Git finds the culprit git bisect reset # when done
Common Interview Questions
Q: What is the difference between git merge and git rebase?
Both integrate changes from one branch into another. merge creates a merge commit preserving the full history. rebase rewrites history by replaying commits on top of the target, creating a linear history.
Q: What is the difference between git fetch and git pull?
git fetch downloads changes from the remote but does not merge them into your working branch. git pull does both β fetch and merge (or rebase if configured).
Q: What is a detached HEAD state?
It happens when you check out a specific commit (not a branch). HEAD points directly to a commit rather than a branch name. Any commits you make are not attached to a branch and can be lost. Fix by creating a branch: git switch -c my-new-branch.
Q: How do you squash multiple commits into one?
bashgit rebase -i HEAD~3 # opens editor showing last 3 commits # Change "pick" to "squash" (or "s") on commits to merge # Save β Git combines them and lets you edit the message
Q: What is git cherry-pick?
It applies a specific commit from another branch onto your current branch:
bashgit cherry-pick abc1234
Useful when you want one fix from a feature branch without merging the whole thing.
Practice Git Questions on Froquiz
Git knowledge is tested in virtually every developer interview. Check out our quizzes on Froquiz β covering version control concepts, workflow patterns, and more.
Summary
- Stage and commit:
git addβgit commit - Branching:
git switch -cto create,git branch -dto delete - Merging:
git mergefor shared branches,git rebasefor personal feature branches - Undoing:
git restorefor files,git revertfor safe undo on shared branches - Stashing:
git stash/git stash popfor temporary saves - Remote:
git fetchto download,git pullto download and merge,git pushto upload