← Back to the blog

Git Cheat Sheet for Everyday Work

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:

shell
git clone git@example.com:team/project.git

Initialize a local project and connect a remote:

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

Change or remove a remote:

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

Inspect the current state

shell
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:

shell
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:

shell
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:

shell
git switch -c feature/clear-name

Switch to an existing branch or create a local branch from a remote one:

shell
git switch main
git fetch origin
git switch --track origin/feature/clear-name

Rename the current branch and publish it:

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

Prune remote-tracking references that no longer exist:

shell
git fetch --prune origin

Restore changes safely

Discard unstaged changes in one file:

shell
git restore path/to/file

Unstage a file while keeping the working-tree change:

shell
git restore --staged path/to/file

Restore a file from a known commit:

shell
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:

shell
git fetch origin

Push the current branch and establish its upstream:

shell
git push --set-upstream origin HEAD

Tags are a separate push:

shell
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