Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A way to revert a whole branch in a single commit

Tags:

git

git-revert

I need to revert a whole branch with a single commit like this:

          [topic]
     o---o---o-------X
    /
   |      [master]
---o---o---o

The commit X must have state like a master~2 (the origin point of topic branch)

My solution is:

git rev-list --reverse master..topic | while read SHA; do
   git revert -n ${SHA}
done
git commit -m "Reverted topic branch"

Is there a better (shorter) solution?

A PURPOSE OF THIS

Imagine I have a kind of almost-git-flow based repo.

              [release-3]
       o---o---o---o
      /         \       [fix-for-rc-3]
     /           o---o---o
    /
   |      [development]
---o---o---o

I have a development branch, upcoming release-3 branch and a so-called hot-fix-feature-for-rc-3 branch. In this branch I'm doing some ugly hack to get my release done but I don't want it in a development at all because more "correct" solution is already landed here but can't be applied to a release-3 branch by some reason. So, I must do the following...

                                    [release-3]
       o---o---o---o--------------------M
      /         \       [fix-for-rc-3] /
     /           o---o---o----------------X
    /                                      \
   |                                        \[development]
---o---o---o---------------------------------D

I must merge fix-for-rc-3 to a release-3 (point M), then do a "revert-all-this-shit" commit (point X) and merge it do development (point D) so this code will never get here, even then a whole release-3 branch is merged to development then release is done.

That's why I need to revert a whole branch...

THE PROBLEM

While the root issue is solved, there are still problem getting fork point of a branch cause' if topic is already merged to release merge-base will fail.

like image 660
Olegas Avatar asked Oct 14 '13 17:10

Olegas


People also ask

How do I revert a git branch?

Use the git switch - (Or git checkout - ) to switch to the previous branch you were working with. This is pretty similar to the cd - command, which is used to switch to the previous directory.


1 Answers

You can create a commit that reverts to the base of the branch like this:

# on "topic" branch
git read-tree $(git merge-base topic master)
git commit -m "Reverted topic branch"
git checkout -- . # update working copy to match the committed tree

In your example use, you want to revert the changes so that you can merge the branch back into master (or development as it's called in your example) without actually incorporating any of the changes from the topic branch. But that can be done without adding a revert commit by using the "ours" merge strategy. From the git-merge documentation:

ours

This resolves any number of heads, but the resulting tree of the merge is always that of the current branch head, effectively ignoring all changes from all other branches. It is meant to be used to supersede old development history of side branches. Note that this is different from the -Xours option to the recursive merge strategy.

For example:

git checkout master
git merge -s ours topic
like image 185
John Bartholomew Avatar answered Sep 23 '22 23:09

John Bartholomew