Jonathan
Jonathan

Reputation: 3534

Why use a git bare repository for website deployment?

I'm starting to deploy a few website plugins via git.

I've been through a number of tutorials on the web and all of them recommend creating a bare remote repository on the web server with a post-update hook to checkout into the DocumentRoot directory. I've gotten this all working without too many issues.

Why are we separating the respository from the working directory? What's wrong with just using the DocumentRoot directory as the repository and then using htaccess to prevent public access to any .git content?

Upvotes: 9

Views: 2906

Answers (3)

Fwolf
Fwolf

Reputation: 691

  1. Use DocumentRoot as git working directory:

    • Use pull for update, need external trigger or cron or manual execute it
    • Need restrict http access to .git directory
    • Do instant edit/hack in DocumentRoot can use diff to compare with git history, good for debug/find problem
    • Switch branch is easy
  2. Use separate bare git repostory

    • User can push to this repo and use post-update hook to update DocumentRoot
    • Usually nobody want to do instant hack in git bare repo, modification in DocumentRoot can't diff with git history
    • DocumentRoot is clean
    • Switch branch may need modify hook

So, seems separate bare git repo is more clean and easy to use, except need to setup a post-update hook.

Plan 1 is also work able, use it in a small project or develope mechine is fine, but beware the access privilege to .git directory, especially when changing web server. eg: nginx doesn't support .htaccess, which is often stored in git with other project files together.

Upvotes: 0

slebetman
slebetman

Reputation: 113876

There are several questions here that, while related are actually separate:

1 - Why are we separating the respository from the working directory?

Because git prevents you from pushing to a checked out branch by default. You can therefore never push to a working directory because by definition, if it's on disk it's checked out.

2 - All of them recommend creating a bare remote repository on the web server

Implied question is: why a bare repo? Well, a bare repo is simply a git repo with nothing checked out. Therefore you can push to any branch on a bare repo. Some people consider this simpler to maintain.

Second implied question: is there an alternative to bare repos? Yes, as mentioned the restriction is only on checked out branches. So you can still freely push to a non checked out branch - maybe "deploy" and then have a hook to merge that non checked out branch to your working directory.

Upvotes: 0

Fred Foo
Fred Foo

Reputation: 363567

Actually the security risks inherent in a poorly configured htaccess are a good reason by themselves, but the main reason is that by default, you can't push to the checked-out branch of a non-bare repo. (It can be enabled, but then requires a git reset --hard to update the work tree, and is no simpler than having a separate deployment directory.)

Upvotes: 5

Related Questions