1

I want to undo some changes without removing them from the history, in a group-friendly way. Currently, I have this (* indicates master):

[rev 1] -- [rev 2] -- [rev 3] -- [rev 4] -- [rev 5] -- [*rev 6]

I want to get back to rev 2, but in a way that makes sense. I believe it should look like this:

[rev 1] -- [rev 2] -- [rev 3] -- [rev 4] -- [rev 5] -- [rev 6] -- [*rev 7]
               |                                                   |
               \---------------------------------------------------/

Where rev 7 is the merge of 6 and 2 (and the "merge" is really just "blindly keep rev 2"). No new heads are created. How do I do this?

Thanks in advance.

Robert Martin
  • 16,759
  • 15
  • 61
  • 87
  • Branch `rev 2` and then you can merge it back into `trunk (rev 6)` right away. – js1568 Aug 30 '11 at 18:40
  • I would think about why I wanted to make my project history misleading. Just create a branch for rev6 and reset master to rev 2. – antlersoft Aug 30 '11 at 18:45
  • @js1568 That leaves me with [rev 6] still considered 'master' and [rev 7] as the new branch. How do I convert [rev7] to be the master instead? – Robert Martin Aug 30 '11 at 18:50
  • @antlersoft good idea. Maybe I will end up doing that instead. But I'd still like to know how to do this in git... – Robert Martin Aug 30 '11 at 18:51
  • @antlersoft Ah. Here is the concern: If the new master doesn't have the old master as an ancestor, won't everyone else get screwed up? – Robert Martin Aug 30 '11 at 18:57

1 Answers1

4

You would

git branch temp
git reset --hard rev2
git reset --soft temp
git add -A
git commit -m "this commit makes all the changes so that the tree at rev7 looks like the tree at rev2"
git branch -d temp

There is a good post by Scott Chacon about the modifiers (hard, soft and mixed) on the reset command and others.

without a temp branch, you could:

git reset --hard rev2
git reset --soft HEAD@{1}
git add -A
git commit -m "this commit makes all the changes so that the tree at rev7 looks like the tree at rev2"

If you want a merge there, you could just:

git merge --no-ff -s ours rev2

(careful, this is different than the recursive strategy with the "ours" option)

user229044
  • 232,980
  • 40
  • 330
  • 338
Adam Dymitruk
  • 124,556
  • 26
  • 146
  • 141
  • Thanks. After-the-fact, I also found this post: http://stackoverflow.com/questions/2763006/change-the-current-branch-to-master-in-git which is related but has a different strategy (although somewhat different because he _needed_ to merge, unlike me) – Robert Martin Aug 30 '11 at 19:11