Setting Up A Bare Remote GIT Repository

This document is a personal record of the steps required to set up a "bare" GIT repository suitable for use as a remote location in which to keep a shared project (or perhaps as a stable backup for a working copy). The document is not intended as a more general guide for others so it will likely be short on some explanations that would otherwise be useful.

It is assumed in the remaining discussion that the repository will reside on the same system that is used for the various working copies so network-related communication issues (including authentication/authorization) do not arise. Should a remote repository on a different machine be desired, then further configuration will be needed to address those issues. These are not considered in the present document.

There are two situations that ultimately arise: one where the working project is not already in a GIT repository and one where it is already contained in one. This document will address both situations.

In The Beginning

In all cases, the first item required is an empty bare GIT repository. To begin, select the desired location (directory) for the repository. The location should NOT already exist ... it will be created by the initialization command. Once the location decision is made, issue the following command:

    git init --bare <location>

where <location> is the given location.

Situation One

In the first situation, the working project is not part of a GIT repository. To begin, clone a copy of the empty bare repository previously created.

    git clone <location> <project>

where <project> is the name of the directory in which the cloned repository will reside. It is assumed that it does not already exist.

A message will be issued to the effect that an empty repository has been cloned.

Next, copy all project files from their present location into the directory that was just cloned. The actual statements required are left as an exercise as they will differ depending on the nature of the project. Since the project was not already part of a repository, GIT commands will not, of course, be used to populate the cloned directory. Otherwise, those command WILL mess up the cloned directory being built.

Make sure that everything is as needed. In particular, if there are files or directories in the project that should not be saved in the repository, then steps should be taken to make sure that doesn't happen. The best way to ensure this to use ".gitignore" files, put in the appropriate places in the project.

At this stage, add the desired files/directories into the repository.

    cd <project>
    git add .

To be safe, check the status of the repository:

    git status

If all is well, commit the added files/directories into the cloned repository:

    git commit -a -m <message>

where <message> is the descriptive message attached to this particular commit. Various update messages will appear.

Please note that if a "git status" command is issued at this point, the following response will be received:

    Your branch is based on 'origin/master', but the upstream is gone.
      (use "git branch --unset-upstream" to fixup)
    nothing to commit, working tree clean

The comment about the upstream being gone can be ignored and the suggestion need not be taken. However, taking the suggestion also does not do any harm.

The final step now left is to push the contents of the cloned repository to the remote (bare) empty repository created at the beginning:

    git push origin master

Once again, update status messages will appear. When finished, the remote repository is completed and can be used to clone new copies which will now actually contain project files.

Situation Two

In the second case, the project is already part of a (local) git repository, possibly with or without an associated remote repository. In this case, the situation is actually much simpler.

Once the bare repository has been created, change into the project directory. The first step is then to see if a remote repository already exists for the project:

    git remote

This will return the name of any associated remote repository. If nothing is returned, then no remote repository is tied to the project. Use of the "-v" option at the end of the command will also identify the location of the remote repository.

If there IS a remote repository, remove it:

    git remote remove <name>

where <name> is the remote repository name returned by the previous command. The previous command can be used to make sure that it is gone.

Now, add the bare repository created earlier as the remote repository for the project:

    git remote add origin <location>

Once again, the "git remote -v" command can be used to make sure that it succeeded.

The final step now is to push the project data to the remote repository:

    git push --set-upstream origin master

The remote repository is now complete.

Final Notes

This has, of course, been a very simplified discussion designed to explain how to perform a very specific setup within the GIT environment. The commands identified in the discussion have far more options than those presented. There are a variety of sources available that are worth exploring in order to fully appreciate to power and usefulness of the GIT system.