Skip to content

Mindcomb · Coding

Git: undo a pushed commit

Curated by Mindcomb

Prompt

I pushed a commit to a shared branch that I need to undo. Compare git revert and git reset for this situation, and give the exact commands for the safe option.

Mindcomb ran and reviewed this comparison for the public library. It was not saved by a community member.

Prompt source
Mindcomb
License
Mindcomb original
Retrieved
Fork this combTry this promptNo winner picked · recorded cost $0.03636
Compare answers word by word

Community preferences

Which answer would you choose? Your preference is separate from the author's pick and appreciation.

Loading community preferences…

Claude Sonnet 5.5

Revert vs. Reset on a Shared Branch

Comparison

git revertgit reset
What it doesAdds a new commit that inverses the bad commit's changesMoves the branch pointer backward, discarding commits from history
HistoryPreserved; the bad commit and its undo both appearRewritten; the commit disappears
PushingNormal git pushRequires git push --force, since your local history diverges from the remote
Effect on teammatesNone. They just pull the new commitDisruptive. Anyone who pulled the bad commit has history that conflicts with the remote, and they can accidentally re-push it
RiskVery lowHigh on shared branches (lost work, duplicate commits, messy merges)

Use git revert for a shared branch. Reset is fine for commits that exist only on your machine, but once others may have pulled the commit, rewriting history causes problems for them.

Commands for the Safe Option (Revert)

  1. Make sure you're on the right branch and up to date:

    git checkout your-branch
    git pull
  2. Find the commit hash:

    git log --oneline
  3. Revert the commit:

    git revert <commit-hash>

    This opens your editor with a default message like Revert "original message". Save and close to finish.

    To skip the editor:

    git revert --no-edit <commit-hash>
  4. Push normally:

    git push

Special Cases

  • Reverting multiple commits (oldest to newest, inclusive):

    git revert --no-edit <oldest-hash>^..<newest-hash>
  • Reverting a merge commit (you must specify which parent to keep, usually 1 for the main branch):

    git revert -m 1 <merge-commit-hash>
  • Conflicts during revert: Resolve the files, then:

    git add <files>
    git revert --continue

    Or back out with git revert --abort.

One Caveat

If the commit contained a secret (password, API key), reverting does not remove it from history. In that case, rotate the credential immediately, and consider a history rewrite only with your team's coordination.

Server-recorded answer

Model: claude-sonnet-5-5

6.5s · 912 tok · $0.008672

Gemini 4 Argon

Since you have already pushed the commit to a shared branch, the golden rule of Git applies: do not rewrite history that others may have already pulled.

Here is how git revert and git reset compare in this scenario, followed by the exact commands you should use.


Comparison: git revert vs. git reset

1. git revert (The Safe Option)

  • What it does: It creates a brand-new commit that applies the exact opposite changes of the commit you want to undo.
  • Effect on history: It moves history forward. The bad commit remains in the Git history, followed by the new "undo" commit.
  • Why use it on a shared branch: Because it doesn't delete or alter existing commits, anyone else pulling from the branch will simply get your new revert commit without any merge conflicts or broken local repositories.

2. git reset (The Dangerous Option)

  • What it does: It moves the branch pointer backward to an older commit, effectively erasing the bad commit from the branch's history.
  • Effect on history: It rewrites history. To update the remote branch, you must use git push --force.
  • Why avoid it on a shared branch: If a teammate has already pulled the branch containing your bad commit, their local history will diverge from the remote history you just rewrote. When they try to push or pull, it will create a mess of duplicate commits, merge conflicts, and risk accidentally re-introducing the bad commit you tried to delete. (Only use reset for local, unpushed commits, or on a personal feature branch no one else uses).

Exact Commands for the Safe Option (git revert)

Scenario A: The bad commit is the most recent commit on the branch

If the commit you want to undo is at the very top of the branch (HEAD), run:

# 1. Make sure your local branch is up to date
git pull

# 2. Revert the most recent commit (this will open your text editor to confirm the commit message)
git revert HEAD

# 3. Push the new "undo" commit to the shared branch
git push

(Tip: If you want to skip the text editor opening in step 2 and just accept the default "Revert..." commit message, run git revert HEAD --no-edit instead).

Scenario B: Other commits have been pushed after your bad commit

If teammates have already pushed new commits on top of your bad commit, you need to specify the exact commit hash to undo:

# 1. Find the hash of the commit you want to undo (e.g., a1b2c3d)
git log --oneline

# 2. Revert that specific commit
git revert <commit-hash>

# 3. Push the new commit to the shared branch
git push

Note on Merge Commits

If the commit you pushed by mistake was a merge commit (merging one branch into another), Git doesn't know which branch was the "mainline" to keep. You will need to add the -m 1 flag to tell Git to keep the parent branch you merged into:

git revert -m 1 <merge-commit-hash>
git push

Server-recorded answer

Model: gemini-4-argon

11.6s · 795 tok · $0.00743

GPT-5.5

For a shared branch, the safe option is git revert, not git reset.

git revert vs git reset

git revert — safe for shared branches

  • Creates a new commit that undoes the changes from the bad commit.
  • Does not rewrite history.
  • Other people who already pulled the branch will not have problems.
  • This is the recommended option for commits already pushed to a shared branch.

git reset — unsafe for shared branches

  • Moves the branch pointer backward, removing commits from the branch history.
  • Requires a force push if the commit was already pushed.
  • Can break other people’s local branches if they already pulled the commit.
  • Usually only appropriate for local/unpushed commits or carefully coordinated history rewrites.

Safe commands using git revert

Replace shared-branch with your branch name and <commit-sha> with the commit you want to undo.

git checkout shared-branch
git pull --ff-only origin shared-branch
git log --oneline
git revert <commit-sha>
git push origin shared-branch

If the commit you want to undo is the most recent commit, you can use:

git checkout shared-branch
git pull --ff-only origin shared-branch
git revert HEAD
git push origin shared-branch

If there are conflicts during the revert:

# fix the conflicted files
git add <fixed-files>
git revert --continue
git push origin shared-branch

So for a pushed commit on a shared branch, use:

git revert <commit-sha>
git push origin shared-branch

Server-recorded answer

Model: gpt-5.5-2026-04-23

7.1s · 530 tok · $0.0149

Grok 4.7

Use git revert. On a shared branch, it is the safe option because it does not rewrite history that others may already have.

Comparison

git revertgit reset
What it doesAdds a new commit that undoes the changesMoves the branch pointer backward and drops commits from history
Shared branchSafe — history stays the same for everyoneUnsafe — requires a force-push and can break other people’s clones
Already pushed?FineAvoid it unless everyone agrees to rewrite history

git reset (especially --hard plus git push --force) rewrites commits others may have based work on. That is the wrong tool here.

Safe commands

Undo the latest commit on the branch and push the undo:

git revert HEAD
git push

Undo a specific older commit (leave later commits in place):

git revert <commit-hash>
git push

If that commit is a merge, use:

git revert -m 1 <merge-commit-hash>
git push

-m 1 keeps the first parent (usually the branch you merged into). Git will open an editor for the revert message; save and close it to finish.

Server-recorded answer

Model: grok-4.7

6.7s · 1,744 tok · $0.00536