How to set up a local git repository

In recent weeks I have become more involved with using git and github for version control. For my spare time projects I work on an iMac and a Mac Book Pro and I wanted a way to enable me to work on the same project on either machine. Setting up a local git repository was the ideal route.

Now I know some will think why not just have an account with GitHub instead of going local? Well I didn’t want the expense or for these to publicly available. Also, I didn’t need one as I generally work on the same local network just on different machines.

Step 1 – The scenario
As I said I work on an iMac and a MBP, both machines are running Lion, not that this has a baring on matters. I have a 250GB spare hard drive that I set up as a Time Machine back up disk some time back and thought this would be just the thing to house my projects. I enabled file sharing on both machines and ensured I could access each one from the other. To note the external drive I attached via usb to the iMac.

Step 2 – Remote setup
The first step was to initialise git on the drive. This requires that you have the knowledge to install git on a mac, if not its easy, google it! To initialise git on the back up drive I found this the best way:

a) Open terminal (on mac) and navigate to the external drive. Create a folder for the project,

mkdir -p myproject

b) Enter the folder and create a new folder called .git

mkdir -p .git

c) Now enter this folder and run the following command

git --bare init

You now have an empty git repository ready to be pushed to and pulled from.

Step 3 – Local setup
We now need to set up the first instance of the project on one of the machines, the iMac will be first. Navigate your way using terminal to the project folder on the iMac, if you need to set it up you can use the same command as I used in step 2 part a. Enter this folder and run the git init command

git init

You now have an empty repository. I am assuming here that you have a project ready to be pushed so the next part is to add all the files. At this point you can either add all the files or create a git ignore file. This is up to you depending on the project and which files you do not want to pass. Things like cache and log files are best to be ignored. To create the git ignore file run

vi .gitignore

You can now add the folders and /or files here and save.

Step 4 – Adding the remote repo and project
Before we add the files we need to add the remote repository on the external hard drive. Run

git remote add NAME /PATH/TO/EXTERNAL/DRIVE/REPO.git

The name part here is what you want to call the repo, I generally use local for locally stored repos, makes sense. We are now ready to add the project, run

git add .

and then commit using

git commit -am "Add a comment here"

I appreciate there maybe other ways to do this, I am just using one method that I find works best for me.

Step 5 – Ready for the push.
We are now ready to push the local files to the remote repo on the external drive. Run

git push NAME master

We are using the master branch here, we haven’t yet created any branches so this is all we have. You will notice files being pushed to the local drive and once done we are ready to set up the other git repository on the second machine and pull to it.

Step 6 – Second machine
This is fairly straight forward as all the steps have been done already so its a case of repeating them. On the 2nd machine follow these steps.
a) navigate to the project folder and run the following commands

git init
git remote add NAME /PATH/TO/EXTERNAL/DRIVE/REPO.git

1 And thats it.

Step 7 Notes
it is worth remembering that if you have add files/folders to the .gitignore then these will need to be created. Some other projects I work on use systems such as Symfony2 and CS Cart which do have cache and logs that do not need to be kept so the folders which these are stored in are ignore and thus need to be created on all machines where the application is being developed.

Tags:

No comments yet.

Leave a Reply

All comments are moderated before being published!