← All cheat sheets

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 name
git config --global user.email "[email protected]"Set your commit email
git config --listShow all current settings

Starting a repo

git initCreate a new repository here
git clone <url>Copy a remote repository locally
git remote -vList configured remotes
git remote add origin <url>Attach a remote named origin

Everyday snapshotting

git statusShow changed and staged files
git add <file>Stage a specific file
git add .Stage everything in the current directory
git commit -m "message"Commit staged changes
git commit -am "message"Stage tracked files and commit in one step
git diffShow unstaged changes
git diff --stagedShow staged changes

Branching & merging

git branchList local branches
git switch -c <name>Create and switch to a new branch
git switch <name>Switch to an existing branch
git merge <branch>Merge a branch into the current one
git rebase <branch>Replay your commits on top of another branch
git branch -d <name>Delete a merged branch

Syncing with a remote

git fetchDownload remote changes without merging
git pullFetch and merge remote changes
git pushUpload your commits to the remote
git push -u origin <branch>Push and set the upstream branch

Undoing things

git restore <file>Discard local changes to a file
git restore --staged <file>Unstage a file, keeping the changes
git commit --amendRewrite the most recent commit
git 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 history
git show <commit>Show the contents of a commit
git blame <file>See who last changed each line
git stashShelve your uncommitted changes
git stash popReapply the most recently stashed changes

Git 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.

Related guides

More cheat sheets