2

Suppose I already have a remote server. The project saved in /var/www/html/project. It has already some files and folders in it. Then, I want to make the deployment using git from my local machine to that server. The question is, how can I achieve that?

If I use git bare --init, it has to be empty files/folders on the server, right? How if in the server the files already exist? Thank you.

SNahar
  • 134
  • 1
  • 13

1 Answers1

0

If you want to push to GitHub, then your server Git repo don't have to be bare.
It should be bare only if you want to push to your server.

Make sure your username and email are set first

 git config --global user.name  ausername
 git config --global user.email anEmail@com

You can start for now by creating on your server (outside of /var/www/html, to avoid the git repo itself to be served by your Apache web server) a repo

 cd /a/path
 git init arepo
 cd arepo
 git --work-tree=/var/www/html/project add .
 git commit -m "First project import"

Once that is done, you can clone that repo as a bare repo:

cd /a/path
git clone --bare arepo
# generates a repo.git folder

(note, I assume your GitHub repo is created completely empty)

If your server has an ssh daemon running, and you have a user account who can access /a/path/repo.git, you can locally do:

cd /a/local/path
git clone auser@aserver:/a/path/arepo.git
cd arepo

You now have the local code on your computer.

After a change, you can push back. But you might want a post-receive hook if you want that change (pushed to the server bare repo) to show up in /var/www/html as well.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Thank you for the answer. But actually, I want to push my local codes (on my laptop) to the server, not to the github. Right now, my local codes are empty and I want it to be filled (git pull) with the existing project from that server. After that, every changes I made on the local, I can push it to the server. How can I achieve that? – SNahar Jun 17 '17 at 05:08
  • Thanks! Very helpful. But just one last question, what's in the `post-receive` file? I created that in `arepo.git` directory with these lines : `#!/bin/sh git --work-tree=/var/www/html/project --git-dir=/a/path/arepo.git checkout -f` and `chmod +x` that file. But when I push the change, on the server there's no change. – SNahar Jun 17 '17 at 05:29
  • [UPDATE] Everything works fine. I edited the post-receive file. Thank you very much. – SNahar Jun 17 '17 at 05:39
  • @WildanS.Nahar Great! Sorry, I had to leave for a moment. – VonC Jun 17 '17 at 05:51