Actions

EmSys

Getting Started with Git Remote Server

From EdWiki

Remote Repositories

To be able to collaborate on any Git project, we need to have a remote Git repository. The preferred method for collaborating with someone is to set up an intermediate repository that all of us have access to, and push to and pull from that. Therefore we have setup a Git Server. Remote repositories are versions of your project that are hosted on the network or Internet.

A remote repository is generally a bare repository - a Git repository that has no working directory. Because the repository is only used as a collaboration point, there is no reason to have a snapshot checked out on disk; it's just the Git-data. A bare repository is the contents of your project's .git directory and nothing else.

The Protocols

Git can use four major network protocols to transfer data:

  1. Local
  2. Secure Shell (SSH)
  3. Git
  4. HTTP

Each protocol has it's own pros and cons in a given circumstances. We have setup our Git Server using the SSH protocol because access over SSH is secure - all data transfer is encrypted and authenticated. SSH is efficient, making the data as compact as possible before transferring it. SSH is also the only network-based protocol that we can easily read from and write to Git Remote repositories.

We want to host our repositories on a Git Server that doesn't have accounts for every one in the lab whom we want to give write access, then we must setup SSH access for them. There are a few ways we can give access to every one in the lab. The first is to set up accounts for every body, which is straightforward but cumbersome. We don't want to run adduser and set temporary passwords for every user in the lab. To simplify the complexcity of user management, our Git Server is configured to authenticate users using the SSH public keys.

The gituser

We have created a single "git" user called gituser on the Git Server and asking every user in the lab who wants to have his/her project repositories on the Git Server to send us an SSH public key of his/her machine so that it is authenticated using the same.

We have taken an extra precaution to restrict the gituser to only doing Git activities with a limited shell tool called git-shell that comes with the Git package, so that the gituser can only use the SSH connection to push and pull his/her repositories and can't shell on to the Git Server. If the gituser tries to login, he/she will see a login rejection:

$ ssh shukra.dese.iisc.ac.in
gituser@shukra.cedt.iisc.ernet.in's password: 
fatal: What do you think I am? A shell?
Connection to shukra.cedt.iisc.ernet.in closed.

Generating Your SSH public Key

Our Git Server (shukra.dese.iisc.ac.in) authenticates using SSH public keys. In order to provide a public key, each user must generate one if he/she don't already have one. This process is similar across all operating systems.

First, you should check to make sure you don't already have a key. By default, a user's SSH keys are stored in that user's ~/.ssh directory. You can easily check to see if you have a key already by going to that directory and listing the contents:

~$ ls ~/.ssh 
id_rsa  id_rsa.pub  known_hosts

The id_rsa.pub file is your public key, and the other file id_rsa is your private key. If you don't find id_dsa/id_rsa, id_dsa.pub/id_rsa.pub OR you don't even have ~/.ssh directory, you have to create them using a program called ssh-keygen which is provided with the SSH package on Linux systems and comes with the MSysGit package on Windows.

$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/jshankar/.ssh/id_rsa):    <PRESS ENTER>
Enter passphrase (empty for no passphrase): <PRESS ENTER>
Enter same passphrase again:  <PRESS ENTER>
Your identification has been saved in /home/jshankar/.ssh/id_rsa.
Your public key has been saved in /home/jshankar/.ssh/id_rsa.pub.
The key fingerprint is:
c9:d8:6b:7b:c5:bb:97:75:0e:34:a3:bf:65:03:b1:75 jshankar@edes25
The key's randomart image is:
+--[ RSA 2048]----+
|                 |
|                 |
|             . .E|
|       + .    B .|
|      . S  . = o |
|         .  + o o|
|        o  . o *+|
|       . .. . +oo|
|        ..  .o.. |
+-----------------+

First, it confirms where you want to save the key (.ssh/id_rsa), and then it asks twice for a passphrase, which you can leave empty if you don't want to type a password when you use the key.

After creating the SSH public key (id_rsa.pub) file, please e-mail this file to haresh@cedt.iisc.ernet.in OR jshankar@cedt.iisc.ernet.in.

Putting the Repository on the Server

  1. Generate SSH public Key of your computer
  2. Send the created SSH public key (id_rsa.pub) to the Git Server admin.
    Please e-mail the SSH public key (id_rsa.pub) file to haresh@cedt.iisc.ernet.in OR jshankar@cedt.iisc.ernet.in.
  3. Create a new bare repository
    Ask your Git Server admin to create a bare repository every time you want to add a project.
  4. Adding Remote Repository
    • Now, create a new project of the same name created on the Git Server on your computer.
    • Use git remote add command to add the new project to the Remote Git Server
    • Now, you are ready to work with your Remote Git Server.


Admin on the Git Server (shukra.dese.iisc.ac.in)

  • Save the received SSH public key file(s) to a temporary directory.
    Suppose, if you have received it from jshankar@cedt.iisc.ernet.in. save it as /tmp/id_rsa.jshankar.pub
  • Append the SSH public Key to the /home/gituser/.ssh/authorized_keys file
$ cat /tmp/id_rsa.jshankar.pub >> /home/gituser/.ssh/authorized_keys
  • Create a bare repository for the user and set appropriate permissions
$ cd /opt/gitrepo
$ mkdir my_project.git
$ cd my_project.git
$ git --bare init
Initialized empty Git repository in /opt/gitrepo/my_project.git/ 
$ chgrp -R gitgrup /opt/gitrepo
$ chmod -R "g+wxr" /opt/gitrepo

User on his/her computer

$ mkdir my_project
$ cd my_project
$ git init
Initialized empty Git repository in /home/jshankar/my_project/.git/ 
$ echo “Hello World” > hello.txt
$ git add .
$ git commit -m ‘Check in number one’
$ git remote add origin gituser@shukra.dese.iisc.ac.in:/opt/gitrepo/my_project.git
$ git push origin master

Others

At this point, the others can clone it down and push changes back up just easily:

$ git clone gituser@shukra.dese.iisc.ac.in:/opt/gitrepo/my_project.git
$ cd  my_project
$ vim README
$ git commit -am 'Written about how to use the Robot'
$ git push origin master

That's It.

Presentations

  1. Introduction to Git
  2. Git - DoItYourSelf, Part - 1
  3. Git - DoItYourSelf, Part - 2
  4. Git - DoItYourSelf, Part - 3
  5. Everyday Tasks with Git

Videos

  1. What Is Version Control
  2. What Is Git?
  3. Get going with Git
  4. Quick wins with Git
  5. Git - Basics
  6. Git - Working with Remotes
  7. Git - Working on Branches