Rss Feed Tweeter button Facebook button Technorati button Reddit button Linkedin button Webonews button Delicious button Digg button Flickr button Stumbleupon button Newsvine button

A Waage Blog

Ruby, Rails, Life

Create a new Git Remote Repository from some local files (or local git repository)

with 42 comments

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

### UPDATED:
If you are on git 1.7+ you can simply do this:

git push -u origin master

You’re done!

———————-
If you are on an older version of git, you may need to follow these instructions to make sure your branch tracks the remote branch:

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!

Written by Andrew Waage

October 21st, 2009 at 1:06 pm

42 Responses to 'Create a new Git Remote Repository from some local files (or local git repository)'

Subscribe to comments with RSS or TrackBack to 'Create a new Git Remote Repository from some local files (or local git repository)'.

  1. 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

  2. thank you very much friend helped me a lot!

    Capy

    17 Mar 11 at 4:17 pm

  3. Vielen Dank für das Tutorial!

    Thank you very much for this tutorial!

    Adrian Kolodzik

    7 Apr 11 at 6:38 am

  4. Thanks for posting this article, it was very helpful to me in getting started with Git.

    BDN

    8 Apr 11 at 12:09 pm

  5. special tnx for this tutorial

    vahid

    25 Apr 11 at 2:52 am

  6. [...] sudo apt-get install git-core git-gui [...]

  7. Very good tutorial, thanks a lot!

    Yorga

    7 Jun 11 at 3:47 pm

  8. Very helpfull, thanks!

    Greg

    23 Jun 11 at 3:30 pm

  9. Great writeup, thanks.

    Stefan

    27 Jun 11 at 11:43 pm

  10. Great tutorial … thanx!

    Camilo

    19 Jul 11 at 8:35 am

  11. thanks, very nice and to the point tutorial, just what I needed.

    Tamas

    26 Sep 11 at 5:10 am

  12. 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

  13. thanks

    abhilb

    24 Oct 11 at 11:11 pm

  14. [...] very good tutorial on how to create a remote git repository from a local one. Uncategorized ← MyBatis Type [...]

  15. awesome!! just what i needed..

    db

    15 Nov 11 at 6:57 pm

  16. 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

  17. @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

  18. 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

  19. @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

  20. 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

  21. Many thanks. Up and running in minutes.

    Rob Baltzer

    26 Dec 11 at 5:29 pm

  22. 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

  23. [...] 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 [...]

  24. You’ve saved me a lot of work.

    Thanks!

    Antonio Serrano

    17 Jan 12 at 1:41 am

  25. Thank you, it’s helpful

    kriom

    20 Jan 12 at 1:39 am

  26. 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

  27. @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

  28. wonderful! thanks a lot.

    mazimo

    1 Feb 12 at 7:12 am

  29. You can skip the last step from version 1.7 onwards – you just need to use:
    git push –set-upstream origin master

    Tim

    2 Feb 12 at 9:41 am

  30. Thanks a lot for this article! Although I am probably going to use a GUI such as Tortoise, this is still helpful for trying to learn more about GIT.

    Bitcoin Kamikaze

    19 Feb 12 at 7:28 pm

  31. I seem not to remember to write this stuff down somewhere (like in the root of where my git repository is!). You saved the day, excellent tutorial!

    Eric

    21 Feb 12 at 8:08 pm

  32. It isn’t clear to me whether the last step (the one that starts with “#Switch to origin/master so you don’t get any error about “fatal: Cannot force update the current branch.” is to be done on the local machine or the remote machine.

    Tom Jameson

    21 Feb 12 at 8:36 pm

  33. [...] Create a new Git Remote Repository from some local files or local git repository at A Waage Blog. [...]

  34. Thanks for this tutorial. It’s the best.

    Aymen Touzi

    21 Mar 12 at 11:04 am

  35. Hey there I only wanted to write and say I love to read your blog!

    Benton Vilaro

    21 Mar 12 at 4:29 pm

  36. Thank you SO much. I’m just learning git and this was very helpful :)

    Jonathan

    29 Mar 12 at 3:49 pm

  37. Great tut. Thank you very much!!

    dalson

    30 Mar 12 at 12:09 pm

  38. Thanks for the tutorial.
    Actually, a group-writeable repository can be created by
    git init –bare –shared=group

    Peter

    8 Jun 12 at 6:43 am

  39. I do this rarely enough to forget the steps, so this is one of those gems I keep bookmarked. Thanks!

    Art

    14 Jun 12 at 3:10 pm

  40. Perfect git recipe to add a new repository !

    Raghavan Parthasarathy

    7 Aug 12 at 7:46 am

  41. About the ‘git add *’ are you sure you didn’t meant ‘git add .’ instead?

    Thanks for the tutorial.

    Pedro Duarte

    13 Nov 12 at 5:52 am

  42. Yup! that’s better – thanks!

    Andrew Waage

    28 Nov 12 at 11:19 pm

Leave a Reply