Git Cheat Sheet for Everyday Work
Practical Git commands for repositories, remotes, branches, staged changes, recovery, and safer collaboration.

Git becomes easier when you identify the state you want to change: working tree, index, local history, or remote history. This reference groups commands by that intent.
Before a destructive or history-rewriting command, run
git status, check the current branch, and confirm whether commits have already been shared.
Start or clone a repository
Clone an existing repository:
git clone git@example.com:team/project.gitInitialize a local project and connect a remote:
git init
git remote add origin git@example.com:team/project.git
git remote -vChange or remove a remote:
git remote set-url origin git@example.com:team/new-project.git
git remote remove originInspect the current state
git status --short --branch
git diff
git diff --staged
git log --oneline --decorate --graph -20git diff shows unstaged changes. git diff --staged shows what the next commit would contain.
Stage and commit
Stage specific paths instead of everything by default:
git add path/to/file another/path
git commit -m "feat: describe the outcome"If .gitignore changed and a tracked file should become ignored, remove only that path from the index:
git rm --cached path/to/fileAvoid clearing the entire index unless you have inspected the scope and genuinely need a repository-wide re-index.
Branches
Create and switch to a branch:
git switch -c feature/clear-nameSwitch to an existing branch or create a local branch from a remote one:
git switch main
git fetch origin
git switch --track origin/feature/clear-nameRename the current branch and publish it:
git branch -m new-name
git push --set-upstream origin new-namePrune remote-tracking references that no longer exist:
git fetch --prune originRestore changes safely
Discard unstaged changes in one file:
git restore path/to/fileUnstage a file while keeping the working-tree change:
git restore --staged path/to/fileRestore a file from a known commit:
git restore --source=<commit> -- path/to/fileThese commands overwrite some local state. Inspect git diff first if the change is not safely reproduced elsewhere.
Synchronize with a remote
Fetch remote history without changing the current branch:
git fetch originPush the current branch and establish its upstream:
git push --set-upstream origin HEADTags are a separate push:
git push origin --tagsRecovery mindset
When something goes wrong, stop adding commands until you know which Git layer changed. git status, git reflog, and a copy of an uncommitted file are usually more useful than an aggressive reset. The safest Git workflow is not memorizing the most commands—it is making the smallest explicit change and verifying it before continuing.