3

Is it possible to revert all changes made to a specific branch without losing history?

I branched master a long time ago to hide specific features but now this branch has to become the same as master but I need the branch history intact.

master -> branched -> (revert stripping) -> branched = master

The branched should now reflect master although I want a commit in branched with all the changes that was needed to get to master.

The easiest way I can think of is to copy the sources of master and delete/paste files in branched but my worry is if this will generate conflicts when later merging from master?

Daniel
  • 3,726
  • 4
  • 26
  • 49

2 Answers2

1

Edit:

The solution from Preuk does not work for me (Git 2.2.1), but those commands work:

$ git checkout branched
$ git revert -n master..branched
$ git commit -m "Revert to master"

Initial post:

I would suggest to use git format-patch followed by git am on a dedicated branch, squash the commits with git rebase -i, and then apply the result on the branch branched:

  1. Create new branch starting from branched:

    $ git checkout branched
    $ git checkout -b tmpbranch
    
  2. Apply the reversed commits between master and branched:

    $ git format-patch -R master..branched --stdout | git am
    
  3. Squash the commits into a single one:

    $ git rebase -i branched
    $ # Use the squash command for all commits except the first one
    $ # and set the log message to "Revert to master" for instance.
    
  4. Then merge and delete the temporary branch tmpbranch onto branched:

    $ git checkout branched
    $ git merge tmpbranch
    $ git branch -d tmpbranch
    
Frodon
  • 3,684
  • 1
  • 16
  • 33
  • This seems to be what I'm looking for but it keeps telling me that "-m" flag is missing. How do I get the parent number accordingly to http://git-scm.com/docs/git-revert? It wont let me use "1" which I understood as the first given branch. – Daniel Apr 24 '15 at 09:02
0

You could try reverting and rebasing from master in squash mode.

First, you would need to revert commit specific to your branch from its inception. To do so, use something like this :

git revert OLDER_COMMIT^..NEWER_COMMIT

You would then get 1 commit on your satellite branch with all of the changes from master since branch split.

git checkout topicbranch
git rebase -i master

and select squash for every commit.

Community
  • 1
  • 1
Preuk
  • 632
  • 7
  • 18