Removing and Recovering Git Commits: Reset, Rebase, and Reflog

February 19, 2025

Removing a commit from a branch and erasing it from a repository are different operations. That distinction explains why a commit can disappear from git log after a reset yet remain recoverable through git reflog.

These notes began with a guide to deleting commits and a question about recovering them after a force push. Together, they describe a useful workflow: decide whether to undo changes or rewrite history, preserve a recovery point, and understand which local records remain available afterward.

Decide what you want to remove

GoalApproach
Undo the effect of a shared commitCreate a new commit with git revert.
Remove the latest commit from a local branchUse git reset, choosing how to handle its changes.
Omit a commit from the middle of a branchUse interactive rebase and drop.
Recover a previous branch tipLocate it in the reflog and create a recovery branch.
Remove a branch nameDelete the branch reference; its commits may remain reachable elsewhere.
Keep the current snapshot with a new historyCreate a new root commit, as an advanced history rewrite.

Before editing history, inspect the checkout:

git status
git log --oneline --graph --decorate -20

Preserve the current committed history with a branch:

git branch backup-before-rewrite

Choose an unused backup name. This preserves the current commit and its ancestors, not uncommitted file changes. Save those separately before destructive operations. The examples below assume a clean working tree and use topic as the branch being edited.

Undo a shared commit without removing history

If other people already use the branch, a new commit that reverses an earlier change is often the simplest option:

git revert COMMIT_ID

Replace COMMIT_ID with the commit to undo. The original commit remains in history, and the new commit records the reversal. Later changes can cause conflicts. Reverting a merge requires selecting a mainline parent and understanding its effect on future merges; it should not be treated as an ordinary single-parent revert.1

Undo last commit but keep changes with git reset

To undo the last local commit but keep its changes, use git reset --soft HEAD~1 to keep them staged, or git reset HEAD~1 to keep them unstaged. For a commit with a parent, the reset modes differ as follows:2

CommandResult
git reset --soft HEAD~1Move the branch back one commit; leave the index and working tree unchanged.
git reset HEAD~1Move back and reset the index; keep working-tree changes unstaged.
git reset --hard HEAD~1Move back and make the index and working tree match the target commit.

HEAD~1 follows the first parent; it is not a general way to choose which side of a merge to preserve. A root commit has no parent, so this expression cannot remove the repository's only commit.

Use --soft to revise a commit while keeping its changes staged. Use the default mixed reset to revise the staging selection. Use --hard only when you intend to discard working-tree changes as well. Untracked files or directories obstructing the target checkout can also be overwritten or removed.2

After a reset, the old commit may still exist through another reference or a reflog. Moving the branch is not immediate object deletion.

Remove a middle commit with interactive rebase

For a simple linear history, start the rebase at the parent of the commit you want to remove:

git rebase -i COMMIT_TO_REMOVE^

Here, COMMIT_TO_REMOVE identifies the unwanted commit itself. The ^ selects its parent. Supplying the preceding commit and then appending ^ would start one commit earlier than intended.

Change the unwanted line from pick to drop, then save the todo list. Git replays the remaining commits on the selected base. Later commits may depend on the removed change, so resolve conflicts and review the resulting files.3

# After resolving and staging a conflict:
git rebase --continue

# To abandon an in-progress rebase:
git rebase --abort

Use git rebase -i --root when the root must be included in the edit. For histories with merges, ordinary rebase does not preserve the original merge structure; --rebase-merges is a separate workflow requiring review.3

Recover a commit with reflog

Git's documentation gives the key definition:

“Reference logs, or "reflogs", record when the tips of branches and other references were updated in the local repository.”

— Git, git-reflog, “Description,” para. 2.4

Inspect the recent movements of HEAD, or a specific branch:

git reflog
git reflog show topic

Look for the commit before the unwanted reset or rebase. Copy its commit ID rather than relying on a reflog position that may change after more operations. Inspect the candidate and give it a branch name:

git show RECOVERED_COMMIT_ID
git branch recovered-work RECOVERED_COMMIT_ID

Creating this branch preserves a reference to the recovered history without moving the current branch or changing working files. Then choose the recovery method that fits the work done since the mistake.5

Restore the branch's previous position

If the recovered commit is the branch tip you want, preserve any newer work first, then restore the branch:

git switch topic
git branch backup-before-recovery
git reset --hard recovered-work

The reset replaces the index and working tree as described above. Pointing to an existing old commit also restores access to its parent history, including merge commits; it does not automatically recreate every other branch reference.2

Keep newer work and reapply one lost change

If the branch has useful new commits, reapply an ordinary lost commit instead:

git switch topic
git cherry-pick RECOVERED_COMMIT_ID

This applies that commit's change to the current history, normally producing a new commit ID. Recover a dependent sequence in the correct order; a merge commit needs a mainline-parent choice. Jonathan.Brink's original answer likewise distinguishes returning to an old position from cherry-picking after additional work.56

Understand the limits of recovery

The same discussion includes this important clarification:

“Yes, reflog is stored locally and will be unique to you”

— Jonathan.Brink (2022, October 4), comment on the recovery answer.7

A clone does not inherit another clone's reflog. If the needed commit was never present locally, inspect another clone or backup that might still contain it. Deleting a branch also deletes its branch reflog, although HEAD or other references may still provide a route to its former commits.48

Reflog records expire. Git's defaults are 90 days for ordinary entries and 30 days for entries unreachable from the current tip, subject to configuration. These are retention defaults, not guaranteed recovery deadlines.4

Once no retained references or reflog entries protect an object, garbage collection can eventually remove it. Avoid expiring reflogs or pruning objects while trying to recover work. Reflog also does not record arbitrary unsaved or uncommitted working-tree content.9

Update a remote branch after rewriting history

Rewriting a local branch does not change the remote branch. For an agreed history rewrite, use an explicit expected remote tip so that a concurrent update causes the push to fail:10

# Before rewriting, fetch and inspect the remote branch.
git fetch origin
git log --oneline --graph --decorate topic origin/topic
expected_tip=$(git rev-parse refs/remotes/origin/topic)

# Perform and review the intended local rewrite, then:
git push --force-with-lease=refs/heads/topic:"$expected_tip" origin topic:topic

Run the capture and push in the same Bash session. The lease requires the remote branch to still point to the recorded commit. It does not prove that your rewrite preserved every intended change or override server-side protection.

A plain --force lacks this comparison. The shorter --force-with-lease normally relies on remote-tracking references; background fetches can update those references and weaken that assumption. Record and review the expected tip explicitly when that distinction matters.10

After recovering a commit, first check whether an ordinary push is sufficient. Recovery does not inherently require another forced update.

Delete a branch without confusing it with object deletion

Switch to another branch before deleting topic:

git switch main
git branch -d topic

-d checks whether the branch is fully merged into its upstream, or into HEAD when no upstream is set. Use git branch -D topic only when deliberately bypassing that check. A branch checked out in another worktree cannot simply be deleted this way.8

Deleting a remote branch is a separate operation:

git push origin --delete topic

Commits reachable through other branches or tags remain part of those histories. Commits without such references may still be retained temporarily, but should not be treated as a permanent backup. Reflogs and garbage collection determine further opportunities for local recovery.89

Advanced: Replace a branch history with one root commit

The original draft included a command that discards a branch's ancestry while keeping its committed snapshot. A more inspectable version creates the new history under a separate branch first:

new_root=$(git commit-tree 'HEAD^{tree}' -m "Initial snapshot")
git branch replacement-history "$new_root"
git log --oneline replacement-history
git diff HEAD replacement-history

git commit-tree creates a commit from the current tree. Because no -p parent is supplied, it is a root commit. The diff should be empty because the file snapshot is unchanged. Uncommitted files are not included.11

Only after inspecting the result, and preserving the original history, would you replace the intended branch:

git switch topic
git reset --hard replacement-history

This creates a branch history containing one new commit, not an empty history. Other references, clones, and backups can still retain the old commits. Updating an existing remote branch requires the same coordinated rewrite process described above.

Prevent accidental history rewrites on GitHub

Protect shared branches against deletion and forced updates. GitHub branch protection disables force pushes and deletion by default for matching branches; review the rule's scope and any permitted bypasses.12

The original notes also describe an enterprise-wide policy. Those instructions are specifically for GitHub Enterprise Server 3.12, not the settings page of an ordinary GitHub.com repository:

  1. Open Enterprise settings → Policies → Options.
  2. Under Force pushes, choose whether to permit them, block them everywhere, or block them on the default branch.
  3. Use Enforce on all repositories when the policy must override organization and repository settings.13

The policy hierarchy runs from the enterprise to owners and repositories. Match the documentation to the deployed server version when applying these controls.

Removing history is not a secret-removal guarantee

The guide collected in the original draft makes this point explicitly:

“Once sensitive data has been committed to your repo you should immediately consider it compromised.”

— Foster, How to delete a commit from Git history, introductory note.14

If a credential has been exposed, revoke or rotate it before relying on history cleanup. Rewriting a branch cannot recall copies already fetched by collaborators or remove every server-side reference. Follow the hosting provider's dedicated sensitive-data removal process for that separate task.15

References

Footnotes

  1. Git. (n.d.). git-revert. Retrieved February 19, 2025, from https://git-scm.com/docs/git-revert ↩

  2. Git. (n.d.). git-reset. Retrieved February 19, 2025, from https://git-scm.com/docs/git-reset ↩ ↩2 ↩3

  3. Git. (n.d.). git-rebase. Retrieved February 19, 2025, from https://git-scm.com/docs/git-rebase ↩ ↩2

  4. Git. (n.d.). git-reflog. Retrieved February 19, 2025, from https://git-scm.com/docs/git-reflog ↩ ↩2 ↩3

  5. Jonathan.Brink. (2016, January 12). [Answer about recovering commits using reflog, reset, and cherry-pick]. Stack Overflow. https://stackoverflow.com/a/34751908 ↩ ↩2

  6. Git. (n.d.). git-cherry-pick. Retrieved February 19, 2025, from https://git-scm.com/docs/git-cherry-pick ↩

  7. Jonathan.Brink. (2022, October 4). [Comment explaining that reflog is local to each repository]. Stack Overflow, comments on the recovery answer. https://stackoverflow.com/a/34751908 ↩

  8. Git. (n.d.). git-branch. Retrieved February 19, 2025, from https://git-scm.com/docs/git-branch ↩ ↩2 ↩3

  9. Git. (n.d.). git-gc. Retrieved February 19, 2025, from https://git-scm.com/docs/git-gc ↩ ↩2

  10. Git. (n.d.). git-push. Retrieved February 19, 2025, from https://git-scm.com/docs/git-push ↩ ↩2

  11. Git. (n.d.). git-commit-tree. Retrieved February 19, 2025, from https://git-scm.com/docs/git-commit-tree ↩

  12. GitHub. (n.d.). About protected branches. GitHub Docs. Retrieved February 19, 2025, from https://docs.github.com/en/repositories/configuring-branches-and-merges-in-your-repository/managing-protected-branches/about-protected-branches ↩

  13. GitHub. (n.d.). Enforcing repository management policies in your enterprise: Blocking force pushes to all repositories. GitHub Enterprise Server 3.12 Docs. Retrieved February 19, 2025, from https://docs.github.com/en/[email protected]/admin/enforcing-policies/enforcing-policies-for-your-enterprise/enforcing-repository-management-policies-in-your-enterprise#blocking-force-pushes-to-all-repositories ↩

  14. Foster, G. (n.d.). How to delete a commit from Git history. Graphite. Retrieved February 19, 2025, from https://graphite.dev/guides/git-delete-commit-from-history ↩

  15. GitHub. (n.d.). Removing sensitive data from a repository. GitHub Docs. Retrieved February 19, 2025, from https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/removing-sensitive-data-from-a-repository ↩

© 2025 borui. All rights reserved. This content may be freely reproduced, displayed, modified, or distributed with proper attribution to borui and a link to the article: borui (2025-02-19). Removing and Recovering Git Commits: Reset, Rebase, and Reflog. https://caiborui.com/blog/2025-02-19-en-remove-and-recover-git-commits

@misc{borui2025-cb89d8d4,
  author = {borui},
  title = {{Removing and Recovering Git Commits: Reset, Rebase, and Reflog}},
  year = {2025},
  publisher = {borui's blog},
  url = {https://caiborui.com/blog/2025-02-19-en-remove-and-recover-git-commits}
}