'Fixing previous merge overwritten files after commits [closed]

So I identified that at some point, when I've merged a feature branch into base branch, that I discarded some changes and from then many other commits and modifications have been added to the base branch.

Problem is:

There are 3 current files that are missing some code from an old commit that is important

The current 3 files have been modified over other commits

What I want to do:

I want to basically merge the HEAD (last commit) with the initial commit before descarding some pieces of code from the files.

What I tried: check out to the commit's

Created 2 branches 1 called initalCommit, second called latestCommit... but when I merge, the files get overwritten with the contents of latestCommit, when it should have promted me with conficts errors, but that does not happen

Any idea how I should go about this?



Solution 1:[1]

You can do something close to a "revert only a set of given files" :

  1. create a patch, which describes how to revert changes only to some selected files
  2. apply that patch on top of your current commit

with a diagram :

   # the merge with the bad things
            |
            v
...*--*--*--m--*--*--*--n <- master
           /
 ... --*--g
          ^
          |
    # the commit with the good things

You can view the patch to go from g to m using : git diff g m,
you can have the patch for the reverse (going from m back to g) using : git diff m g.

So you can try :

  1. # create a revert patch only for selected files :
    git diff m g -- file1 file2 file3 > mypatch.txt
    
  2. # apply that patch to the current HEAD :
    git apply mypatch.txt
    

You may even open mypatch.txt in a text editor after step 1 if you know that you want to keep only a few selected chunks in the diff and completely delete the other ones.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1