Practical Strategies for Reverting and Resetting Code with Git: Revert, Reset, Rebase, and File‑Based Rollback
This article walks through four practical Git techniques—revert, reset, rebase + revert, and a file‑copy method—to safely roll back code when a problematic jar package causes performance issues, comparing their trade‑offs and providing step‑by‑step command examples.
Introduction
The author shares a real‑world scenario where a project upgraded to version A introduced a jar with severe performance problems, and subsequent releases B and C inherited the issue. To fix an urgent bug while the jar remained problematic, a code rollback was needed.
Basic Exploration
revert
The git revert commit_id command creates a new commit that undoes the changes introduced by commit_id . However, the author found the commit history too complex (many merges) to apply a simple revert for dozens of commits.
reset
The git reset command moves the HEAD pointer to a previous commit, discarding later commits from the current branch. Using --hard on the master branch and force‑pushing would erase the unwanted history, but the protected branch policy prevented a force push.
master> git reset --hard commit_id
master> git push --force origin masterBecause force‑push was disallowed and the team wanted to preserve history, reset was not ideal.
Upgrade Fusion
rebase
The author discovered a workflow combining git rebase -i N (interactive rebase) to squash multiple commits into a single one, then revert that single commit. Steps include:
Create a new branch F and identify the target commit N .
Run git rebase -i N and replace pick with squash for later commits.
Edit the resulting commit message, then merge master into F to align histories.
Finally, revert the new combined commit on F to roll back the original changes.
If errors occur, git rebase --abort/--continue/--edit-todo can be used.
File Operations
An alternative, more straightforward method avoids complex Git history manipulation:
Create a branch F identical to master .
Copy the project folder to a backup bak and run git checkout N inside bak to restore files to the desired commit.
Copy all files from bak (except .git ) back into the original working directory.
Stage and commit the changes, effectively creating a reverse commit.
This leverages Git’s file‑level change detection without altering the workflow.
Conclusion
The author successfully rolled back the code using the file‑operation method after five hours of effort. The four approaches—revert, reset + force‑push, rebase + revert, and file copy—are summarized with their suitable use cases.
Java Captain
Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.
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.