Git: How to create a repository on a web server

David Nash bash, linux 2 Comments

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 (, 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

On both 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:$ mkdir website.git$ cd website.git$ git init --bare --shared=group
Initialized empty shared Git repository in /home/david/website.git/$ cd /var/www/website$ git init$ 
Initialized empty Git repository in /var/www/website/.git/$ git add *$ git commit -m "Initial commit"
[master (root-commit) ae6cf76] Initial commit
...(create mode output)...$ git remote add origin ~/website.git/$ 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$ cd ~/website.git/$ sudo chgrp www-data -R .
[sudo] password for david:$ cd /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
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)


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)
   ae6cf76..7d494d2  master -> master

Back to the server$ git pull origin master
From /home/david/website
 * branch            master     -> FETCH_HEAD
Updating ae6cf76..7d494d2
 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 2

Leave a Reply