Interview questions and cheat sheets
Git Commands Cheat Sheet by Task
This Git cheat sheet is organised by what you are trying to do, with copy-ready commands and a clear label on every command that can destroy work. It ends with the reflog, which is how most lost commits get back.
By CodingMarble · Published · 4 min read

Setup and a .gitignore habit
Configure your identity once per machine, because it is written into every commit. Then create a .gitignore before the first commit, not after, so that dependencies, build output and secrets never enter history.
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git config --global init.defaultBranch main
git init # start a repository in this folder
git clone <url> # copy an existing repository# .gitignore
node_modules/
dist/
.next/
.env
.env.local
*.log
.DS_StoreSetting init.defaultBranch means new repositories start on main instead of the older default name. Cloning also sets up origin, the default name for the remote you cloned from, and checks out its default branch. Global settings live in your home folder, while a setting without --global applies only to the current repository.
A file that is already tracked stays tracked even after you add it to .gitignore. Stop tracking it with git rm --cached <file>, then commit. If a secret such as a key in .env was ever committed, removing it later is not enough; rotate the key, because the old commit still contains it.
The daily loop: status, add, commit
Git has three places a change can be: the working folder, the staging area, and the repository. You edit, stage what belongs in the next commit, then commit. The staging area exists so that a commit can contain exactly the changes that belong together, even when you have edited many unrelated files. A commit is a snapshot of the staged content, identified by a hash, with a pointer to its parent.
git status # what changed, what is staged
git add src/app.js # stage one file
git add -p # stage chosen hunks, reviewing each
git diff # unstaged changes
git diff --staged # what the next commit will contain
git commit -m "Fix total when cart is empty"
git commit --amend # rewrite the last commit (only if not pushed)- git add . stages everything in the current folder, so check git status first to avoid committing a stray file.
- git add -p is the quickest way to keep unrelated edits in separate commits.
- A good commit message states what changed and why in the first line, in the imperative mood.
- git commit --amend replaces the last commit; do not use it after pushing a shared branch.
Branching and merging
A branch is a movable label on a commit, so creating one is cheap. Work on a branch per task and merge it back when it is reviewed. git switch is the modern command for changing branches; the older git checkout does the same job and also restores files, which is why newer guides split it into switch and restore.
git branch # list local branches
git switch -c feature/login # create and move to a new branch
git switch main # move back
git merge feature/login # bring the branch into the current one
git branch -d feature/login # delete a merged branch
git branch -D feature/login # force delete, even if unmerged (destructive)If both branches changed the same lines, the merge stops with a conflict. Open the marked files, choose the final text, delete the marker lines, then git add the file and run git commit. git merge --abort returns you to the state before the merge.
Rebase versus merge
Both combine work from two branches. Merge keeps the true history and adds a merge commit. Rebase replays your commits on top of another branch, giving a straight line of history but creating new commits with new hashes.
git switch feature/login
git rebase main # replay my commits on top of latest main
git rebase -i HEAD~3 # tidy the last 3 commits: squash, reword, reorder- Use rebase to update your own unpublished branch with the latest main, and to clean up commits before opening a pull request.
- Use merge when the branch is shared, because rebasing public history forces everyone else to repair their copies.
- The rule to remember: never rebase commits that other people have already pulled.
- If a rebase goes wrong, git rebase --abort restores the branch.
Undoing mistakes safely
Pick the undo command by asking two questions: has the change been committed, and has it been pushed. The table of options below runs from harmless to destructive.
git restore --staged app.js # unstage a file, keep the edits (safe)
git restore app.js # discard unstaged edits in the file (destructive)
git revert <commit> # new commit that undoes an old one (safe, public-friendly)
git reset --soft HEAD~1 # undo last commit, keep changes staged
git reset HEAD~1 # undo last commit, keep changes unstaged
git reset --hard HEAD~1 # undo last commit AND delete the changes (destructive)
git clean -n # preview untracked files that would be removed
git clean -fd # delete untracked files and folders (destructive)- git revert is the right tool for a commit that is already pushed, because it adds history rather than rewriting it.
- git reset moves the branch pointer. The soft and default modes keep your work, and only --hard throws it away.
- git restore <file> and git clean -fd delete uncommitted work that Git cannot bring back, since it was never saved in a commit.
- If you must overwrite a remote branch after a rebase, use git push --force-with-lease, which refuses to overwrite commits you have not seen. Plain --force does not check.
Stash and remotes
Stash shelves uncommitted changes so you can switch tasks with a clean working folder. Remotes are named copies of the repository elsewhere, usually called origin.
git stash push -m "half-done form" # shelve tracked changes
git stash list
git stash pop # reapply the latest and drop it
git stash apply stash@{1} # reapply a chosen one and keep it
git remote -v # show remotes
git fetch # download new commits, change nothing locally
git pull --rebase # fetch, then replay my commits on top
git push -u origin feature/login # first push, sets the upstream branchgit fetch is always safe because it only updates your view of the remote. git pull is fetch plus a merge or rebase, so it can change your branch. Stash does not include untracked files unless you add -u.
Reading history: log, diff, blame
History is most useful when you can search it. These commands answer who changed a line, when, and what a commit actually did.
git log --oneline --graph --decorate -15 # compact branch picture
git log -p -- src/app.js # history of one file, with diffs
git log --author="Asha" --since="2 weeks ago"
git show <commit> # one commit in full
git diff main..feature/login # differences between two branches
git blame -L 20,40 src/app.js # who last changed lines 20-40Each command answers a different question. git log with a path shows how one file evolved, git show prints one commit with its message and diff, git diff between two branches shows what a merge would bring in, and the double-dot range lists changes reachable from one side. Adding -15 limits the output to the latest fifteen commits.
git log -S 'functionName' finds commits that added or removed that exact text, which is often faster than reading blame output. Blame shows the last editor of a line, not necessarily the author of the idea, so follow it back with git show.
Keep reading
- Interview questions and cheat sheetsSQL Cheat Sheet for BeginnersThis cheat sheet teaches the SQL you use every week, task by task, with every query written against one small customers and orders schema. It uses standard SQL and points out where MySQL, PostgreSQL, SQL Server and SQLite differ.
- Interview questions and cheat sheetsHTTP Status Codes Cheat SheetThis cheat sheet explains the HTTP status codes you meet in real websites and APIs, grouped by class, with guidance on which one to return and why. It also covers redirects and soft 404s for SEO, caching with 304, and how to inspect responses with curl.
- AI and developer toolsDebug With AI Without Leaking Secrets or Customer DataYou can debug with an AI assistant safely by pasting only a small, redacted, reproducible example and never anything that grants access, such as keys, tokens or connection strings. If a secret does leave your machine, treat it as compromised and rotate it immediately instead of hoping nobody saw it.
- Web developmentWebsite Security Basics for Small BusinessesMost small business sites are compromised through outdated plugins, weak or reused passwords and missing basic server settings, not through clever attacks. Cover HTTPS, updates, authentication, security headers, form handling and tested backups, and you remove the common routes in.
Need this built properly for your business?
CodingMarble is a remote studio in India that builds websites, apps and games on fixed packages with the scope written down before payment. Try the free tools we run, or start a project when you are ready.