0

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.

  • Merge strategy "ours"? What exactly are you trying to do? Merging without actually merging? Is the history important? Because if it is not, why merge in the first place, if you are not merging the content of the files? – knittl Jan 07 '23 at 22:14
  • @knittl yes the history is important. a lot of the work i did was with the old branch, and i just created a new project for the newer version and copy and pasted and fixed the stuff. If it actually merges it will not work and cause me a lot of problems. i just want the history and do not care about the code. –  Jan 07 '23 at 22:18

1 Answers1

2

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.

knittl
  • 246,190
  • 53
  • 318
  • 364