mmgross
mmgross

Reputation: 3092

git: partially restore previous version of a file

In a project I'm currently working on, I have a file with roughly the following history:

  1. It started as a copy of a file from a different project
  2. Some changes where made
  3. Somewhere in between the majority of the file was deemed unnecessary and deleted
  4. After that, existing lines where modified and additionally new lines where created either by copying from the original file or as completely new lines that where not in the original file.

Now, I regret having deleted the vast majority of the file and I want to revert back to the initial file (some 100 commits ago) but with all modifications except the deletions since then.

In other words: Every line that exists in the latest commit should be taken from that, every line that's not in there should be taken from the first commit.

How can I achieve that?

Upvotes: 2

Views: 120

Answers (1)

matt
matt

Reputation: 536028

I would suggest using a third-party tool to show you the initial file and the current file side by side and just perform the fix manually. It's unlikely that there's any automatic solution; you could git checkout --merge the original file from that first commit, but if you're unlucky you might not get a merge conflict, and if you're lucky and you do get merge conflict it's unlikely that it would be a very good place to work from.

Upvotes: 1

Related Questions