Unlike Subversion, git has no cp command. For files, this is not a problem: if I want to copy a file a to b, I can just do:
cp -a a b
git add bHowever, say I want to copy a directory d to give it another name e. I can still do the same thing. However, d may contain files that are not tracked by git, e.g., compiled binaries, etc. In this context, I do not want to do the above, because I do not want git to track these additional files.
With Subversion, I can do svn cp, and it will only copy and add the files that are tracked by Subversion. How can I do this with git?
3 Answers
You can use git clone:
$ git clone /path/to/project /target/pathThen remove the .git file
$ rm -R /target/path/.git 3 The right solution is simply to make sure that all untracked files are ignored in .gitignore.
In this case, when you copy the directory with cp -Ra d e and run git add e on the copy, git will be smart enough to avoid adding the files that it ignores.
- Find the tree hash of "d" (git ls-tree HEAD d)
- git read-tree -u --prefix=e tree-hash
Done!
git read-tree lets you replace your index, or (as in this case) a subtree of it, with any tree object that you know the hash of. The -u checks out the subtree from the index afterwards (as if you had done git checkout -- e).
For more information about tree objects and the index, please see the Pro Git book.