Search code examples
gitgit-mergegit-repo

How do I merge a specific foler from "Their" repo into "my" repo where the directories are in different directories


I have a repository (my repository) that has a non empty directory, say its called "target". In this folder I have modified files from their repository's target folder. They have updated their files in their target directory, and I want to keep some of my modifications as it patches some issues but pull in their changes.

Say it looks like this: my repo structure

- main.py
- non_main.py
- target/
- - file_1
- - file_2

theirs

- other.py
- something_else.py
- long/path/to/the/target/
- - - - - - - - - - - - - - file_1
- - - - - - - - - - - - - - file_2
- - - - - - - - - - - - - - file_3

and I have modified in my repo file_1 and file_2. They have added file 3 and possibly made changes to file_1 or file_2

I would like to merge in their changes to the target folder into my target folder. Like a merge but the repos look different but have 1 directory that we want to combine. I am only interested in combining the target folder

I have tried this: How do I merge a sub directory in Git? but seems like the prefix does not overwrite any files present in the original index. as it would give me error entry "file" overlaps with "file". Cannot bind. Is there a way to accomplish this?


Solution

  • Try git merge-file.

    Suppose you have cloned the two repositories and checked out the expected branch in both.

    # In your repository, create an empty file as the base file
    touch foo.txt
    
    # Merge their file_1 to yours
    git merge-file target/file_1 foo.txt /path/to/long/path/to/the/target/file_1
    

    Your target/file_1 will be changed. If there is any conflic, just resolve it.

    If you know what the real base file foo.txt is, the last common version of yours and theirs, you can use that version instead of the empty one.