Home > Blockchain >  Merge branches but don't keep old files
Merge branches but don't keep old files

Time:01-08

I want to merge two branches but not keep the old branches files.

These are two different versions of a framework and it will be a headache if they actually merge. I need to do this because I need to preserve the commit history of the main branch. This is because I just started a new project and then made it a branch after I fixed all the depreciated things, so most of the commits are on the main branch and not the newer one.

edit: i thought of deleting all the files on the main branch and committing an empty folder then merging, but there must be a cleaner way.

CodePudding user response:

It sounds like you want to perform a fake-merge: keeping the history, but ignoring any changes of "theirs" (the other branch). This can be achieved with merge strategy "ours":

git checkout main
git merge -s ours your_branch

This will create a merge commit – so all history is kept – but the tree of the merge commit is the same as the tree of "ours", i.e. the target branch, i.e. "main" in your case. No actual merge will be performed, it simply takes the same tree snapshot, but creates a new commit with two parents.

    T1 T2 T3 T3
    |  |  |  |
...-C1-C2-C3-M
      \     /
       C4-C5
       |  |
       T4 T5

Legend:
  C?: commit
  T?: tree
  M: merge commit

This is very similar to overwriting, rather than merging a branch, but the other way round.

  •  Tags:  
  • git
  • Related