Git Commands Every Developer Should Know
3 min read · Published
Table of contents
Git tracks changes to files so you can collaborate, experiment safely and go back in time. You can get far with about twenty commands. This guide groups them by task and explains how to recover when something goes wrong.
Mental model
Files move through three areas:
- Working directory: the files you edit.
- Staging area (index): what will go into the next commit.
- Repository: committed history.
git add moves changes from 1 to 2; git commit moves them from 2 to 3.
Setup
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git config --global init.defaultBranch main
Starting and cloning
git init # new repo in the current folder
git clone https://github.com/user/repo.git
The everyday loop
git status # what changed?
git diff # unstaged changes
git add file.js # stage one file (or: git add -p to pick hunks)
git commit -m "Fix login redirect"
git log --oneline --graph --decorate -10
Write commit messages in the imperative ("Add search"), with a short first line under about 50 characters. To compare two blocks of text outside Git, see the Text Diff Checker.
Branching
git branch # list branches
git switch -c feature/search # create and switch
git switch main
git branch -d feature/search # delete after merging
Syncing with a remote
git fetch # download, do not change your branch
git pull # fetch + merge (or --rebase)
git push -u origin feature/search # first push of a branch
git push
Merge versus rebase
git switch feature
git merge main # adds a merge commit; history is preserved
git switch feature
git rebase main # replays your commits on top of main; linear history
Use merge on shared branches. Rebase only commits that nobody else has pulled, because it rewrites history.
Stashing work in progress
git stash push -m "wip search"
git switch other-branch
git stash list
git stash pop # reapply and drop
Undoing things safely
| Situation | Command |
|---|---|
| Discard changes in a file | git restore file.js |
| Unstage a file | git restore --staged file.js |
| Fix the last commit message | git commit --amend |
| Undo last commit, keep changes staged | git reset --soft HEAD~1 |
| Undo last commit, keep changes unstaged | git reset HEAD~1 |
| Undo a pushed commit | git revert <hash> (creates a new commit) |
| Throw away everything uncommitted | git reset --hard (destructive!) |
git revert is the safe choice for anything already pushed; reset --hard and force-push can permanently destroy work.
Finding things
git log -S"parseToken" --oneline # commits that added/removed that string
git blame file.js # who last changed each line
git grep "TODO" # search tracked files
git bisect start # binary-search for the commit that broke things
Patterns for git grep -E use regular expressions; see Regular Expressions for Beginners.
The reflog: your safety net
Even after a bad reset, commits survive for a while:
git reflog # history of where HEAD has been
git reset --hard HEAD@{2} # go back to an earlier state
Ignoring files
Add patterns to .gitignore:
node_modules/
.env
dist/
*.log
Never commit secrets. If you do, rotating the credential is mandatory, since history keeps them.
Handling merge conflicts
When Git cannot combine changes, it marks the file:
<<<<<<< HEAD
const title = "Home";
=======
const title = "Welcome";
>>>>>>> feature
Edit the file to the correct result, remove the markers, then git add file and git commit (or git rebase --continue).
Good habits
- Commit small, logical changes often.
- Pull or rebase before you push.
- Use branches for every feature or fix.
- Review
git diff --stagedbefore committing. - Protect
mainwith pull requests and CI.
Related reading: REST API Basics for what you will probably be building, and the Case Converter when naming branches (kebab-case) and variables.
Related tools
Text Diff Checker
Compare two texts and see added and removed lines.
Regex Tester
Test JavaScript regular expressions with live match highlighting.
Case Converter
Convert text to camelCase, snake_case, kebab-case and more.
FAQ
git merge or git rebase?
Merge preserves history exactly and is safe on shared branches. Rebase rewrites commits for a linear history; avoid rebasing commits others already pulled.
How do I undo my last commit but keep the changes?
Run git reset --soft HEAD~1.
What is the difference between git fetch and git pull?
fetch downloads remote changes without touching your branch; pull is fetch plus a merge (or rebase).
Keep reading
- REST API Basics
Understand REST APIs: resources, HTTP methods, status codes, JSON payloads, idempotency, pagination, versioning and authentication basics.
- Regular Expressions for Beginners
Learn regular expressions from scratch: character classes, quantifiers, anchors, groups, lookaheads, flags and common patterns, with JavaScript examples.