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 65 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://

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

65 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!


    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.


    8 Apr 11 at 12:09 pm

  5. special tnx for this tutorial


    25 Apr 11 at 2:52 am

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

  7. Very good tutorial, thanks a lot!


    7 Jun 11 at 3:47 pm

  8. Very helpfull, thanks!


    23 Jun 11 at 3:30 pm

  9. Great writeup, thanks.


    27 Jun 11 at 11:43 pm

  10. Great tutorial … thanx!


    19 Jul 11 at 8:35 am

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


    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!


    13 Oct 11 at 1:01 am

  13. thanks


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


    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.


    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:

    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 (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!


    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!


    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.


    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.


    Antonio Serrano

    17 Jan 12 at 1:41 am

  25. Thank you, it’s helpful


    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.


    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


    31 Jan 12 at 4:25 am

  28. wonderful! thanks a lot.


    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


    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!


    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 :)


    29 Mar 12 at 3:49 pm

  37. Great tut. Thank you very much!!


    30 Mar 12 at 12:09 pm

  38. Thank you so much! So happy to find this :D


    29 Apr 12 at 6:23 pm

  39. git remote add origin ssh://

    “ssh://” should not be required.


    3 Jun 12 at 7:23 am

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


    8 Jun 12 at 6:43 am

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


    14 Jun 12 at 3:10 pm

  42. You should use git add . instead of git add *

    with * you won’t pick up .gitignore file.


    20 Jun 12 at 6:08 am

  43. When I get to the ‘git push -u’ step I get an error.
    connect to host port 2227: connection refused
    fatal: the remote end hung up unexpectedly

    I have opened port 2227.

    Any ideas on what’s causing this?


    22 Jun 12 at 5:37 am

  44. Super helpful, thanks!


    2 Jul 12 at 9:38 am

  45. Helped me too. Thanks.

    Duncan Scott

    3 Jul 12 at 7:02 pm

  46. [...] The first thing to do is to add the node.js code to a git repository. I always forget this process. This blog post is as good as any for showing you what to do on your local machine, beginning with git [...]

  47. Perfect git recipe to add a new repository !

    Raghavan Parthasarathy

    7 Aug 12 at 7:46 am

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

  49. Yup! that’s better – thanks!

    Andrew Waage

    28 Nov 12 at 11:19 pm

  50. Thank you for good tutorial,

    Worked perfect.


    3 Dec 12 at 3:48 pm

  51. I found this helpful. Thanks :)

    Adam McKee

    20 Dec 12 at 1:15 am

  52. [...] Create the remote repository [...]

  53. Thanks buddy.

    Nick Wiedenbrück

    19 Jan 13 at 2:18 pm

  54. After you
    1. cd and do
    2. git init

    make sure you have .gitignore in place with all relevant entries, so that you do not have to go later and remove those .project and .settings files you may have in an Eclipse project from the remote repo.


    29 Jan 13 at 2:49 pm

  55. Nice tutorial.
    Thank you.

    Sergey Triputsco

    5 Apr 13 at 2:32 am

  56. [...] up version control etc. is outside the scope of this tutorial, but here’s a good one for Git. [↩] This entry was posted in Web and tagged Tutorial, VirtualBox, [...]

  57. thanks for the nice post… i am beginner in git it really helped me a lot… cheers!!!


    17 Apr 13 at 8:27 am

  58. This was very helpful . I have an older version of git.


    23 May 13 at 1:06 pm

  59. Very nicely done Andrew. This helped me quite a bit vs. the slew of online manuals and quickstarts. This is by far the best quickstart I have come across.


    Mike Cotrone

    14 Jul 13 at 6:33 am

  60. works wonderfully. thanks a lot.


    7 Aug 13 at 5:35 am

  61. I did all that and push are said to be successful and i couldn’t find the repo in the bare repo, why its not there although i pushed the master?


    23 Sep 13 at 8:12 am

  62. Helpful, thanks !


    3 Dec 13 at 8:35 am

  63. Worked brilliantly. Thanks


    28 Mar 14 at 10:48 pm

  64. Thanks a lot! It really helped me!)


    6 Apr 14 at 1:10 pm

  65. On gitorious it can be done this way because:
    Sorry, Gitorious doesn’t provide shell access. Bye!


    4 Jan 15 at 8:06 am

Leave a Reply