i'd like to just checkout the files without the .git files and the whole repository. It's because i'd like to manage a website (php & html) with git and i'm looking for an easy way to update the files in the htdocs folder from the repository, without having the repository public. (now it's in the home-dir and is accessed via ssh, but i have to put the new files to htdocs manually.
-
3It would probably be easier to leave .git and just have your webserver block access to it – Michael Mrozek May 19 '10 at 14:30
-
3The answer to this question about emulating an Svn export may be what you need: http://stackoverflow.com/questions/160608/how-to-do-a-git-export-like-svn-export – Rob Wilkerson May 19 '10 at 15:01
6 Answers
The git-archive manpage contains the following example:
git archive --format=tar --prefix=junk/ HEAD | (cd /var/tmp/ && tar xf -)
Create a tar archive that contains the contents of the latest commit on the current branch, and extract it in the '/var/tmp/junk' directory.
Or you can use low level git-checkout-index, which manpage contains the following example:
Using git checkout-index to "export an entire tree"
The prefix ability basically makes it trivial to use '
git checkout-index
' as an "export as tree" function. Just read the desired tree into the index, and do$ git checkout-index --prefix=git-export-dir/ -a
git checkout-index
will "export" the index into the specified directory.The final "/" is important. The exported name is literally just prefixed with the specified string.
Or you can try to use --work-tree
option to git wrapper, or GIT_WORK_TREE environment variable, e.g. by using "git --work-tree=/somwehere/else checkout -- .
".

- 309,089
- 65
- 217
- 230
-
4git-archive is great for this. also, you can also use git archive --remote to build an archive directly from a remote repo (even over git:// protocol iirc) and hence use this as a way to fetch straight from a central point. e.g. `git archive --remote git://git/mindy.git --format tar r3.2.0 | tar tf -` (provided you enable upload-archive in git-daemon config) – araqnid May 19 '10 at 19:57
-
4@araqnid that might be cool but doesn't work with Github: while using ssh URL `Invalid command: 'git-upload-archive`, while using https URL: `fatal: Operation not supported by protocol.` – Patryk Apr 28 '15 at 21:24
-
`git checkout-index` works like a charm, but make sure you use an absolute path for the `--prefix` argument. I tried `~/…` and actually got a `~` directory in my working directory (so, instead, use, e.g., `/Users/aral/…`) – Aral Balkan Oct 13 '15 at 14:03
git clone --depth 1
does exactly what you want. I have a git repository with 41,000 commits and this is vastly faster. See this answer or this more general response for a more detailed explanation.
If you want to specify the branch to use (instead of the default "master" branch) use this syntax (requires Git 1.9+):
git clone -b <remoteBranch> --single-branch --depth 1 ssh://username@servername.net:serverport/PathToProject <FolderName>

- 1
- 1

- 569
- 5
- 8
Git is much easier than Subversion for this, as the whole repository is held in a single directory. You only need to delete the hidden ".git" folder in the root of the project to create a production-ready copy of your site.
In Linux/OSX this would be:
mkdir dist && cd dist
git checkout --depth=1 http://path/to/your/project.git
rm -rf .git

- 17,426
- 15
- 71
- 93
-
4This has the downside of copying the entire history... It could be slow if it's a large repo and over network. – trusktr Mar 30 '13 at 00:17
-
I think the question wants to minimize network usage, and `diff` calculations... In other minimize the resource usage. – Willem Van Onsem Sep 28 '14 at 02:08
-
1You might be right, and some of the other answers are more suitable for these situations, but the question doesn't ask for a performance critical answer. This approach is simple and workable for many workflows, particularly if the repo is already checked out on the target machine. I'll keep it listed as some people have found it useful. – seanhodges Sep 30 '14 at 06:09
-
2The only modification I'd make to this answer is use `--depth=1` to prevent downloading the entire history. Since `.git` is going to be removed in the end, using `--depth=1` allows skipping the transfer of information that won't be kept in the end, and does not change the end result. As to the usefulness of this answer: I'd much rather use `git archive`, but as pointed out under [that answer](https://stackoverflow.com/a/2867314/1906307) *Github does not support `git archive`* so I cannot use that. The next best thing is `git checkout --depth=1 ... && rm -rf .git` – Louis Jun 27 '19 at 12:16
You could create a tar archive in a git working directory and copy the tar to the server:
git archive -o foo.tar HEAD
scp foo.tar server:

- 574,206
- 118
- 941
- 841
Expanding on seanhodges answer, you could just rsync the project's files to wherever you need to put them and use --exclude to avoid the .git directory.
rsync -avz --exclude=.git /source/directory/ user@remote-server.com:/target-directory
I didn't test that command so feel free to edit it, but you get the jist.
A useful option is to add --dry-run to the end of the command, that will show you what files will be sent without actually making any changes.

- 3,554
- 36
- 33
You'd probably benefit from a bare repo plus post-receive hook.
This method is useful if you desire to push changes to your server but don't want the htdocs themselves under version control.
We'll make a folder for the bare git repo. I like to put it in the same parent folder as htdocs:
mkdir barerepo.git
cd barerepo.git
git --bare init
Then create a post-receive hook file, make it executable:
touch hooks/post-receive
chmod ug+x hooks/post-receive
Edit post-receive in your favorite text editor:
GIT_WORK_TREE=/full/path/to/htdocs git checkout -f
# optional stuff:
cd down/to/some/directory
[do some stuff]
Now every time you push to this bare repo it will checkout the working tree to htdocs. But htdocs itself is not under version control; running git status
in htdocs will give the error fatal: Not a git repository (or any parent up to mount point /data)
. It's just plain files.
Note that you must always push from master for this to work otherwise git checkout
won't know what branch to checkout.

- 3,125
- 1
- 15
- 24