Fundamentals 10 min read

Practical Guide to Reverting, Resetting, and Rebasing for Git Code Rollback

This article walks through a real‑world scenario where a team needed to roll back a problematic jar introduced in an earlier release, comparing four Git‑based rollback strategies—revert, reset, rebase + revert, and a pure file‑operation method—highlighting their commands, pitfalls, and suitability.

Top Architect
Top Architect
Top Architect
Practical Guide to Reverting, Resetting, and Rebasing for Git Code Rollback

Basic Exploration

The author describes a situation where version A introduced a jar with severe performance issues, later versions B and C were built on top of A, and an urgent bug required a quick fix while still needing to revert the problematic jar.

revert

Using git revert <commit_id> creates a new commit that undoes the changes of the specified commit, but the author found the commit history too long and full of merge commits, making a manual, ordered series of revert operations error‑prone.

reset

git reset --hard <commit_id> moves the HEAD pointer to a previous commit, discarding later history. However, because feature branches lag behind develop , merging a reset feature branch back caused conflicts, and the protected master branch prevented a forced push.

master> git reset --hard commit_id
master> git push --force origin master

Upgrade Fusion

rebase

The author discovered a workflow that first squashes several commits into one using interactive rebase, then reverts that single commit. This preserves history while achieving a clean rollback.

Steps:

Create a new branch F and locate the target commit N with git log .

Run git rebase -i N to open the interactive editor.

pick 6fa5869 commit1
pick 0b84ee7 commit2
pick 986c6c8 commit3
pick 91a0dcc commit4

Change the later lines to squash (or s ) so that all commits are merged into the oldest one.

After saving, edit the commit message; the rebase creates a new combined commit (e.g., commit5 ).

Merge master into F to keep the branch up‑to‑date, then run git revert commit5 to roll back the entire series.

This method leverages Git’s ability to rewrite history and automatically merge identical changes.

File Operations

An alternative, less “elegant” approach avoids complex Git workflow:

Create a branch F identical to master .

Copy the project directory to a temporary folder bak and run git checkout N inside bak to restore the desired historical state.

Copy all files (except .git ) from bak back to the original working directory; Git detects the changes as file modifications.

Stage and commit the changes, completing the rollback.

Conclusion

The author finally succeeded using the file‑operation method and summarizes the four rollback techniques:

revert : suitable for a small number of clean commits without merge conflicts.

reset + force‑push : works when you can overwrite the protected branch and want no trace in git log .

rebase + revert : a “proper” way that keeps history tidy but requires careful branch handling.

file operation : the simplest, most direct method when elegance is not a priority.

Git is portrayed as a powerful, flexible tool that, with a handful of commands, can meet diverse development needs.

software developmentgitrebaseVersion Controlresetrevertcode-rollback
Top Architect
Written by

Top Architect

Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.