Ralf Wehner's Blog

Just another WordPress.com weblog

A simple way to create git repository on a server machine connecting via ssh

with 7 comments

Situation:

Let’s assume following situation:

  • We have a folder ˜/workshop , that contains the project on a local machine
  • Our project folder workshop is not a git repository yet
  • We want to have a server that hosts the workshop project and new developer can get the repository using a ‘git clone …’ command
  • On server site, the new repository should be located unter ˜/gitrepos/workshop.git.

To simplify the following steps i will use the machine localhost as the server machine.

Create a working copy repository

First, create a new local git repository and add all files within this folder.

cd ˜/workshop
git init
git add .
git commit -m "initial repository creation"

Create the bare repository

Then we have to create a bare repository on the server side. Let’s assume the user ralfwehner is the repository admin user on server side. For this step i will show two alternative ways:
a) We clone the server’s repositiory on the client machine and copy it via scp up to the server:

git clone --bare .git ../workshop.git
scp -r ../workshop.git ralfwehner@localhost:/Users/ralfwehner/gitrepos/workspace.git

b) We create a new empty repository on the server side and copy the developer’s repository from client machine to server (recommended when using difference git versions on server and clients):
So, first create the bare repository on server side:

sudo -u ralfwehner mkdir -m 770 /Users/ralfwehner/gitrepos/workshop.git
cd /Users/ralfwehner/gitrepos/workshop.git
sudo -u ralfwehner git --bare init --shared=group

From client side the developer’s project must be pushed into the new bare server repository:

git remote add origin ssh://ralfwehner@dev-server/Users/ralfwehner/gitrepos/workshop.git
git push origin master

That’s it. The project ‘workshop’ is now available on the server and can be cloned using the git clone command. E.g.:

cd /tmp/
git clone ralfwehner@localhost:/Users/ralfwehner/gitrepos/workshop.git myclonedworkshop

Synconize local and server repositories

Push developers repository to server

To synchronize the changes checked in into the local developer’s project to the server repository:

git push

Pull or merge the server repository into developer’s one

This command synchronizes the server’s repository to the local developer’s one. By this step changes made from other developers that pushed their stuff up to the server will be merged into the local repository.

git pull . remotes/origin/master

Checkout a project from server

In git terminology the checkout of a projekt can be understood as a clone of a git repository from a server to the developer’s local machine. You can do this simply by:

mkdir myNewWorkspace && cd myNewWorkspace
git clone ssh://localhost/Users/ralfwehner/gitrepos/workshop.git

The new created project can be pushed and pulled with:

cd workshop
... do you changes...
git push
... merge changes made from other users...
git pull

Heads up for Mac OS X users as i’m, too

There is a problem using the ssh commands to connecto to the git server which can end in error messages like: ‘bash: git-upload-pack: command not found’ on some machines. I’ve found this article that describe the problem and the solution more precisely.

For short, the solution is to create the symbolic link .bashrc to the .profile file:

server$ cd ~
server$ ln -s .profile .bashrc
About these ads

Written by rwehner

March 1, 2010 at 4:35 pm

Posted in git

Tagged with

7 Responses

Subscribe to comments with RSS.

  1. Thanks for this, this is the best explanation I found online.

    Zoran

    May 7, 2011 at 12:08 am

  2. [...] If you just want to create a bare repo on the server from your local repo, Ralph posted a nice way to do this: http://rwehner.wordpress.com/2010/03/01/a-simple-way-to-create-git-repository-on-a-server-machine-co… [...]

  3. Sir When i give the command for starting the git then it wasd not start it gives error like following
    so plz sir give me advice about that error

    error-
    The term ‘git’ is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spellin
    g of the name, or if a path was included, verify that the path is correct and try again.
    At line:1 char:4
    + git <<<< init *
    + CategoryInfo : ObjectNotFound: (git:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

    Sandip phatanagare

    April 28, 2012 at 9:00 pm

    • Hi Sandip,

      it seems to me that you have not installed git on your system before, isn’t it?

      Make sure, git is properly installed on your system and the ‘git’ command is in your PATH variable.

      rwehner

      April 29, 2012 at 11:32 am

  4. Thanks a lot!
    You have one more subscriber!

    Dan Cantir

    July 23, 2012 at 3:42 am

  5. Hi,

    I need to have slightly different setup as follows:

    We’ve a master server repository in remote location for entire team and we need to have a local git repository from/to which local team members can pull/push source code changes. So the scenario is as follows:

    – We have a folder ˜/workshop , that contains the project on a local machine (Pulled from remote master server repository)
    – Our project folder workshop is a git repository (clonned from remote master repository)
    – We want to have a server that hosts the workshop project and new developer (local team) can get the repository using a ‘git clone …’ command
    – On server site, the new repository should be located unter ˜/gitrepos/workshop.git.
    – At regular interval we need to push the local changes (from local team) to master server repository and also pull the latest changes from master server repository.

    I would appreciate if you can suggest some solution.

    Thanks,
    Bhavin

    Bhavin

    January 10, 2013 at 10:31 am

    • Hi Bhavin,

      i’ve never used such setup before but i think this will work. Just create a git repository on the remote master server. Then clone the project to your local company server. After that all developers should clone their project from your local company server. From developers point of view the remote git server is the local company server, so they will push their changes to this one. When you want to sync against your remote master server you have to push from local company server to remote master server and vice versa.

      But i think this scenary is not very elegant. What do you really want to have is a kind of stage. Better use the concept of branches and use only one remote git repository.

      You can create a new branch team_A in your local repository with ‘git checkout -b team_A’. Push this to the remote git repository with ‘git push origin team_A’, so other developer will see this branch as well and can pull the branch to their local machines. With git ‘checkout -b ..’ you will create a new branch and swith to this one. When you want to merge the code from branch team_A to master, you simply have to switch to master (git checkout master) and merge the branch code into the master sources (git merge team_A). After that push the actual source to your remote git server’s master branch with ‘git push’. Optionally you can delete the team_A with ‘git branch -d team_A’.

      rwehner

      January 11, 2013 at 11:27 am


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: