Initializing Git remote server

By | 30/11/2013

I will be creating a Git repository to a server using a shared folder so that others can access it too without any awkward sharing of accounts. I got the idea of a shared folder from Tero Karvinen and will be using his following posts as reference Shared Folder with chmod SetGID and Git from Offline to Network . This post will also work as a basis to my linux centralized management course’s final module.

Systems used: Server 64-bit Ubuntu 12.04.3 LTS, client 64-bit Ubuntu 12.10

Let’s get started:

Shared folder:

Server computer

First I’ll be adding a new user to serve as the shared folder.

$ sudo adduser samuelgit

I lock the ability to login to the newly created user.

$ sudo usermod --lock samuelgit

Next I create the actual folder I’ll be using later on.

$ sudo mkdir /home/samuelgit/projects

I give all rights to the group samuelgit and set groupid with the ‘s’ so all files and folders under projects folder will have the same permissions.

$ sudo chmod g+rwxs /home/samuelgit/projects/

I set the actual ownership of the folders to the group samuelgit.

$ sudo chown .samuelgit /home/samuelgit/projects/

I added my user to the group samuelgit

$sudo adduser samuel samuelgit

After this I logged out and back in and tested the new folder

$ nano /home/samuelgit/projects/testing

testing new shared folder

The file was added successfully.

Now to get git working by either creating a new repository from scratch or adding an old git project to the repository:

Git repository from scratch:

Server computer

after logging in to the server I navigated to the folder I created earlier

$ cd /home/samuelgit/projects/

I created a new folder under projects with the ‘.git’ -suffix which is used if the folder is only used for storage.

$ mkdir helloRepository.git

I moved in and created a bare git project (added also the shared -flag just to be sure…) and exited the server

$ cd helloRepository.git
$ git init --bare --shared
$ exit

Local computer

I created a new folder for the git project:

$ mkdir newProject/
$ cd newProject/

I cloned the the project from the server to the folder I created:

$ git clone ssh://[email protected]/home/samuelgit/projects/helloRepository.git

And we’re done!

notice: the first push to the server needs to be:
$ git push origin master

since we haven’t added any content before.

Testing is done in the end of this post.

Git repository after local use:

Local computer

I started by creating a folder for the git project.

$ mkdir -p gitProjects/fromLocal
$ cd gitProjects/fromLocal/

I created a new empty git project

$ git init

I created a “hello world” file

$ nano README

Hello World

I added all new files (README) to be committed to git (-A = all) and commited the changes (-m = message).

$ git add -A
$ git commit -m "Initial commit"

Server computer

after logging in to the server I navigated to the folder I created earlier

$ cd /home/samuelgit/projects/

I created a new folder under projects indentical to the local folder exept I added the ‘.git’ -suffix

$ mkdir fromLocal.git

I moved in and created a bare git project (added also the shared -flag just to be sure…) and exited the server

$ cd fromLocal.git
$ git init --bare --shared
$ exit

Local computer

I added the git repository’s address to my local folder and pushed my local changes to the repository

$ git remote add origin ssh://[email protected]/home/samuelgit/projects/fromLocal.git
$ git push origin master

I defined the repository as the master, where the pulls and pushes will be made to

$ git branch --set-upstream master origin/master

Testing

Local computer

I created a new file inside the created git -folder

$ nano testingGit

I will be testing git's pull and push

I added the new file to be committed with:

$ git add -A

and committed it with:

git commit -m "Initial commit"

It’s always good to pull all changes before pushing your own to the server

git pull

and

git push

notice: the if you haven’t already, the first push needs to be:
$ git push origin master

That’s pretty much the basics, the project can be cloned to other computers too. Remember to start with git pull as there will be content now.

One thought on “Initializing Git remote server

  1. Pingback: How to get shared Git repository to server | Lauri Soivi

Leave a Reply

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