PUBLIC REFERENCE // GIT

Git Cheatsheet

Git tracks your project as a series of snapshots rather than file-by-file diffs, which is why operations like branching and merging are so fast — a branch is just a movable pointer to a commit, not a copy of the codebase.

Commands
83
Sections
10
Source policy
Official links

FIELD NOTES

Decisions worth remembering

  • git merge keeps the full history of both branches via a merge commit; git rebase rewrites commits onto a new base — never rebase commits you've already pushed and others may have pulled.
  • git push --force-with-lease is safer than git push --force: it aborts if the remote branch has commits you have not seen yet, instead of silently discarding them.
  • Checking out a specific commit hash (not a branch) puts you in a detached HEAD state — any commits made there become unreachable once you switch branches, unless you create a new branch first.
  • Adding a path to .gitignore only stops new files from being tracked — for files already committed, you also need git rm --cached <file> to actually stop tracking them.

GIT // REFERENCE

Setup & Config

6 commands

GIT // REFERENCE

Creating & Cloning

5 commands

GIT // REFERENCE

Staging & Committing

9 commands

GIT // REFERENCE

Branching

10 commands
git branch -d feature-name

Delete a merged branch

Safety: This command can discard data, stop work, or remove resources. Replace placeholders and verify the exact target in a disposable environment first.

git branch -D feature-name

Force delete a branch (unmerged)

Safety: This command can discard data, stop work, or remove resources. Replace placeholders and verify the exact target in a disposable environment first.

GIT // REFERENCE

Merging & Rebasing

9 commands

GIT // REFERENCE

Stashing

8 commands

GIT // REFERENCE

Remote Repositories

11 commands
git push --force-with-lease

Safe force push (checks remote state)

Safety: This command can discard data, stop work, or remove resources. Replace placeholders and verify the exact target in a disposable environment first.

GIT // REFERENCE

Undoing Changes

7 commands
git reset --hard HEAD~1

Undo last commit and discard changes

Safety: This command can discard data, stop work, or remove resources. Replace placeholders and verify the exact target in a disposable environment first.

git clean -fd

Remove untracked files and directories

Safety: This command can discard data, stop work, or remove resources. Replace placeholders and verify the exact target in a disposable environment first.

GIT // REFERENCE

Log & Diff

10 commands

GIT // REFERENCE

Tags

8 commands

COMMON QUESTIONS

Git command FAQ

How do I undo the last commit without losing my changes?

Run git reset --soft HEAD~1 — it removes the commit but keeps your changes staged, ready to re-commit.

What is the difference between git fetch and git pull?

git fetch downloads remote commits without touching your working branch; git pull runs fetch and then immediately merges (or rebases) into your current branch.

How do I recover a deleted branch?

If you know the last commit hash (check git reflog), run git checkout -b branch-name <hash> to recreate it — Git doesn't actually delete commit objects immediately.

Why does git status show a file as modified right after I clone or pull?

Usually a line-ending mismatch (CRLF vs LF) — configure core.autocrlf consistently across your team, or add a .gitattributes file.