Tools
Git Cheat Sheet — Essential Commands Reference
The Git commands that cover almost all everyday work, grouped by what you are trying to do.
Put it into practice with the matching tool.
Generate a .gitignore →Advertisement
Setup & config
git config --global user.name "Name"Set your commit namegit config --global user.email "[email protected]"Set your commit emailgit config --listShow all current settingsStarting a repo
git initCreate a new repository heregit clone <url>Copy a remote repository locallygit remote -vList configured remotesgit remote add origin <url>Attach a remote named originEveryday snapshotting
git statusShow changed and staged filesgit add <file>Stage a specific filegit add .Stage everything in the current directorygit commit -m "message"Commit staged changesgit commit -am "message"Stage tracked files and commit in one stepgit diffShow unstaged changesgit diff --stagedShow staged changesBranching & merging
git branchList local branchesgit switch -c <name>Create and switch to a new branchgit switch <name>Switch to an existing branchgit merge <branch>Merge a branch into the current onegit rebase <branch>Replay your commits on top of another branchgit branch -d <name>Delete a merged branchSyncing with a remote
git fetchDownload remote changes without merginggit pullFetch and merge remote changesgit pushUpload your commits to the remotegit push -u origin <branch>Push and set the upstream branchUndoing things
git restore <file>Discard local changes to a filegit restore --staged <file>Unstage a file, keeping the changesgit commit --amendRewrite the most recent commitgit revert <commit>Create a new commit undoing an old one (safe)git reset --hard <commit>Discard commits and changes (destructive)History & stashing
git log --oneline --graphCompact visual historygit show <commit>Show the contents of a commitgit blame <file>See who last changed each linegit stashShelve your uncommitted changesgit stash popReapply the most recently stashed changesGit in practice
A typical day with Git
Start by updating: git switch main and git pull. Create a branch for your change with git switch -c fix-login-redirect. Work, then review what changed with git status and git diff before staging with git add. Commit small, focused changes with messages that explain why, not just what. Push the branch with git push -u origin fix-login-redirect and open a pull request. When it is merged, switch back to main, pull, and delete the branch.
Undoing things safely
Choose the undo that matches the situation. To discard unstaged edits to a file, git restore file. To unstage without losing changes, git restore --staged file. To change the last commit before pushing, git commit --amend. To undo a commit that has already been pushed and shared, use git revert, which creates a new commit reversing it, rather than rewriting history. Reserve git reset --hard for local work you are certain you want to throw away.
If something seems lost, git reflog lists every position HEAD has been in, including commits that no branch points to any more. Almost nothing committed is ever truly gone for the first few weeks.
Merge or rebase?
Merging keeps history exactly as it happened and adds a merge commit; it is safe for shared branches. Rebasing replays your commits on top of the latest main, producing a straight line that is easier to read. The rule is simple: rebase your own unpushed or private branches freely, and never rebase commits other people have based work on. git pull --rebase keeps a local branch tidy when syncing.
Writing useful commits
A good commit does one thing and can be understood later from its message alone. Use a short summary line of around 50 to 72 characters in the imperative mood — "Fix redirect after login" — then a blank line and, if needed, a body explaining the reason. Stage only related changes; git add -p lets you choose individual hunks within a file. Small commits make reviews easier and make git bisect effective when hunting for the commit that introduced a bug.
Common pitfalls
Committing secrets: once pushed, assume they are leaked and rotate them. Committing generated files and dependencies: add them to .gitignore before the first commit. Working directly on main in shared repositories. Force-pushing over a teammate's work — use git push --force-with-lease, which refuses to overwrite commits you have not seen. And resolving merge conflicts without running the tests afterwards.

