
Undoing Commits Safely: git reset Rewrites History, git revert Records It
- كاتب موثّق
محتويات المقال
git reset --hard HEAD~1, Git rejects her push, so she forces it, and the flag disappears from main. That afternoon Omar, who had pulled before the reset, pushes his coupon work as usual. The flag is back on main, and nobody put it there on purpose.
The common misconception
git reset is how you undo a commit, pushed or not. Move the branch back to before the mistake, push, and the mistake is gone for everyone.
git reset HEAD~ and points out that it "essentially undid the last git commit command." Afterwards your own git log looks perfect: the bad commit is simply not there.
What actually happens
Reset moves your branch
--soft stops there. The default, --mixed, also makes the match the new commit, and --hard goes on to overwrite your working files too. Git's own overview of the commands sums up what all three have in common: reset "is about updating your branch, moving the tip in order to add or remove commits from the branch. This operation changes the commit history."
Other clones still have the commit
git reset docs, right after an example that drops three commits with --hard, add: "Do not do this if you have already given these commits to somebody else." Removing a pushed commit also takes a , which the previous article in this path covered.
Revert records the undo as a new commit
git revert works the other way. Given existing commits, it will "revert the changes that the related patches introduce, and record some new commits that record them." Nothing is rewritten: the revert is an ordinary commit on top, so it pulls and pushes like any other, and the history shows both the mistake and its fix.
Locally, reset is safe, and the reflog has your back
git reset --hard HEAD~1# HEAD is now at e88fe03 Add debug free-shipping flaggit reflog -2# e88fe03 HEAD@{0}: reset: moving to HEAD~1# da7f63f HEAD@{1}: commit: Add coupon fieldgit reset --hard 'HEAD@{1}'# HEAD is now at da7f63f Add coupon fieldgit reflog expire drops entries after 90 days, or after 30 days when the branch's current tip can no longer reach them.
The fix: revert what's been pushed
# Linagit reset --hard HEAD~1# HEAD is now at f938e68 Add checkoutgit push --force # a plain push was rejected# + 156eb4a...f938e68 main -> main (forced update)# Omar, later that daygit pull --no-rebase# + 156eb4a...f938e68 main -> origin/main (forced update)# Already up to date.git push# f938e68..74ed234 main -> maingit log --oneline origin/main# 74ed234 Add coupon field# 156eb4a Add debug free-shipping flag# f938e68 Add checkout# Linagit revert --no-edit HEAD# [main a2dbf7d] Revert "Add debug free-shipping flag"# 1 file changed, 1 deletion(-)# delete mode 100644 flags.jsgit push# 45919d6..a2dbf7d main -> main# Omar, later that daygit pull --rebase# Successfully rebased and updated refs/heads/main.git log --oneline# 452ade0 Add coupon field# a2dbf7d Revert "Add debug free-shipping flag"# 45919d6 Add debug free-shipping flag# 1d03087 Add checkoutAlready up to date. because, from his clone's point of view, the remote had simply fallen behind: his main already contained f938e68. His next push was an ordinary fast-forward, and it carried 156eb4a back to main. In the second tab the flag's file is gone for both of them, the revert sits in the history next to the mistake, and nobody needed --force. Commit IDs differ between tabs because each scenario ran in a fresh repository.
git reset --soft HEAD~1 | |||
git reset HEAD~1 (--mixed) | |||
git reset --hard HEAD~1 | |||
git revert <commit> |
How to apply it in your project
Ask whether the commit has left your machine Run git branch -r --contains <commit>. If it lists a remote branch such asorigin/main, the commit is shared; if it prints nothing, only you have it.Shared: revert and push normally Run git revert <commit>and push. Everyone gets the undo on their next pull, and nobody has to repair their clone.Local: pick the reset mode on purpose --softto redo the last commit, the default--mixedto unstage, and--hardonly when you really mean to discard your changes.Reset by mistake: check the reflog first Before you panic, run git reflog, find the entry from before the reset andgit reset --hardback to it.
Key takeaways
git resetmoves your branch and changes history in your clone only.Every clone that pulled the commit still has it, and an ordinary push can bring it back. git revertadds a new commit that reverses the old one, so it's safe on shared branches.Locally, the reflog can bring back a commit you reset away, but nothing restores uncommitted work after --hard.Rule of thumb: pushed means revert, local means reset.
التعليقات (0)