Create a new remote GIT repo from a local repo

It’s actually quite easy to create a new remote GIT repository from a local repository in GIT 1.7. Many times you start a new small project and want to play around and somewhere on the line you start to think – “Hey this would be nice to work from different computers” or -“This is now critical software for my club, I need to share the code and secure it!” but you have it in a local repo.

0. If you don’t have a local repository you can start with creating one:
<you are standing in you project folder>

 
git init
git add *
git commit -m "My initial commit message"

1. Go to your server and create a new bare repository
<you are logged in to your server>

 
mkdir foo-bar.git
cd foo
-bar.git/
git
--bare init

2. Go to your server and create a new bare repository
<On your local machine in you project folder>
git remote add origin ssh://<user>@<server>/<git directory>/foo-bar.git
3. Push your code to the server and track the remote branch (master)
This step only works with GIT 1.7+
 
git push -u origin master
And you are done 🙂
UPDATE: If you are on an older version of git,  you need a few extra steps instead of step 3:
 
git push origin master

Now, to ensure that your local branch is tracking when you do a fetch, you need to use -f option to force a new local branch to be created even though it already exists.

# Checkout the origin/master branch
git checkout origin
/
master 
# Now we can create the local "master" branch that is tracking the remote branch
git branch
-f master origin/master 
# Switch back to your "master" branch
git checkout master