ir2pid
ir2pid

Reputation: 6126

How to restore git history

One of our developers copied a file from one branch to another and rewrote the file's history. What is the best way to restore it.

   original__________________________________________________(commits d)____(commits e)___(no commit history for a, b, c in master)
    |                                            |
    |                                         (copied)
    |                                            |
    |                                            |
    |__(commits a)__(commits b)___(commits c)_____

Upvotes: 2

Views: 4446

Answers (1)

lukas-reineke
lukas-reineke

Reputation: 3322

Make a new branch from the original. This is your backup with every commit after the file was copied.

git checkout -b backup

Now reset original to the last commit before the file was copied over.

git checkout original && git reset SHA --hard  

Merge the branch with commits a-c into original.

git merge branchname

Check out your backup and rebase it

git checkout backup && git rebase original

You will have a merge conflict telling you 'both added x`, resolve this conflict and continue the rebase.

Now go back to original and merge the backup back

git checkout original && git merge backup

This should do it.

Upvotes: 2

Related Questions