I have a git repository that looks like this:
A -> B -> C -> D -> HEAD
I want the head of the branch to point to A, i.e. I want B, C, D, and HEAD to disappear and I want head to be synonymous with A.
It sounds like I can either try to rebase (doesn't apply, since I've pushed changes in between), or revert. But how do I revert multiple commits? Do I revert one at a time? Is the order important?
Similar to Jakub's answer, this allows you to easily select consecutive commits to revert.
This is an expansion of one of the solutions provided in Jakub's answer
I was faced with a situation where the commits I needed to roll back were somewhat complex, with several of the commits being merge commits, and I needed to avoid rewriting history. I was not able to use a series of
git revert
commands because I eventually ran into conflicts between the reversion changes being added. I ended up using the following steps.First, check out the contents of the target commit while leaving HEAD at the tip of the branch:
(The -- makes sure
<target-commit>
is interpreted as a commit rather than a file; the . refers to the current directory.)Then, determine what files were added in the commits being rolled back, and thus need to be deleted:
Files that were added should show up with an "A" at the beginning of the line, and there should be no other differences. Now, if any files need to be removed, stage these files for removal:
Finally, commit the reversion:
If desired, make sure that we're back to the desired state:
There should be no differences.
The easy way to revert a group of commits on shared repository (that people use and you want to preserve the history) is to use
git revert
in conjunction with gitrev-list
. The latter one will provide you with a list of commits, the former will do the revert itself.There two ways to do that. If you want the revert multiple commits in a single commit use:
this will revert a group of commits you need, but leave all the changes on your working tree, you should commit them all as usual.
Another option is to have a single commit per reverted change:
For instance, if you have a commit tree like
to revert the changes from eee to bbb, run
Expanding what I wrote in a comment
The general rule is that you should not rewrite (change) history that you have published, because somebody might have based their work on it. If you rewrite (change) history, you would make problems with merging their changes and with updating for them.
So the solution is to create a new commit which reverts changes that you want to get rid of. You can do this using git revert command.
You have the following situation:
(arrows here refers to the direction of the pointer: the "parent" reference in the case of commits, the top commit in the case of branch head (branch ref), and the name of branch in the case of HEAD reference).
What you need to create is the following:
where "[(BCD)^-1]" means the commit that reverts changes in commits B, C, D. Mathematics tells us that (BCD)^-1 = D^-1 C^-1 B^-1, so you can get the required situation using the following commands:
Alternate solution would be to checkout contents of commit A, and commit this state:
Then you would have the following situation:
The commit A' has the same contents as commit A, but is a different commit (commit message, parents, commit date).
The solution by Jeff Ferland, modified by Charles Bailey builds upon the same idea, but uses git reset:
First be sure that your working copy is not modified. Then:
and then just commit. Don't forget to document what's the reason for revert.
I'm so frustrated that this question can't just be answered. Every other question is in relation to how to revert correctly and preserve history. This question says "I want the head of the branch to point to A, i.e. I want B, C, D, and HEAD to disappear and I want head to be synonymous with A."
I learned a lot reading Jakub's post, but some guy in the company (with access to push to our "testing" branch without Pull-Request) pushed like 5 bad commits trying to fix and fix and fix a mistake he made 5 commits ago. Not only that, but one or two Pull Requests were accepted, which were now bad. So forget it, I found the last good commit (abc1234) and just ran the basic script:
I told the other 5 guys working in this repo that they better make note of their changes for the last few hours and Wipe/Re-Branch from the latest testing. End of the story.