I read GIT documentation, but couldn't figure out how to just simply copy newest version of code to another directory (or deploy it). Of course I could just simply copy -r code /path/to/another/dir
, but then it copies all other files that are created by GIT. And I don't need that. So is there a simple way to just copy raw files without all those hidden files (or copies of original files)?

- 19,658
- 37
- 143
- 243
-
Do you have access to Unix tools in something like a bash shell, or Cygwin? – Aug 14 '13 at 05:50
-
1Might want to consider this as a duplicate of [How to do a "git export" (like "svn export")](http://stackoverflow.com/questions/160608/how-to-do-a-git-export-like-svn-export). – Aug 14 '13 at 06:12
2 Answers
This is one way to do it:
mkdir /path/to/target/dir
git archive master | tar x -C /path/to/target/dir
The git archive
command is normally to generate an archive file from the content of the repository. It dumps to archive to standard output, which you normally redirect to a file. Here, we subvert the command a bit: instead of redirecting, we immediately extract it, into the specified directory. The target directory must be created first.
That said, you seem to want this for deploying your project. Personally I use Git itself to deploy projects. That way you can upgrade and roll back easily using Git commands. Granted, the .git
directory will exist in the deployment directory, on the other hand, a simply copy will not delete files that were removed in the repository, they will remain at the destination.
Finally, you might be interested in my personal favorite: using a post-receive hook to deploy projects remotely.

- 120,954
- 29
- 226
- 236
Option 1: Exclude .git subdirectory
Assuming you have a standard setup for your Git repo, you could just copy everything in your project's root, but exclude the .git
subdirectory.
Apparently you can do this with a bash terminal using rsync
:
rsync -av --exclude='.git' source destination
but I'm not sure how you could do it with Windows copy
.
Option 2: git archive export
Another option is to use git archive
:
git archive --format zip --output "output.zip" master -0
will give you an uncompressed archive (-0
is the flag for uncompressed). The issue with this is that you then have to extract the files from the archive.
If you have access to Unix tools in something like a bash shell or Cygwin, you could also use these Unix tools in janos's answer. See also How to do a “git export” (like “svn export”).
# Option 3: Use git ls-files
Edit actually having problems with this one because I'm passing the arguments to cp
in the wrong order (files as destination directories instead of sources), so scratch this for now.
Another option is to get a list of all the files that Git is tracking in your working copy, then pass them as an argument to a copy command. Here is an example with Unix cp
:
git ls-files | xargs cp <destination-root>

- 1
- 1
-
I tried this command: `rsync -av --exclude='.git' source destination`, but it still copies files that has en ending with `~`, is it possible to exclude files that has en ending with `~`? – Andrius Aug 14 '13 at 05:42
-
-
@Andrius you can add multiple `--exclude` flags, for example: `rsync -av --exclude='.git' --exclude='*~' source destination` – janos Aug 14 '13 at 05:54
-
@Andrius are you able to try [janos's answer](http://stackoverflow.com/questions/18223842/git-copy-newest-branch-to-another-directory/18223920#18223920)? It's probably simpler. – Aug 14 '13 at 05:54
-
@janos for some reason, trying to exclude `'*~'`, doesn't work. Also are these files with `~` important in git? Or can I just delete them and add ignore in git? I saw an option there in exclude file with parameter - `*~?` – Andrius Aug 14 '13 at 06:00
-
@janos tried again your solution, and it worked (to exclude both ~ and .git). Maybe I misspelled something then. Thank you very much :). You could right this to your answer as this solved my problem and I will choose your answer. – Andrius Aug 14 '13 at 06:10
-
This is @Cupcake's answer, I just added a simple note to your follow-up question in your comment. If this is the solution you used and like, you should accept this answer instead of mine. You can vote up everyone's answer, that will be nice. – janos Aug 14 '13 at 06:36
-
The `~` files are backup files most probably generated when you edit the originals using `vi`. You can safely delete them, but they will keep coming back every time you edit something. – janos Aug 14 '13 at 06:39