← Back to the blog

Git Cheat Sheet for Everyday Work

Practical Git commands for repositories, remotes, branches, staged changes, recovery, and safer collaboration.

Git workflow reference

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

Initialize a local project and connect a remote:

git init
git remote add origin git@example.com:team/project.git
git remote -v

Change or remove a remote:

git remote set-url origin git@example.com:team/new-project.git
git remote remove origin

Inspect the current state

git status --short --branch
git diff
git diff --staged
git log --oneline --decorate --graph -20

git 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/file

Avoid 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-name

Switch 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-name

Rename the current branch and publish it:

git branch -m new-name
git push --set-upstream origin new-name

Prune remote-tracking references that no longer exist:

git fetch --prune origin

Restore changes safely

Discard unstaged changes in one file:

git restore path/to/file

Unstage a file while keeping the working-tree change:

git restore --staged path/to/file

Restore a file from a known commit:

git restore --source=<commit> -- path/to/file

These 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 origin

Push the current branch and establish its upstream:

git push --set-upstream origin HEAD

Tags are a separate push:

git push origin --tags

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

← Back to the blog