Git: How to create a repository on a web server

David Nash bash, linux 1 Comment

I’m writing this mainly for my own reference. I found it very difficult to find instructions on using git for what I wanted.

The scenario: I have a web server (example.com), which I can ssh into. It has a website running on /var/www/website/. I want to create a git repo for it, and then from my development machine (devmachine) clone that repo, make changes, and when I’m happy push them to the web server.

I already have ssh keys installed so I can ssh from devmachine to example.com.

On both example.com and devmachine I have a user david who is in the group www-data.

Git on the server

First I need to create somewhere to store the project:

david@example.com:~$ mkdir website.git
david@example.com:~$ cd website.git
david@example.com:~$ git init --bare --shared=group
Initialized empty shared Git repository in /home/david/website.git/
david@example.com:~$ cd /var/www/website
david@example.com:/var/www/website$ git init
david@example.com:/var/www/website$ 
Initialized empty Git repository in /var/www/website/.git/
david@example.com:/var/www/website$ git add *
david@example.com:/var/www/website$ git commit -m "Initial commit"
[master (root-commit) ae6cf76] Initial commit
...(create mode output)...
david@example.com:/var/www/website$ git remote add origin ~/website.git/
david@example.com:/var/www/website$ git push origin master
Counting objects: 221, done.
Compressing objects: 100% (202/202), done.
Writing objects: 100% (221/221), 480.79 KiB | 0 bytes/s, done.
Total 221 (delta 27), reused 0 (delta 0)
To /home/david/threegoodthings.git/
 * [new branch]      master -> master
david@example.com:/var/www/website$ cd ~/website.git/
david@example.com:~/website.git$ sudo chgrp www-data -R .
[sudo] password for david:
david@example.com:~/website.git$ cd /var/www/website
david@example.com:/var/www/website$ git status
On branch master
nothing to commit, working directory clean

Okay, great! After creating the place to store it, I then went into the website directory and told it that the project is stored in my home directory. I then go back to my home directory and change the group permissions to www-data, so that I can push to it remotely, from devmachine.

Git on the development machine

I clone the repo I created, and then create a file called “test” to make sure I can push it to the server.

david@devmachine:~$ cd /var/www
david@devmachine:/var/www$ git clone example.com:website.git
Cloning into 'website'...
remote: Counting objects: 221, done.
remote: Compressing objects: 100% (175/175), done.
remote: Total 221 (delta 27), reused 221 (delta 27)
Receiving objects: 100% (221/221), 480.79 KiB | 302.00 KiB/s, done.
Resolving deltas: 100% (27/27), done.
Checking connectivity... done.

david@devmachine:/var/www$ vim test
david@devmachine:/var/www$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Untracked files:
  (use "git add <file>..." to include in what will be committed)

	test

nothing added to commit but untracked files present (use "git add" to track)

david@devmachine:/var/www$ git add .

david@devmachine:/var/www$ git commit -m "testing git push"
[master 7d494d2] testing git push
 1 file changed, 1 insertion(+)
 create mode 100644 test

david@devmachine:/var/www$ git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)
nothing to commit, working tree clean

david@devmachine:/var/www$ git push
Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 297 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
To example.com:website.git
   ae6cf76..7d494d2  master -> master

Back to the server

david@example.com:/var/www/website/$ git pull origin master
From /home/david/website
 * branch            master     -> FETCH_HEAD
Updating ae6cf76..7d494d2
Fast-forward
 test | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 test

And there’s my test file, on the server! Now I can work locally, and when I’m ready push them to the server. I then ssh to the server and pull the changes to make them live.

Comments 1

Leave a Reply

Your email address will not be published. Required fields are marked *