janos
janos

Reputation: 124646

How to get a sub-directory contents with a different name from another Git repo

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

git remote add checklists https://github.com/janosgyerik/software-construction-notes
git fetch checklists
git checkout checklists/master checklists

The example remote repository has a directory in its root called checklists. The last checkout command will grab the contents of that directory and put it in the root of my local repository.

But if I want to put the directory somewhere else? Sure, after the checkout I could move the directory anywhere I want with git mv checklists my/specs/dir/checklists. However, this could get troublesome if I already have a directory with the same name (and possibly different purpose) in the local project. I would first have to move the directory out of the way. Is there a cleaner way to do this, in one step? Something like this:

# 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 software-construction-notes project is meant as a common resource with a collection of notes, which I just shallow-clone like this in multiple projects to use as a template for doing the requirements analysis and architecture design. These independent projects don't need to track the history of the software-construction-notes projects, I really need just the latest snapshot of the files.

Upvotes: 3

Views: 56

Answers (1)

andi5
andi5

Reputation: 1616

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

might already work.

It reads the checklists/master tree and puts it into the index, but within a certain directory. The checkout command afterwards just updates your working directory from the index and basically reverts the new "to-be-deleted" files within my/specs/dir/checklists.

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

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

Upvotes: 1

Related Questions