git revert undoes the changes introduced by a specific commit by creating a brand-new commit that applies the inverse of those changes, leaving the original commit intact in history.
Key Points: • Unlike git reset, revert never rewrites existing history, which makes it the safe choice for commits already pushed to a shared branch. • Reverting a merge commit requires the -m flag to specify which parent to treat as the mainline. • If the revert conflicts with later changes, Git will pause and ask you to resolve the conflicts manually before completing the commit. • Reverts show up clearly in the log, giving a full audit trail of what was undone and why. • A revert can itself be reverted later if the original change turns out to be needed after all.
Example: If commit a1b2c3d introduced a bug, running git revert a1b2c3d creates a new commit that removes exactly those changes, and the team can push it immediately without disturbing anyone else's history.
Code Example:
git revert a1b2c3d
git push origin mainInterview Tip: A concise interview answer is:
"git revert creates a new commit that undoes the changes from a specific commit, without altering existing history. That makes it safe for shared branches, unlike git reset, which rewrites history and can cause problems for anyone who already pulled those commits."