0

I royally messed up the git repository of one of my projects and need help fixing it.

This is what I wanted:

alpha      E--
          /
master --A--
         |\
beta     | B--C--
          \
gamma      D--

Four branches total, 3 unstable branches and a master.

Somehow, however, I created an accidental merge, then another bad merge when I tried to undo it. So now my repository looks like this:

master --A--E--------Y
         |          / \
beta     |   X--B--C   Z--C'
          \ / 
gamma      D--B?--C?

With no alpha branch at all (somehow, even though I definitely did a git checkout -b alpha), with the beta branch in a state of very weird reversion, and with the gamma branch having the commits of the beta branch on it without any apparent merge.

Luckily I know exactly what each file should look like on each branch and I can rewrite history on the remote for a couple more weeks before my team returns to action.

How can I make my 4 separate branches instead of my merged mess without losing work?

LambdaBeta
  • 1,479
  • 1
  • 13
  • 25

1 Answers1

2

Assuming the example commit identifiers exactly reflect the real arrangement of your commits:

From a starting point:

    master --A--E--------Y
             |          / \
    beta     |   X--B--C   Z--C'
              \ / 
    gamma      D--B?--C?

1.

git checkout master
git reset --hard A

    master --A
             |\-E--------Y
             |          / \
    beta     |   X--B--C   Z--C'
              \ / 
    gamma      D--B?--C?

2.

git checkout gamma
git reset --hard D

    master --A
             |\-E--------Y
             |          / \
    beta     |   X--B--C   Z--C'
              \ / 
    gamma      D

3.

git checkout -b alpha
git reset --hard E

    master --A
    alpha    |\-E
             |   --------Y
             |          / \
    beta     |   X--B--C   Z--C'
              \ / 
    gamma      D

4.

git checkout beta
git reset --hard C

    alpha      E
              /
    master --A
              \
    gamma      D
                \
    beta         X--B--C

git rebase --onto master X C

    alpha      E--
              /
    master --A--
             |\
    beta     | B"--C"--
              \
    gamma      D--
bames53
  • 86,085
  • 15
  • 179
  • 244
  • That looks like it should work. After I rebase though, how do I get my new branch structure to be reflected by my remote? – LambdaBeta Nov 30 '15 at 21:17
  • 1
    @LambdaBeta You do a force push. `git push --force origin alpha master gamma beta`. Make sure no one else has fetched/pulled any of the commits or branches you're messing with. – bames53 Nov 30 '15 at 21:19