1

I mistakenly merged one branch (a) to branch(b), now the files in a got deleted.

I don't have any tag for branch a, and made several commits previously.

How to roll back or back the stage before the merge was made, so I can have my files back?

I tried: git reset --soft HEAD~3 Seems it doesn't work.

Nick Volynkin
  • 14,023
  • 6
  • 43
  • 67
Zhao Peng
  • 1,741
  • 2
  • 13
  • 7
  • possible duplicate of [Undo a Git merge?](http://stackoverflow.com/questions/2389361/undo-a-git-merge) – Nick Volynkin May 21 '15 at 04:17
  • 1
    reset soft change only information about commits. Please try to use git reset --hard instead. It will rollback also files. – madoxdev May 21 '15 at 04:17
  • If you merged branch A into branch B, that means only B was modified. You make it sound like you care about the files in branch A, so what's the problem here? – Martin Konecny May 21 '15 at 04:21
  • Kamil, Nick, by using hard, I won't have much risk to lose more, right? – Zhao Peng May 21 '15 at 04:27
  • 1
    You can only lose uncommited data. Do a backup anyway. `git reset --hard` will change commit history, but old HEAD will be accessible with `git reflog`. – Nick Volynkin May 21 '15 at 04:29
  • Thanks, please make some comment, then I can give point to you, thanks. – Zhao Peng May 21 '15 at 05:30

1 Answers1

1

You have:

A --- B --- C - branch_a
 \           \
  --D--- E ---F - branch_b 

branch_a was merged to branch_b, but the content of branch_a is unchanged. You can do git checkout branch_a and the files on your file system will be as they were on branch_a before the merge.


If the merge of branch_a to branch_b (commit F) was wrong, you can roll it back, and set branch_b to where it was before the merge. git reset is the command of choice for moving HEADs (branches) around:

git checkout branch_b
git reset <SHA_1 of commit E> # --hard would update your local files as well, if you wish

resulting in:

A --- B --- C - branch_a
 \           
  --D--- E - branch_b 

Commit F has disappeared (you could still find it, at least for a while, but that is more advanced).

Gauthier
  • 40,309
  • 11
  • 63
  • 97