0

I've encountered a problem when 2 branches were developed independently for a long time and one of the files was moved into another directory
Let's say we had a file called src/dir1/Main.cpp and two branches branch1 and branch2 with common base
Then they were moved the file in branch1 and now it's called src/dir2/Main2.cpp
Of course both branches also modified that file independently
Now I want to rebase branch2 on top of branch1 and keep both changes in the file src/dir1/Main.cpp (original one).

git checkout branch2
git rebase branch1

Git asks if I want to use modified or deleted version, I choose deleted one. Then I want to proceed with a merge of file branch1:src/dir1/Main.cpp and <old_branch2_hash>:src/dir2/Main2.cpp with the base of base:src/dir1/Main.cpp. Is it possible to do with built-in git command (that possibly calls default mergetool) or without using bash-like $() syntax? I want it to work on both linux and windows systems and without bothering with manual copying of these files and calling a merge-tool on them.

IC_
  • 1,624
  • 1
  • 23
  • 57

1 Answers1

1

According to git merge-file, try

git cat-file -p branch1:src/dir1/Main.cpp > current.cpp
git cat-file -p old_branch2_hash:src/dir2/Main2.cpp > other.cpp
git cat-file -p base:src/dir1/Main.cpp > base.cpp
git merge-file current.cpp base.cpp other.cpp

The merge result is written into current.cpp. There could be conflicts. After resolving the conflicts if any, you could copy current.cpp to overwrite the target file.

ElpieKay
  • 27,194
  • 6
  • 32
  • 53
  • `>` syntax is not portable. It cannot be used in powershell but thanks for the link. I wonder if it can be used directly in `merge-file` command. I'll try it out shortly – IC_ May 18 '21 at 15:09
  • @Herrgott: Surely Powershell has something equivalent...? [(Apparently Powershell 3.0 has this supported directly, without complicated workarounds.)](https://stackoverflow.com/q/1215260/1256452) – torek May 18 '21 at 23:02