Mastering Git Rollbacks: Revert, Reset, Rebase, and File‑Based Tricks
This guide walks through four practical Git rollback strategies—revert, reset, rebase + revert, and a file‑copy method—detailing when each works, the exact commands, and the pitfalls to avoid when fixing urgent bugs after problematic releases.
Background
After upgrading a project from version N to A, B, and C, a performance‑critical JAR introduced in version A caused severe issues. Versions B and C depended on A, so fixing the JAR required a rollback while an urgent bug also needed immediate attention.
1. Using git revert
The git revert commit_id command creates a new commit that undoes the changes of commit_id. It works well when only a few commits need to be undone and there are no merge conflicts.
In the described scenario the commit history was long and contained several merge commits. Reverting each commit in reverse order would require dozens of commands, and reverting a merge commit also needs the --mainline option to specify which parent line to keep, making the approach impractical.
2. Using git reset
git reset --hard commit_idmoves the HEAD pointer to a previous commit, discarding later commits from the history. After resetting, a forced push ( git push --force origin master) can overwrite the remote branch.
However, the master branch was protected in GitLab, preventing a force push. Even if forced, the history loss makes future debugging difficult, and recovering from a mistaken reset would require git reflog.
3. Combining git rebase and git revert
This method preserves history while allowing a bulk rollback.
Create a new branch F from the current tip and identify the target commit N to roll back to.
Run git rebase -i N to start an interactive rebase. The editor shows a list such as:
pick 6fa5869 commit1
pick 0b84ee7 commit2
pick 986c6c8 commit3
pick 91a0dcc commit4Change the first pick to keep the oldest commit and replace the subsequent pick entries with squash (or s) to merge them into a single commit.
After saving, edit the commit message to reflect the combined change. If needed, abort or continue the rebase with git rebase --abort, --continue, or --edit-todo.
The main branch now has commits older, commit1, commit2, commit3, commit4 while branch F has older, commit5. Because F is behind the main branch, merge the main branch into F with git merge master. Git detects that the changes are identical and creates a new commit5 without altering content.
Finally, run git revert commit5 on branch F to generate a single revert commit that effectively undoes the original series of commits.
This approach keeps a complete history while achieving a full rollback.
4. File‑Based Rollback
When command‑line tricks are too risky, a straightforward file‑level method works:
Create a new branch F that matches the current master exactly.
Copy the entire project directory to a backup folder bak. Inside bak, run git checkout N to restore the files to the desired historical state.
Copy all contents from bak back into the original project directory, excluding the .git folder.
Stage and commit the changes ( git add . && git commit -m "Rollback to N"), completing the revert without altering the commit graph.
This technique leverages Git’s file‑change detection without manipulating the branch history.
Conclusion and Recommendations
git revert– best for a small number of non‑conflicting commits. git reset --hard + forced push – useful when you can overwrite the remote and want the rollback to disappear from git log. rebase + revert – the “proper” way for complex histories, preserving all history while achieving a bulk rollback.
File‑copy method – the simplest, most direct approach when you don’t care about a tidy history.
Understanding these four strategies equips developers to handle urgent rollback scenarios safely and efficiently.
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.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
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.
