简体   繁体   中英

Add git to existing project on server

I know maybe this question is answered somewhere but I didn't find a solution for it. I have a project that is already in a live server and I want to add a .git to be able to pull/push from my local machine.

I was thinking that I can do on the server : git init git remote add origin git@project.git git add . git commit -m "Initial commit"

But it seems not to be the right way so I tried to do a git init --bare project.git and clone from my local, but the directory is empty.

Can someone tell me how I can do add git in this case. Thank You.

I think I know what you want to do and I have done the same thing before. What I wanted was something like this, where both folders are Git repos

  laptop              server
~/website   <=>   /var/www/html
  .git/             .git/
  index.html        index.html

However, that is not possible because from my laptop I can't push to the repo in /var/www/html because that is not a bare repo. I also can't make it a bare repo because a bare repo doesn't have a worktree (which I need because that's the files that display my website).

So instead I needed a middle man:

  laptop            server              server
~/website   <=>   website.git   =>   /var/www/html
  .git/                                .git/
  index.html                           index.html

where website.git is a bare repo. Then I just add a post receive hook in the file website.git/hooks/post-receive that cd s to /var/www/html and pulls from the bare repo that's on the same hard drive. Here is my hook

#!/bin/bash

# unset environment set by git
unset $(git rev-parse --local-env-vars)

# go to website and assemble
cd /var/www/html
git fetch origin
git reset --hard origin/master

# Whatever other instruction you need to build the website
# ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM