Git‎ > ‎

Start a Repository

The general process of making a new git repository is to make the first repository and either check in a dummy file, some code that already existed, or perhaps a brand new file.  From there, you usually progress to making a bare repository elsewhere and letting others have access to it somehow.

Starting with a Working Repository

A working repository (alternately called a working copy) is a repository that has all of the files you expect to see.  If you are working on a web site, you'd see a bunch of *.html, *.jpg, *.png, *.css, and other assorted website files.  If it was not a working repository you would only see the various metadata files that git needs to track all history.

Start With A Blank Directory

This is the simplest option by far.  If you don't have any code you want to check in, just make a directory and use git init inside this empty directory and presto, you are done.

Use Existing Codebase

If you have an existing codebase, go to the top level of your application and use git init to create the metadata for the repository, then check everything in.  A quick and easy way to check everything in is to use git add . and then git commit -m 'Initial commit' to add all of the files in one sweeping action.

Strengths Of Just Using A Working Repository

With this option you can commit, find differences, have a revision history, and benefit from all sorts of tools without having any overhead of pushing and pulling.  This type of repository is ideal for a single person on a computer that gets properly backed up regularly.  One can clone from this repository if you have a team member that is also working on the codebase, but you can not have the team member push into the repository; you should have a bare repository that you both work off of.

If you have any intention to have multiple people working on the repository or if you like to have another copy of the code elsewhere to help guarantee that nothing goes wrong with your project, I suggest using a bare repository instead.

Moving Up To A Bare Repository

This is the typical way of using git; people will make a bare repository and push code into it and pull changes from it into their repositories.  A bare repository has special restrictions to help keep things in order.  The easiest way to start up a bare repository is with a working copy that you push to a bare repository.

First, make your working repository per the instructions above.  Have at least one commit made before you start.  For the instructions, let's assume your project is in the directory my_project/ and we want to make my_project.git/ (bare repositories typically have directory names that end in .git).  From the directory just above your project, run these commands:

git init --bare --shared my_project.git
cd my_project
git remote add origin ../my_project.git
git push -u origin master

These instructions will change your working copy's origin to point to the new bare repository so you can just use git pull and git push to get/send history to the bare repository.

I specified the --shared flag in the instructions above.  It isn't needed if your account is the only one accessing the files, but it does help if you use group-level permissions to grant others access down the road.  It makes directories and files group writable.  It typically won't harm anyone and will help out in for projects where everyone can be put into a group and given access to the files directly on the machine.

You don't need to use local directories.  You can just as easily use git init on another machine and get the files copied over using ssh, HTTP, or a git server.

Clone From Another Repository

Our final way to get a repository started is to clone it from somewhere else.  If you are doing this, then there's probably some URL that you have which points to a git repository.  I'll use the public URL for the PHPTools repository here.  You issue one command to pull down a complete copy of the history and set up your working repository.

git clone https://github.com/chrismcmacken/phptools.git

This checks out the repository to the phptools directory (the .git extension is automatically removed).  Again, you can use a local directory path (/usr/share/git/my_project.git), or a URI that describes an ssh connection to another machine and a path on that machine (some_user@another.machine.net:/usr/share/git/my_project.git).
Comments