Rewriting History

8

questions

How do you recover a commit after git reset –hard?

git reflog shows every move of HEAD with hashes, even for commits no longer in the branch. I find the right entry and go back: git reset --hard <hash>, or create a branch git branch rescue <hash>. The reflog is local and kept for about 90 days by default.

How do you roll back a bad release in main?

git revert the problematic commit (for a merge commit — git revert -m 1 <hash>), push, and ship a new release. In parallel — a quick deploy rollback to the previous tag. No reset and force push on main.

What is cherry-pick for?

To move one specific commit into another branch without merging the whole branch — for example a hotfix from main into a release branch. It creates a new commit with the same changes and a different hash. For many commits, merge or rebase is better.

What's the difference between merge and rebase?

Merge joins branches with a merge commit and keeps history as it was. Rebase moves your commits on top of another branch, creating new commits with new hashes — history becomes linear. The resulting code is the same; the difference is the history.

What's the difference between reset and revert?

git reset moves the branch back and removes commits from history — it rewrites it, for local changes. git revert creates a new commit that undoes an old one; history isn't rewritten, so it's safe for shared branches.

When do you use git stash?

When I need to switch to another task urgently and the current changes aren't ready to commit: git stash push -m "description", switch, come back, git stash pop. Also before pull if local changes get in the way.

When must you not rebase?

On branches others work on, and for commits already in a shared branch (main, develop). Rebase changes hashes, and your teammates' history diverges. Rebase is for your own branch before merging.

Which operations must not be done on shared branches?

Anything that rewrites history: rebase, reset of pushed commits, commit --amend, push --force. On shared branches fix things with git revert and new commits.