Home > Software design >  Copying a file from a specific commit/hash from one repo into another (unrelated) repo
Copying a file from a specific commit/hash from one repo into another (unrelated) repo

Time:03-16

I have two unrelated repos, A and B. I wish to fetch a specific file from A for a specific commit/hash/tag and copy it into B.

I have both A and B locally, i.e. both a cloned.

Thus far the only method I have is go to A, checkout to the desired commit, go back to B and do a cp path_to_repo_A/file path_to_repo_B/file

CodePudding user response:

You can use git show "[commit/hash/tag]:[file]" in repo A to print the contents of a file from git without checking it out. You can then pass that to a file in repo B.

E.g. to copy README.md on the main branch:

cd A
git show "main:README.md" > ../B/README.md
cd ../B
git add README.md
git commit

You might also find the -C option to git useful - it let's you specify the path for git to work from without having to cd to it:

cd B
git -C ../A show "main:README.md" > README.md
git add README.md
git commit
  • Related