Sneaksta
Sneaksta

Reputation: 1061

Pushing Git Repo to Remote Server

So I am trying to push a repo from my localhost to my VPS.

I have setup a --bare repo on my vps and a repo on my localhost.

I have used the following commands to push to the VPS:

# git remote add origin ssh://[email protected]/home/mysite/public_html/mygit/.git
# git push origin master

It pushes successfully, but what I need to be able to do is checkout the files from the VPS .git. But, when I try running 'git checkout' I get the error:

fatal: This operation must be run in a work tree

I have tried editing the .git config file and editing/adding these lines:

[core]
bare = true
worktree = /home/mysite/public_html/mygit/

[receive]
denycurrentbranch = false

This allows me to run git commands, but the files wont checkout, and git tells me files have been deleted.

I am just a little bit confused. How should I be going about this?

Upvotes: 3

Views: 5431

Answers (1)

AD7six
AD7six

Reputation: 66218

Trying to perform checkouts on a bare repository is quite illogical - checkout is a command for updating your working copy/tree:

git-checkout - Checkout a branch or paths to the working tree

A bare repository doesn't have a working tree.

Setting up your git repos the normal way

So starting from zero, let's assume you are on your home/office computer:

Create bare repo

Create a bare repo for your project - or move your existing repo to some appropriate location:

ssh [email protected]
cd repos
mkdir repo.git ##give it a better name
cd repo.git
git init --bare

Make a clone for your site

ssh [email protected]
cd /home/mysite/public_html
git clone ~/repos/repo.git .

Create a local clone

cd mylocalsite/public_html
git clone [email protected]:repos/repo.git .
git push/pull etc.

Update site automatically

Since your goal is to update automatically, You can use git-hooks to update the working copy that is your live site whenever you push to your repo.

Here's a simple example:

#!/bin/sh
cd /home/mysite/public_html
git --git-dir /home/mysite/public_html/.git pull > /dev/null

save it as /home/root/repos/.git/hooks/post-receive, make it executable, and every time you push, it'll attempt to update your live site by issuing a pull

OR

you can forgoe the use of a bare repository and use your live site's checkout as your main/only repository. This is not recommended but if you choose to do so, you would do:

git remote add live [email protected]:/home/mysite/public_html/.git
git push live master

It'll give you an error about updating the currently checked-out branch, read the warning and understand what it means. If you decide you still want to do this you can allow it to work with:

ssh [email protected]
cd /home/mysite/public_html
git config receive.denyCurrentBranch ignore

Again not recommended, much better to have a bare repo and maintain your live site as a normal checkout.

Upvotes: 6

Related Questions