All Undo Branches Remote Stash History Merge

Git Situation to Command Reference

Git is powerful but its command syntax can be hard to remember, especially for less common operations. This tool maps real-world Git situations to their exact commands, with plain-English explanations and warnings for operations that can't be undone.

Destructive vs Safe Git Operations

Some Git operations are safe and reversible (commits, branches, stashes). Others permanently alter history or can cause data loss if used carelessly:

  • Safe: git stash, git branch, git checkout, git merge, git commit
  • Caution needed: git reset --hard, git push --force, git rebase (rewrites history), git clean -fd
  • Never do on shared branches: force-pushing to main/master, rebasing public commits

Git Best Practices

  • Always create a branch before making changes — never work directly on main
  • Commit often with clear, descriptive messages
  • Use git status and git diff before committing
  • Prefer git revert over git reset --hard on shared branches
  • Use git stash to temporarily save work-in-progress

Frequently Asked Questions about Git

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

Use git reset --soft HEAD~1 to undo the last commit but keep all changes staged. Use git reset HEAD~1 (mixed mode, the default) to undo the commit and unstage changes while keeping them in your working directory. Only use git reset --hard HEAD~1 if you want to permanently discard all changes — this cannot be undone once done.

How do I delete a local and remote branch?

Delete a local branch with git branch -d branch-name (safe, requires the branch to be merged) or git branch -D branch-name (force delete regardless). Delete a remote branch with git push origin --delete branch-name. To clean up stale remote-tracking references locally, run git fetch --prune or git remote prune origin.

What is the difference between git merge and git rebase?

git merge creates a new merge commit joining two branches while preserving full history. git rebase replays your commits on top of another branch, producing a cleaner linear history. Use merge for public and shared branches — it is safe and non-destructive. Use rebase only on local feature branches before merging. Never rebase commits already pushed to a shared remote branch — it rewrites history and forces other contributors to reconcile diverged histories.

How do I see what changed in the last commit?

Use git show to see the diff of the most recent commit including commit message, author, date, and full patch. For a summary without the full diff, use git show --stat. To compare two specific commits, use git diff commit1 commit2. To see the history of a specific file, use git log --follow -p -- filename.

Related Developer Tools