Irbis
Irbis

Reputation: 1491

Copying commits from one git repo to another when a directory structure is different

The following topic describes how to copy commits from the one git repo to another. I have the similiar case but the directory structure in repos is different:

Simplified example:

Copy from repo: src/file.cpp

Copy to repo: src/logic/file.cpp

How to copy commits in such case ?

Upvotes: 3

Views: 1465

Answers (2)

Pino
Pino

Reputation: 9323

git diff hash1..hash2 > mypatch.diff

then search&replace the pathnames in the file and finally

git apply mypatch.diff

For a single commit hash2 is the target commit and hash1 is the previous one.

Warning: do not exchange the position of hash1 and hash2 in the first command otherwise the second command will give you "patch does not apply" on each file that should be patched. If you get "patch does not apply" just on some files (not all) you can use

git apply --reject mypatch.diff

to skip the problematic files and patch the remaining ones.

Upvotes: 1

alextechtai
alextechtai

Reputation: 101

The first thing you would have to do is to add the one of the repository as a remote with

git remote add <remote name> <remote URL>

and then fetch with

git fetch <remote name>

and use

git cherry-pick

to apply it to a commit

Upvotes: 1

Related Questions