How to safely revert changes in team collaboration?
Reverting changes in team collaboration requires caution — an incorrect revert can overwrite someone else's work or create conflicts for the entire team. Here are safe approaches.
git revert — the safest method. Creates a new commit that undoes the changes of a specified commit. History is not rewritten, so it's safe for shared branches. The command git revert <commit-hash> reverts one commit. You can revert a range: git revert HEAD~3..HEAD creates three revert commits. If revert causes conflicts (surrounding code has changed), resolve them as with a regular merge.
git reset — rewrites history, use only for local branches or branches no one else uses. git reset --hard <commit> moves the branch pointer back and removes all changes after that commit. Never do reset --hard on branches pushed to remote if other developers work on them — it creates divergences.
git revert vs git reset: revert adds a new undo commit — safe for everyone. reset removes commits from history — safe only locally. Rule: if a branch is in remote and someone might have pulled — use revert. If the branch is local or you're the only developer — reset is acceptable.
Reverting a push: if you've already pushed unwanted commits, use git revert, then git push. If you need to remove commits from remote (e.g., accidentally pushed secrets), do git reset --hard <last-good-commit>, then git push --force-with-lease. Warn the team — they'll need to git fetch and git reset --hard origin/<branch> because their local history no longer matches.
Recovering accidentally deleted commits: git reflog shows the history of HEAD movements. Find the hash of the needed commit and run git reset --hard <hash> — the commit is restored. Reflog keeps history for 90 days by default.
Golden rule: when in doubt — use revert, not reset. Revert is always safe because it adds, not removes.