Create a new Git Remote Repository from some local files (or local git repository)
So you have some files or a new Rails application, and you want to add this to a new shared remote Git repository. (I’m assuming you have access to your server and are setting up a remote repo over ssh.)
I know I can never remember how to do it, so here’s a post for me and hopefully you!
Create a local Git repository in your application for your local files.
#On local machine
cd foo_project
git init
git add *
git commit -m "My initial commit message"
Now, create the repository on your Git server. All of my git repositories are owned by a user git and located at /usr/local/git_root/. You can change these things accordingly to match your server setup.
#On remote machine (Git remote repository)
sudo su - git
cd /usr/local/git_root/
Create your new project git repo as a bare Git repository
mkdir foo-project.git
cd foo-project.git/
git --bare init
Make sure permissions are set properly. These are common options I use for my shared repositories
git config core.sharedrepository 1
git config receive.denyNonFastforwards true
find objects -type d -exec chmod 02770 {} \;
#The core.sharedrepository flag tells git to keep everything group readable and writable.
#The receive.denyNonFastforwards flag makes sure that merges can't happen when you push to the repo. You have to do the merges on your local machine, and then push the result.
Now, go back to your local repository, and add the newly created remote repository so it tracks from the remote repository (origin).
#On local machine, in your git project
git remote add origin ssh://git@example.com:2227/usr/local/git_root/foo_project.git
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.
#Switch to origin/master so you don't get any error about "fatal: Cannot force update the current branch."
git checkout origin/master
#Create the local "master" branch that is tracking the "origin/master" branch
git branch -f master origin/master
#Switch back to your "master" branch
git checkout master
There you have it. You should be able to push changes to origin and fetch changes to your local copy!

Best tutorial of its kind on the web. It’s completely accurate, to the point, and beautifully formatted. You have saved me hours of pain. Thanks!
Andy K
9 Dec 10 at 8:13 pm
thank you very much friend helped me a lot!
Capy
17 Mar 11 at 4:17 pm
Vielen Dank für das Tutorial!
Thank you very much for this tutorial!
Adrian Kolodzik
7 Apr 11 at 6:38 am
Thanks for posting this article, it was very helpful to me in getting started with Git.
BDN
8 Apr 11 at 12:09 pm
special tnx for this tutorial
vahid
25 Apr 11 at 2:52 am
[...] sudo apt-get install git-core git-gui [...]
Montar un servidor de git | Capy.net
29 May 11 at 5:30 am
Very good tutorial, thanks a lot!
Yorga
7 Jun 11 at 3:47 pm
Very helpfull, thanks!
Greg
23 Jun 11 at 3:30 pm
Great writeup, thanks.
Stefan
27 Jun 11 at 11:43 pm
Great tutorial … thanx!
Camilo
19 Jul 11 at 8:35 am
thanks, very nice and to the point tutorial, just what I needed.
Tamas
26 Sep 11 at 5:10 am
Exactly what I needed to synch my work to a remote repository and share it with my collaborators. Thanks a lot for writing this down!
Gregor
13 Oct 11 at 1:01 am
thanks
abhilb
24 Oct 11 at 11:11 pm
[...] very good tutorial on how to create a remote git repository from a local one. Uncategorized ← MyBatis Type [...]
How to create a remote git repository | Sweettle TechNotes
29 Oct 11 at 8:02 am
awesome!! just what i needed..
db
15 Nov 11 at 6:57 pm
My repo is already git (derived from a public git repo). I want to now setup a private git on my own servers (different my computer). Can you tell how? Your instructions don’t work, since origin is already set for me.
sparsh
6 Dec 11 at 5:52 pm
@sparsh – i’m not sure if this is what you mean, but you can delete the .git directory and start from scratch. For more info, see: http://stackoverflow.com/questions/1213430/how-to-fully-delete-a-git-repository-created-with-init
Andrew Waage
6 Dec 11 at 6:00 pm
Thank you, this writeup helped me greatly in setting up my own remote git server on a Synology DiskStation. I’m stuck with one thing though: everything works well, but it seems clones and pushes need a little help – I have to manually edit gitolite.conf-compiled.pm (perl?) and add new repos I want to work with for each username. It shouldn’t be like that. I read the official gitolite ssh troubleshooting docs, but can’t find anything wrong with my install. How are new repos normally automatically added for user’s to the .pm file? Thanks!
sander
9 Dec 11 at 6:29 am
@sander – sorry I’m unfamiliar with Gitolite! Maybe someone else here would be able to help.
Andrew Waage
13 Dec 11 at 11:34 pm
Thanks for responding Andrew! I’ve solved my problem in the mean time, and it was Gitolite specific, I did not discern the git-only part of your blog at the time, sorry for any confusion. With gitolite, one administers the server from a client by cloning a special repo, ‘gitolite-admin’ and adding new repo’s and user permissions to the server from the client side, by fiddling with confs in gitolite-admin and then pushing the changes. It works wonderfully (once I understood what I was doing)! Originally, I tried your method with gitolite installed, but this was asking for trouble… Part of your instructions are still usable though, also for gitolite users, so you’ve actually contributed much to my understanding of the subject as a whole. Thanks for that!
sander
19 Dec 11 at 3:37 am
Many thanks. Up and running in minutes.
Rob Baltzer
26 Dec 11 at 5:29 pm
In a decent piece of software, simple things should be simple to do. This is by no means simple.
systemBuilder
3 Jan 12 at 10:10 am
[...] I found an excellent resource called Create a new Git Remote Repository from some local files (or local git repository). Very accurate, very clear, and very easy to follow. Essentially I was taking a 4GB set of [...]
Some quick git tips
5 Jan 12 at 12:18 pm
You’ve saved me a lot of work.
Thanks!
Antonio Serrano
17 Jan 12 at 1:41 am
Thank you, it’s helpful
kriom
20 Jan 12 at 1:39 am
Thanks for the info, it saved me lots of time.
Great formatting for all the commands as well, very easy to copy as needed.
–mC
Marcel Chastain
26 Jan 12 at 7:51 pm
@sparsh: You can call the remote repository anything you want – the origin is just usually called ‘origin’. You can define different remote repositories to push and pull from, just give them different names.
Say you were adding your private repo, make sure you create it with correct permissions, then in your local clone..
git remote add priv ssh://git@priv-home-server/git_path/foo_project.git
Stealthii
31 Jan 12 at 4:25 am