5
Posted August 1, 2011 by Spyros in General Tips & Tricks
 
 

Create a Git Repository on Remote Server and Checkout From Your Local Machine

git-r-done
git-r-done

Some time ago, i’ve written a post about the basics of git, how to create a local repository on git and more. However, git is a bit complicated, especially if you are switching from subversion. It’s highly likely that you have been using subversion in a remote repository, that is in another machine, and you have been committing your changes there. This is definitely the most standard model that programmers tend to work with.

Although this can happen with git, doing it can be a bit weird. For starters, you need to know that git has 2 different types of repositories. The first one is the non bare repository and the other one is the bare repository. But, what are the differences ?

Git Bare Repository vs Git Non Bare Repository

The bare repository is the one that you are used to with subversion. That is, a repository that has an internal way of representing data, using indexes and more.  On the other hand, a non bare repository actually refers to a working copy of the repository. A copy that just has the files of your project and not any configuration, indexes or other that the non-bare repository has.

5 Easy Steps For Creating Your Remote Repository

1) First, we will create an empty bare repository. The placeholder for our new project. SSH to your server, and give the commands :


mkdir /home/you/git/repository.git

cd /home/you/git/repository.git

git init --bare

This creates your empty bare repository and initializes it as empty.

2) Go the source directory, the one that has your project files. If it’s not in your remote server, just scp it there, upload it via ftp, whatever is more comfortable for you. Let’s say that your root project folder lies on /home/you/project. Now, we need to go to this folder and make it a non-bare repository. We do this with the commands :


cd /home/you/project

git init

3) Now that you have your non-bare repository, you need to add the files to that repository and commit the changes :


git add .

git commit -m 'initial commit'

4. You are now ready to add the bare repository as the remote origin :


git remote add origin /home/you/git/repository.git

5. You are now ready to push the changes to your bare repository, which is now the origin. One command to go :


git push --all

Check out (or clone for git) from your local machine

Your remote repository is ready now. What is left is testing it. Open up a shell in your local computer and execute :


git clone ssh://remote_ip/path_to_project.git new_project

new_project is the name of the folder that will be created with your files inside.


Spyros