Remove local Git commits before pushing

Inspect which commits differ from the intended upstream, choose how reset handles their changes, and preserve a recovery point before rewriting.

Best practices
Illustration of Git branches and version history

Use git reset to move your current branch to an earlier commit. Choose the mode based on whether you want to preserve, unstage, or discard the associated changes. Confirm that the history is yours to rewrite; “not on this upstream” does not prove no one else has received it.

Inspect the branch first

git status
git fetch origin
git log origin/main..HEAD --oneline
git branch backup-before-reset

Replace origin/main with the intended comparison branch. The log lists commits reachable from HEAD but not that fetched ref. It does not tell you whether they were sent to another remote, shared as patches, or used in another local branch. With a configured upstream, git log '@{u}..HEAD' --oneline is a convenient equivalent comparison.

The backup branch retains committed history. Stash, commit elsewhere, or externally copy uncommitted work you need to preserve.

Choose one reset mode

Command Result
git reset --soft HEAD~1 Move HEAD; leave index and working files as they are
git reset --mixed HEAD~1 Move HEAD and reset index; preserve working files
git reset --hard HEAD~1 Move HEAD and reset index and tracked working files

These are alternatives, not a sequence. Soft reset usually leaves the removed commit's changes staged, but it also retains any pre-existing index state. Mixed reset leaves differences unstaged, and newly introduced files can become untracked. Hard reset discards local tracked edits and can remove untracked paths that block writing the target tree.

Reset to a specific commit

git log --oneline
git reset --mixed a1b2c3d

Choose the target you want to retain. Replace --mixed only after deciding whether another mode is appropriate. HEAD~N follows first parents; after merges it is not necessarily “exactly N commits” in the full graph. The root commit has no parent to reset to.

Recover a mistake

git reflog
git branch recovery OLD_TIP_ID

Inspect the recovery branch before moving your current branch again. Reflogs and unreachable objects are retained only according to repository policy and cleanup, so recovery is not guaranteed forever. Git cannot generally recover never-stored working-tree edits overwritten by a hard reset.

FAQ

Can I just amend the latest commit? Yes. Use amend to add intentionally staged changes or edit its message. For message-only edits that should exclude current staging, use git commit --amend --only.

What if the commits were already published? Revert usually fits shared history. An agreed rewrite may be possible with a carefully reviewed lease-protected push, but account for collaborators and branch rules first. A local reset alone does not alter any server ref.

See git-reset, git-reflog, and revision syntax.

Get a tailored plan

Contact us

Join the community

Scan with WeChat
to join the community

Try Guance

Start online and pay only for what you use.

Get started

Choose a Guance plan

Code hosting