Is there a `git cp` command that can copy only tracked files, like `svn cp`?

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 b

However, 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?


Solution 1:

You can use git clone:

$ git clone /path/to/project /target/path

Then remove the .git file

$ rm -R /target/path/.git

Solution 2:

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.