How to get the contents of a subdirectory with a different name from another Git repository

Getting a subdirectory from another Git repository with the same name and the same relative path is easy, for example:

git remote add checklists
git fetch checklists
git checkout checklists/master checklists


The sample remote repository has a directory in the root directory checklists

. The last command checkout

will grab the contents of this directory and put it in the root of my local repository.

But what if I want to put the directory somewhere else? Of course, after checkout

I could move the directory anywhere with git mv checklists my/specs/dir/checklists

. However, this can cause problems if I already have a directory with the same name (and possibly a different destination) in a local project. I would have to move the directory to the side first. Is there a cleaner way to do this in one step? Something like that:

# grab the "checklists" dir and put its contents to my/specs/dir/checklists
git checkout checklists/master checklists my/specs/dir/checklists


Btw, the local repository is a completely independent project. The project is software-construction-notes

meant as a shared resource with a set of notes, which I just cloned in this way in several projects to use as a template for performing requirements analysis and architecture design. These independent projects don't need to keep track of project history software-construction-notes

, I really only need the latest snapshot of the files.


source to share

1 answer

git read-tree --prefix=my/specs/dir checklists/master
git checkout-index -a


can work.

It reads the control tables / master tree and puts them in the index, but within a specific directory. After that, the checkout command just updates your working directory from the index and basically returns the new "be deleted" files in my / specs / dir / checklists.

If you usually agree with what git will do, you can combine both commands with

git read-tree -u --prefix=my/specs/dir checklists/master




All Articles