5

I'm getting more and more into Git and have to straighten this one out,

I read somewhere that it's bad security practice to put a .git repo inside folders that can be accessed trough the web (sounds reasonable!).

My question is, besides if its correct, what's the best solution to this is? The problem occuring mainly of course if you work on a remote host while web-developing.

For example if i work on domain.com/project where "project" is the repository, what can i do?

Is it possible to move the .git-folder somewhere outside htdocs and link to it? Or can i add some layer of protection to it?

I'm primarily developing on a shared host so i'm limited in what i can install and set up.

I'v read than you can have the whole repository/folder somewhere hidden on the server and then set a script to copy the content when pushing to it.

But when developing on a "live" server, i want to be able to test my code in real time between commits.

Am i clear?

Help appreciated!

jonas
  • 620
  • 1
  • 6
  • 14
  • You should revise your commit strategy: especially in git commits should be very small and frequent. You should thus commit more and don't immidiatly publish your changes to live. A possible solution is to use two branches (e.g. master and stable), another is to explicitly export a specific commit/branch/tag (see my answer) – dtech May 08 '12 at 12:11

4 Answers4

4

In the past, I've done this:

project/
  .git/
  README,etc
  public/
    index.html

Then you make the public/ subdirectory world visible, but hide the parent project/ directory. You can either do that with a symlink or with the right .htaccess incantations.

regularfry
  • 3,248
  • 2
  • 21
  • 27
  • Sounds like a good idea.. Since i can control the folders for each domain on my shared host i can always create a sub domain for each repo and have the domains root folder be project/public :) – jonas May 08 '12 at 11:40
2

Yes, you can move the .git directory and use GIT_DIR environment variable to point to it.

GIT_DIR

    If the GIT_DIR environment variable is set then it specifies a path to use instead of the
    default .git for the base of the repository.
KurzedMetal
  • 12,540
  • 6
  • 39
  • 65
  • Altough a good solution, note that this will break scripts or tools that don't respect the variable (i.e. have .git hardcoded) – dtech May 08 '12 at 12:07
  • 1
    @dtech sorry to say but your point is completely invalid, it's like saying "don't use w3g standards because browsers don't comply 100% with them" – KurzedMetal May 08 '12 at 12:11
  • It's more like: Don't use that HTML5 element even through W3C approves it, IE doesn't support it yet. – dtech May 08 '12 at 22:00
1

My answer assumes you're using apache on debian, but it is trivially adaptable for other distributions and servers.

You have two possibilities:

1: Create a symbolic link to your repository

You can now create a symbolic link from your public_html folder to your working copy. E.g. say your vhost config puts the DocumentRoot in "/var/www/site/public_html", you can symlink this to your git working copy:

ln -s /var/git/site/html /var/www/site/public_html

Or if you just want to make a subfolder accesible:

ln -s /var/git/site/feature/ /var/www/site/public_html/feature

This will still leave some .git files accesible, so you will have to modify .htaccess or vhost files to make them unavailable

2: Export your repository

In my opinion the far superior option is to create a (bare) repository and export (archive) when and the versions you want: you'll usually don't want to update your site after every push, but export a specific tag, branch or commit.

git archive master | tar -x -C /var/www/site/public_html

Here you can replace master by any of the accepted tree-ish structures (e.g. branch, commit or tag)

dtech
  • 13,741
  • 11
  • 48
  • 73
  • When I tried to create a symbolic link, I'm getting: `ln: nfs://123/opt/lampp/htdocs: No such file or directory`. But that is the path that I see when I get info on it. – ScottyBlades Nov 23 '19 at 09:35
0

I see two main solutions to your problem that are still simple enough (in contrast to the copy script).

First would be to have your accessible directory http://example.com/ in something like /var/www/example-com/ and have your git directory located in /var/www/.git. This works only if your host only has example-com in the /var/www/ directory (or at least none of the other v-servers need a git repository. You could get around this, if you can tell the admin to move your root directory to /var/www/example-com/site, such that files directly in /var/www/example-com are inaccessible.

The other would be to deny access to the .git directory, either via .htaccess, or if that's not available, via different users. If the server runs as www-data with main group www-data and your user name is me, you can have your root directory belong to me:www-data and set the group privileges to r-S meaning that everything can be read by members of the www-data group, directories (and only directories) can be executed, and the group-sticky bit is set. Then have your .git directory simply be rwx------ or a assign it to your own group.

bitmask
  • 32,434
  • 14
  • 99
  • 159