Fundamentals 9 min read

Practical Strategies for Reverting and Resetting Code with Git

This article walks through four practical Git techniques—revert, reset, rebase + revert, and a file‑copy method—to safely roll back code when faced with performance‑critical bugs and complex version histories, while preserving commit history for future debugging.

Architecture Digest
Architecture Digest
Architecture Digest
Practical Strategies for Reverting and Resetting Code with Git

Introduction

The author shares a real‑world scenario where a performance‑critical jar introduced in version A caused problems in later releases, forcing a need to roll back code while an urgent bug also required a quick fix.

Basic Exploration

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 tangled (many merges) to apply revert repeatedly.

reset

git reset moves the HEAD pointer to a previous commit, erasing later commits from the history; however, resetting a feature branch caused merge conflicts with develop, and protected branches prevented a forced push.

Using git reset --hard commit_id on master and then git push --force would discard old code, but this is risky on protected branches.

Upgrade Fusion

rebase

The author proposes combining git rebase -i N (interactive rebase) to squash multiple commits into one, then performing a single revert on the squashed commit. This preserves a cleaner history while achieving the same rollback effect.

Steps include creating a new branch, running git rebase -i N , editing the todo list (using pick and squash ), handling abort/continue, merging back to master, and finally reverting the combined commit.

File‑operation method

An alternative approach avoids complex Git workflows: copy the project folder to a backup, checkout the target commit with git checkout N to restore files, copy everything except .git back to the original directory, then add and commit the changes.

Conclusion

The author successfully rolled back the code using the file‑operation method after five hours of effort and summarizes the four strategies:

revert – suitable for few, non‑conflicting commits.

reset + force‑push – works when you can overwrite the protected branch.

rebase + revert – a formal, history‑preserving solution.

file‑copy – the simplest, least elegant but effective method.

Git remains a powerful tool that, with the right combination of commands, can handle even complex rollback scenarios.

workflowgitrebaseVersion Controlcode managementresetrevert
Architecture Digest
Written by

Architecture Digest

Focusing on Java backend development, covering application architecture from top-tier internet companies (high availability, high performance, high stability), big data, machine learning, Java architecture, and other popular fields.

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.