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

Archive for the ‘remote tracking’ tag

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