How to Undo Complex Git Changes: Revert, Reset, Rebase, and File Tricks Explained
This article walks through four practical Git rollback strategies—revert, hard reset with force‑push, a combined rebase‑then‑revert approach, and a file‑copy method—detailing their pros, pitfalls, and step‑by‑step commands for handling tangled commit histories.
Basic Exploration
When a project is upgraded from version N to A, then B and C, a severe performance issue in a jar introduced at A forces a rollback while an urgent bug must still be fixed. The team decided to revert the code to a state before A and spent five hours exploring solutions.
revert
Using git revert commit_id creates a new commit that undoes the changes of commit_id. However, the commit history contains many merges, making a manual series of revert operations error‑prone, especially when identifying the correct mainline for merge commits.
reset
git reset --hard commit_idmoves HEAD to a previous commit, discarding later history. On a feature branch this caused merge conflicts with the develop branch, and a forced push to the protected master branch was not allowed, making reset unsuitable for preserving history.
master> git reset --hard commit_id
master> git push --force origin masterUpgrade Fusion (rebase)
The team discovered a workflow that combines git rebase -i N to squash multiple commits into one, then uses git revert on the squashed commit. This retains a relatively clean history while effectively undoing the unwanted changes.
Create a new branch F and identify the target commit N.
Run git rebase -i N and change subsequent commits to squash so they merge into the oldest commit.
After editing the commit message, the original commits are replaced by a single new commit.
Merge master into F to align histories, then revert the new commit to roll back all changes.
This method leverages rebase’s ability to rewrite history and Git’s automatic merge of identical changes.
File Operations
An alternative, simpler approach is to copy the project files to a backup folder, checkout the desired historical commit in the backup, then replace the current working directory’s files (excluding .git) with those from the backup and commit the changes. This avoids complex Git workflow manipulations.
Conclusion
Four rollback techniques were evaluated: simple revert for few clean commits, hard reset with force‑push when history can be overwritten, rebase‑plus‑revert for a formal, traceable solution, and direct file replacement for the quickest fix. Each has trade‑offs regarding safety, history preservation, and team workflow.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
