Git Cheatsheet
Quick reference for everyday Git commands: commit, branches, undo, remotes, and stash.
Git Cheatsheet
Git is a version control system: it tracks changes to your files over time and lets you work with others on the same codebase. You commit snapshots of your project, switch between branches to try ideas or fix bugs, and sync with a remote (e.g. GitHub) to push and pull changes. Most teams use it for source code, but you can use it for any set of files you want to version and share.
Clone and daily loop
Clone a repo once. Then you usually repeat: edit, stage, commit, push.
git clone https://github.com/username/myapp.git # copy repo to current folder
cd myappAfter you change files:
git status # see what changed
git add style.css # stage one file (or git add . for all)
git commit -m "Update button color" # save a snapshot with a message
git push # send commits to remote (e.g. origin)git add . stages everything in the current directory. git add -p lets you stage parts of a file (patch mode). To see what you’re about to commit: git diff --staged. To see what’s not staged: git diff.
Example with several files: you changed index.html, style.css, and added script.js. You want to commit the CSS and script but not the HTML yet.
git add style.css script.js
git commit -m "Add styles and main script"
# index.html stays modified, uncommitted
git pushBranches
Branches let you work on a feature or fix without changing the main line. You create a branch, commit there, then merge it into main when ready. That way main stays stable while you experiment. List branches: git branch. Create and switch in one go: git switch -c feature/login. Do your work, commit, then merge into main.
git switch -c feature/login # create branch and switch to it (-c = create)
# edit files...
git add .
git commit -m "Add login form and validation"
git switch main # go back to main to receive the merge
git merge feature/login # bring feature branch into main
git pushDelete the branch when you’re done: git branch -d feature/login. If you haven’t merged, Git refuses unless you use -D. To only create a branch without switching: git branch hotfix/typo. To switch to an existing branch: git switch main or git checkout main.
What happens when you merge. Git combines the history of two branches. If the same lines were changed in both branches, you get a merge conflict. Git marks the file with <<<<<<<, =======, >>>>>>>. Open the file, choose what to keep (or edit both sides), remove the markers, then git add thefile and git commit (or run git merge --continue if you’re in the middle of a merge). To abort a merge: git merge --abort.
Example: you’re on main and need to fix a typo on the site. Someone else might be working on main, so you use a short-lived branch.
git switch -c fix/typo
# fix the typo in about.html
git add about.html
git commit -m "Fix company name typo"
git switch main
git merge fix/typo
git push
git branch -d fix/typoUndo and fix mistakes
You haven’t committed yet. To throw away changes in one file: git restore index.html. Older Git: git checkout -- index.html. Those changes are gone.
You staged a file by mistake. Unstage but keep the edits: git restore --staged index.html.
Wrong commit message. Fix the last commit message: git commit --amend -m "Correct message". If you already pushed, rewriting history can confuse others. Prefer a new commit or git revert on shared branches.
Forgot to add a file to the last commit. Add it and amend without changing the message: git add forgotten.js then git commit --amend --no-edit.
Need to undo the last commit but keep the changes staged. git reset --soft HEAD~1. The commit is gone, the files are still staged so you can edit and commit again.
Need to undo the last commit and discard everything. git reset --hard HEAD~1. Destructive. Only do this if you’re sure.
Revert a commit that’s already pushed (safe). Creates a new commit that undoes the given one: git revert abc1234 or git revert HEAD. Good for shared branches. Use revert when others may have pulled the bad commit. Use reset only on commits you haven’t shared, since reset rewrites history.
Example: you committed with message “WIP” and want a proper message.
git commit --amend -m "Add contact form and success message" # rewrites last commit (message and/or staged files)Example: you pushed a bad commit. Others might have pulled. Revert it instead of reset.
git log --oneline # find the bad commit hash
# e.g. a1b2c3d
git revert a1b2c3d -m "Revert accidental removal of config" # new commit that undoes that one (safe for shared history)
git pushRemotes
A remote is another copy of the repo (usually on a server like GitHub). You push your commits to it and pull others’ changes. Most repos have one remote named origin. List remotes: git remote -v. First time pushing a new branch: set the upstream so later git push and git pull work without extra arguments.
git push -u origin feature/login # -u sets upstream so later just git push works
# from now on: git push, git pullFetch updates without merging: git fetch origin. Then merge or rebase when you want. To pull a specific branch into your current branch: git pull origin main.
Example: you created a repo on GitHub and have local commits. Add the remote and push.
git remote add origin https://github.com/username/repo.git # "origin" is the usual name for the main remote
git branch -M main # rename current branch to main if needed
git push -u origin main # first push; -u sets upstream for this branchFetch vs pull. git fetch origin downloads new commits and updates your remote-tracking branches (e.g. origin/main) but does not change your current branch. You then decide: git merge origin/main or git rebase origin/main. git pull is shorthand for fetch plus merge (or rebase if you configured it). Use fetch when you want to see what changed before merging, or when you’re on a feature branch and want to update it with main without switching.
Example: you’re on feature/login and main has moved. Get the latest main and replay your commits on top.
git fetch origin # get latest from remote, don't merge yet
git rebase origin/main # replay your commits on top of latest main (can cause conflicts)
# fix any conflicts, then git add . && git rebase --continue
git push # may need --force-with-lease if you already pushed this branchIf you already pushed feature/login, rebase rewrites history so you may need git push --force-with-lease (safer than --force: it refuses if someone else pushed to the same branch).
Push rejected. If git push says the remote has commits you don’t have, someone else (or you on another machine) pushed. Pull or fetch and merge first, then push. Don’t force push on shared branches.
git pull origin main
# resolve conflicts if any, then commit
git pushStash
Stash saves your working changes and clears the working tree so you can switch branch or pull. Later you reapply the stash.
# you have uncommitted changes, need to switch to main
git stash # save changes away, working tree becomes clean
git switch main
# fix something, commit, push
git switch feature/login
git stash pop # reapply the stash and remove it from the list (use stash apply to keep it)git stash list shows your stashes. git stash apply applies the latest but keeps it in the list. git stash pop applies and removes it. To stash untracked files too: git stash -u. To stash with a label: git stash push -m "WIP form validation".
Example: you’re in the middle of a feature, your boss asks for a hotfix on main.
git stash -m "login form WIP"
git switch main
# make fix, commit, push
git switch feature/login
git stash popHandy one-liners
Stage all changes including deletions: git add -A. Commit all tracked file changes without a separate add: git commit -am "Fix typo" (only tracked files). Short log: git log --oneline -10. See who changed a line: git blame style.css. Force push (rewrites remote history): git push --force. Use only on branches you own and when you accept the risk.
Viewing history and diffs. git log --oneline -20 shows the last 20 commits. git log -p file.css shows commits that touched that file plus the patch. To compare your branch to main: git diff main (unstaged and staged changes) or git diff main -- file.css (one file). To see what changed in the last commit: git show HEAD.
Rebase to tidy history. On a feature branch, git rebase main replays your commits on top of the current main. Result: a linear history as if you had started from the latest main. Use before merging so the main branch stays clean. If you’re not comfortable with rebase, merging is fine and keeps the full history.
Ignore files. Create a .gitignore file in the repo root. Each line is a path or pattern. Git will not track matching files. Example: node_modules/, *.log, .env. Useful so build output, dependencies, and secrets stay out of the repo.