الانتقال إلى المحتوى
DevOps والـ CloudGit· 7 دقائق للقراءة

Undoing Commits Safely: git reset Rewrites History, git revert Records It

Reset moves your branch back in your clone only, so a teammate's next push restores the commit. Revert undoes it with a new commit everyone gets.

ReadIn

مقال كل يوم يفرق معاك

21 نوفمبر 2026
  • كاتب موثّق
محتويات المقال

Lina pushes a debug flag that gives every customer free shipping, and notices a few minutes later. She runs 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

The belief: 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.

It's an understandable belief, because reset really does undo commits. Pro Git's "Reset Demystified" chapter walks through 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.

The half-truth is in the word "your". Reset undoes the commit in your repository. It can't reach the copies that already left it.

What actually happens

Reset moves your branch

Pro Git is precise about it: "reset moves the branch that HEAD is pointing to." That's step one, and --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

A reset changes one pointer in one repository. The commit you "removed" still exists in every clone that pulled it, and their branches still point past it. That's why the 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

The records "when the tips of branches and other references were updated in the local repository", so a commit you reset away is still easy to find. On Git 2.54:

Bash terminal
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 field

The reflog is local and doesn't last forever: by default, git 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

Both sessions start from the same history: Lina pushed the flag, and Omar pulled it and committed his own work on top before anyone noticed.

Bash terminal
# 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

The first tab is the hook, reproduced. Omar's pull printed Already 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.

CommandMoves the branchWhat happens to your filesSafe after pushing
git reset --soft HEAD~1YesUnchanged; the commit's changes stay stagedNo
git reset HEAD~1 (--mixed)YesUnchanged; the changes are unstagedNo
git reset --hard HEAD~1YesOverwritten with the older commit's versionNo
git revert <commit>No, it adds a commitThe commit's changes are reversedYes

How to apply it in your project

  1. Ask whether the commit has left your machineRun git branch -r --contains <commit>. If it lists a remote branch such as origin/main, the commit is shared; if it prints nothing, only you have it.
  2. Shared: revert and push normallyRun git revert <commit> and push. Everyone gets the undo on their next pull, and nobody has to repair their clone.
  3. Local: pick the reset mode on purpose--soft to redo the last commit, the default --mixed to unstage, and --hard only when you really mean to discard your changes.
  4. Reset by mistake: check the reflog firstBefore you panic, run git reflog, find the entry from before the reset and git reset --hard back to it.

Key takeaways

  • git reset moves 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 revert adds 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.

📚 المصادر

  1. Git Tools: Reset Demystified — Pro Git
  2. git-revert — Git reference
  3. git-reset — Git reference
  4. git-reflog — Git reference
  5. git — Git reference (Reset, restore and revert)

التعليقات (0)