Title: Git Recovering From Common Mistakes — LearnCoachAssist

Q: Undo the last commit but keep changes staged.
A: git reset --soft HEAD~1

Q: Undo the last commit AND discard changes.
A: git reset --hard HEAD~1  ⚠ destructive.

Q: Undo the last commit but keep changes unstaged in the working tree.
A: git reset --mixed HEAD~1 (the default).

Q: Amend the last commit message.
A: git commit --amend -m 'new message'

Q: Amend the last commit to include forgotten files.
A: git add forgotten-file
git commit --amend --no-edit

Q: Discard local changes to a specific file.
A: git checkout -- file or modern: git restore file

Q: Discard all local uncommitted changes.
A: git restore . followed by git clean -fd for untracked files.

Q: Unstage a file you accidentally added.
A: git restore --staged file (or older: git reset HEAD file).

Q: List untracked files that would be removed.
A: git clean -nd — dry-run.

Q: Force-remove untracked files and directories.
A: git clean -fd  ⚠ destructive.

Q: Recover a deleted commit you can no longer see.
A: git reflog to find its SHA, then git checkout -b rescue <sha>

Q: Switch back to a previous commit temporarily.
A: git checkout <sha> (detached HEAD) — create a branch if you want to keep changes: git switch -c branch-name.

Q: Revert a public commit (creates an inverse commit).
A: git revert <sha>

Q: Apply a commit from another branch onto current.
A: git cherry-pick <sha>

Q: Squash the last 3 commits into one.
A: git rebase -i HEAD~3 — change pick to squash for all but the first.

Q: Reorder or edit commits via interactive rebase.
A: git rebase -i HEAD~5 — reorder lines, change pick to edit or reword.

Q: Abort an in-progress rebase.
A: git rebase --abort

Q: Continue a rebase after resolving conflicts.
A: git add <resolved files> && git rebase --continue

Q: Skip the current commit in a rebase.
A: git rebase --skip

Q: Merge two branches.
A: git switch main && git merge feature

Q: Force a fast-forward merge only (fail otherwise).
A: git merge --ff-only feature

Q: Always create a merge commit, even if fast-forward is possible.
A: git merge --no-ff feature

Q: Cancel an in-progress merge.
A: git merge --abort

Q: Save uncommitted work without committing.
A: git stash push -m 'message'

Q: List stashes.
A: git stash list

Q: Reapply most recent stash and remove it from stack.
A: git stash pop

Q: Reapply but keep the stash entry.
A: git stash apply

Q: Apply a specific stash.
A: git stash apply stash@{2}

Q: Stash including untracked files.
A: git stash push -u

Q: Drop a single stash.
A: git stash drop stash@{0}

Q: Clear all stashes.
A: git stash clear  ⚠ destructive.

Q: Rename the current branch.
A: git branch -m new-name — add -M to force overwrite.

Q: Delete a local branch.
A: git branch -d branchname (refuses if unmerged); force: -D.

Q: Delete a remote branch.
A: git push origin --delete branchname

Q: Sync local branch list with remote (prune stale branches).
A: git fetch -p

Q: Set upstream tracking on push.
A: git push -u origin branchname

Q: Pull with rebase instead of merge.
A: git pull --rebase

Q: Make rebase the default for pulls.
A: git config --global pull.rebase true

Q: Resolve a conflict marker like <<<<<<<.
A: Edit the file, keep only the version you want, remove all conflict markers, then git add the file and continue rebase/merge.

Q: List files with conflicts.
A: git status or git diff --name-only --diff-filter=U

Q: Accept theirs / ours for a file during merge?
A: git checkout --theirs file or --ours.

Q: Difference between merge --strategy=recursive 'theirs' and -X theirs?
A: -X theirs: prefer their side automatically for conflicts (within recursive).
--strategy=ours: keep ours entirely; ignore theirs (rare and dangerous).

Q: Push to a remote branch with a different name.
A: git push origin local-name:remote-name

Q: Fix 'fatal: refusing to merge unrelated histories'.
A: Add --allow-unrelated-histories to merge or pull — but verify you actually want to combine.

Q: Update a feature branch with latest main (no merge commit).
A: git switch feature && git rebase main

Q: Show files changed in a commit.
A: git show --name-only <sha>

Q: See line-by-line history of a file.
A: git log --follow -p file

Q: Who last touched each line of a file?
A: git blame file; ignore whitespace with -w.

Q: Find when a function was introduced/removed.
A: git log -L :funcName:file or -S 'searchString' for pickaxe.

Q: Compare branches.
A: git log main..feature — commits in feature not in main.

Q: Commits in main not yet in feature?
A: git log feature..main

Q: Two-dot vs three-dot diff?
A: git diff main..feature: tip-to-tip diff.
git diff main...feature: diff from their common ancestor (good for PR diffs).

Q: Fix accidentally committed to wrong branch.
A: From wrong branch: git log --oneline -n 3 note SHA.
git switch right-branch && git cherry-pick <sha>
git switch wrong-branch && git reset --hard HEAD~1

Q: Restore a single file from another branch.
A: git checkout branchname -- path/to/file

Q: Reset a branch to match remote exactly.
A: git fetch origin && git reset --hard origin/branchname  ⚠ discards local commits.

Q: Show what would change in a pull without merging.
A: git fetch && git log HEAD..@{u} --oneline

Q: Tag the current commit.
A: git tag v1.0.0 (lightweight) or git tag -a v1.0.0 -m 'release' (annotated).

Q: Push tags to remote.
A: git push origin --tags

Q: Delete a tag locally and remotely.
A: git tag -d v1.0.0
git push origin :refs/tags/v1.0.0

Q: Clone a single branch only.
A: git clone --branch main --single-branch URL

Q: Shallow clone for fast CI?
A: git clone --depth 1 URL

Q: Unshallow later if needed.
A: git fetch --unshallow

Q: Bisect to find the commit that introduced a bug.
A: git bisect start
git bisect bad
git bisect good <sha> — Git walks you through halves until it finds the bad commit.

Q: Auto-bisect with a test script?
A: git bisect run ./test.sh

Q: End a bisect session.
A: git bisect reset

Q: Recover from a force-push that overwrote your branch.
A: Find old tip in reflog: git reflog show branchname, then git reset --hard <old-sha>. Don't pull/fetch until you do this.

Q: Prevent destructive force pushes.
A: git push --force-with-lease — refuses if remote has commits you don't have.

Q: Difference between --force and --force-with-lease?
A: --force: blind overwrite.
--force-with-lease: only force if you have the latest of the branch (safer).

Q: Get rid of a committed secret in history.
A: Use git filter-repo or BFG Repo-Cleaner to remove from history. Then force-push and rotate the secret.

Q: Set local config for one repo.
A: git config user.email 'work@example.com' (omit --global).

Q: View all config and where each setting comes from.
A: git config --list --show-origin

Q: Make Git case-insensitive on file names?
A: git config core.ignorecase true — but be cautious on cross-platform projects.

Q: Show diff with word-level changes.
A: git diff --word-diff

Q: Diff staged changes only.
A: git diff --cached (or --staged).

Q: Avoid 'detached HEAD' confusion on checkout?
A: Use git switch (modern) — separates branch ops from checkout.

Q: Sparse checkout (only some folders)?
A: git sparse-checkout init --cone && git sparse-checkout set folder1 folder2

Q: Submodule basics?
A: git submodule add URL path, git submodule update --init --recursive, commits the SHA pointer in the parent.

Q: Track empty directory?
A: Git doesn't track directories — add a placeholder file like .gitkeep.

Q: Why was my .gitignore rule ignored?
A: File was already tracked. Run git rm --cached path to stop tracking, then commit.

Q: Show ignored files?
A: git status --ignored or git check-ignore -v path.

Q: Add an upstream remote.
A: git remote add upstream URL && git fetch upstream

Q: Pull from upstream and update your fork's main.
A: git switch main
git fetch upstream
git rebase upstream/main
git push origin main

Q: Show a graphical commit history in the terminal.
A: git log --graph --oneline --decorate --all

Q: Make commits readable in PRs via auto-rebase?
A: git rebase -i --autosquash main — combined with git commit --fixup <sha> for tagging follow-ups.

Q: Sign your commits with GPG?
A: Set user.signingkey and commit.gpgsign = true, then commit normally.

Q: Move a commit from one branch to another?
A: git switch target && git cherry-pick <sha> then remove from source via reset or rebase.

Q: Empty a feature branch to start over but keep its name?
A: git switch --orphan feature && git rm -rf . then commit fresh.

Q: Why does git status show files as modified after a fresh clone on Windows?
A: Line-ending normalization — configure core.autocrlf and add a .gitattributes file with text=auto eol=lf.

Q: Quickly create a PR-ready single commit from messy history?
A: git reset --soft main && git commit -m 'clean message' — collapses everything into one commit while keeping changes.

Q: Show changes that haven't been staged yet?
A: git diff

Q: Show changes that ARE staged?
A: git diff --staged

Q: Pause a long-running rebase to test, then continue?
A: git rebase --edit-todo to add break lines; later git rebase --continue.

Q: Restore deleted file from previous commit?
A: git checkout HEAD~1 -- path

Q: Recover a deleted branch?
A: git reflog to find its last tip, then git switch -c branchname <sha>.

Q: List branches sorted by most recent commit?
A: git for-each-ref --sort=-committerdate refs/heads/ --format='%(committerdate:short) %(refname:short)'

Q: Reflog default expiry?
A: 90 days for reachable, 30 days for unreachable; bump with gc.reflogExpire.

Q: Manual garbage collection?
A: git gc; aggressive: git gc --aggressive --prune=now.

Q: Why git pull sometimes creates ugly merge commits?
A: Default is merge-pull. Set pull.rebase true or use git pull --rebase.

Q: How do you check the upstream of the current branch?
A: git rev-parse --abbrev-ref --symbolic-full-name @{u}

Q: Diff a file at two SHAs?
A: git diff sha1 sha2 -- file

Q: Show full commit message body?
A: git log -1 <sha> or git show <sha>

Q: Inspect a packed object?
A: git cat-file -p <sha>

Q: Recover dangling commits older than reflog?
A: Inspect git fsck --lost-found — Git reports unreachable commits; you can git show them and cherry-pick.

Q: Difference between origin/main and origin main?
A: origin/main: the remote-tracking branch (local cache of remote's main).
origin main: remote name + ref name (used in commands like git push origin main).

Q: Reuse recorded conflict resolutions?
A: Enable rerere: git config --global rerere.enabled true — Git remembers resolutions and reapplies them.