Starting with Git - Setting up remote environment for Git

In the tutorial, I will discuss the setup and configuration for remote and local Git repositories using SSH to transfer files between server and client. I used a Linux based server and a Mac OS X client for development but steps should work on similar environments supporting SSH. I have the following Git versions installed (using 'git --version'):

Linux server: 1.7.9.5
OS X client: 1.7.5.4

On Server:

We'll do the following things on server.

Setting Git variables:

git config --global user.email "<email address here>"
git config --global user.name "<repository admin's name here>"
git config --global core.editor nano
git config --global merge.tool vimdiff

Creating a new user for Git:

The first step was to add a new user so as to be able to use it for file transfers and restricting access to the system.

sudo useradd git
sudo passwd git

Initializing a Git repository:

Lets setup a repository area for your future repositories/projects. I used the directory /git/ but you can use any other base directory you wish. Then I created an empty directory inside /git/ for geeksww.com and initialized a repository there.

sudo chown git -R /git
# switch to git user
su git
cd /git/ ; mkdir ./geeksww
cd ./geeksww
git init

Adding existing project files to the repository:

If you already have files that you want to add to this repository then you can add them now.

cp -R /path/to/project/files /git/geeksww

Now, the next step is to add all files to the staging area before committing to the repository. But, before doing that you may have some files and/or directories that you do not want to be tracked by git such as cache files, log files, third party libraries etc. To ignore such files, you can create a text file named .gitignore in the main repository directory /git/geeksww and add files, directories, or patterns to ignore them. For example, I ignored the log folder and .gitignore itself. So my contents for /git/geeksww/.gitignore looked like below:

.gitignore
log/

From inside /git/geeksww, run the following command to make sure you are tracking what you want:

git status

To add/stage all files and sub-directories to be tracked by git do the following:

git add .

To make sure you are tracking what you want, run 'git status' again (this will show all files, so could be a long list, you can redirect it to a temporary file and view it in an editor).

Removing files from staging area

In case you are not satisfied, you can run 'git rm --cached' to remove files from the staging area. Please note that nothing has been committed to the Git repository yet.

git rm --cached <filename>
# to remove all staged files
git rm --cached -r .
# to find what is left in staging
git status

Committing staged files:

In order to commit staged files to Git, run the following from git directory /git/geeksww.

git commit -m 'initial commit to the repository'
# check status
git status

All files should be committed to the repository now. Now, lets setup the client and download files to client (or development machine) through Git.

On Client:

We'll be doing the following on client.

Setting up SSH access:

If you do not have a public and private key pair created already then use ssh-keygen program on your client to create one for you. Assuming that you already have a public key in ~/.ssh/ folder (file has .pub extension), copy and paste its contents in /home/git/.ssh/authorized_keys file on the server.

You can try logging in to the git server through git user by using the command like below:

ssh [email protected]

You might be asked for a password for your private key that you had set at the time of creating the public/private key pair. The next step is to clone the repository from server to your client.

Cloning your repository:

On your client,

cd ; mkdir git; cd git
git clone [email protected]:/git/geeksww

This should download all files from the remote Git server. Now, you can start making changes to the local files, stage them, and commit them locally using the same method that we used on server. But, in order to upload the changes to Git server you have to run git push.

Securing your git server:

Restricting access for the new git user:

You could optionally use the shell that comes with git to restrict access for the new git user on server. On my system, I found it under /usr/bin/git-shell. Here is how you change the shell name.

sudo chsh -s /usr/bin/git-shell git
# make sure shell has been set properly
sudo cat /etc/passwd | grep git
# switch to git user to make sure it does not allow login access
su git

Did this tutorial help a little? How about buy me a cup of coffee?

Buy me a coffee at ko-fi.com

Please feel free to use the comments form below if you have any questions or need more explanation on anything. I do not guarantee a response.

IMPORTANT: You must thoroughy test any instructions on a production-like test environment first before trying anything on production systems. And, make sure it is tested for security, privacy, and safety. See our terms here.