Git Reference

Version Control

Use this page when you need the right Git command quickly. Each task includes the command, a concrete example, and a practical warning where the command can be destructive or confusing.

Undo & Recovery
git reset --soft HEAD~1

Example: Use this after a commit with the wrong message or when you want to split the commit into smaller commits.

Gotcha: This rewrites local history. Do not use it on a commit that you already pushed to a shared branch unless you know the team impact.

git restore path/to/file

Example: Example: git restore resources/views/cheats/sheets.blade.php

Gotcha: This permanently drops your local edits for that file if they are not staged or backed up elsewhere.

Stash
git stash push -m "wip before deploy"
git stash pop

Example: Stash before switching branches, then restore the work after the urgent task is done.

Gotcha: Use a message so the stash is identifiable. `pop` removes the stash entry after applying it; use `apply` if you want to keep it.

Inspect & Compare
git log --follow -- path/to/file

Example: Example: git log --follow -- app/Http/Controllers/Coding/CheatsheetController.php

Gotcha: The `--follow` flag helps across renames, but it works best for a single file path.

git diff main...feature/my-change

Example: Use the three-dot form to compare what your feature branch changes relative to the merge base.

Gotcha: Two-dot and three-dot diffs answer different questions. The three-dot form is usually the safer review view for pull-request style work.

CodersTool Categories