Search code examples
gitrepositorygit-remote

How to create a new 'Local' remote repository for an existing project with Git


I have just started a new git project that is so far stored on my local Mac. Now that the project has a few revisions checked into it, I would like to push these changes to a network location (I have a Time Capsule in my local network, where I wish to maintain the master copy of all my code, instead of pushing to Github).

I'm currently reading the Pro Git book, but there are some points that aren't explained. The main point that I need explaining is what's the difference between a git project and a git repository; or are they the same thing?

This is the structure I would like.

my remote root location (over local) for all my projects will be:

/Volumes/Capsule/dev/github/...

under here I would like my projects, eg:

/Volumes/Capsule/dev/github/canary
/Volumes/Capsule/dev/github/guinea

So would canary & guinea be repositories or projects?

I know you can add a remote using the command of the form: git remote add

The example I'm working from is:

git remote add local_proj /opt/git/project.git

but what is project.git? (or is this a typo? shouldn't it be .../project/.git)

My canary project root folder is ~/dev/github/canary and the git administration files are in ~/dev/github/canary/.git

so what is the equivalent command for the canary project?

I tried the following from ~/dev/github (canary is in this folder)

git remote add canary /Volumes/Capsule/dev/github/canary.git

but got this error message:

fatal: Not a git repository (or any of the parent directories): .git

The reference that you specify in the git remote add, is that per project or per repository? Would I have 2 difference references for canary and guinea, or is there just 1 remote locations under which both canary and guinea be referenced?


Solution

  • So, after much experimentation I found the solution to this quite simple problem that doesn't appear to be explained adequately anywhere in a single location.

    (There is potential for confusion with 'local' terminology. For the purposes of this article 'local' is just referring to local location of a project on mac's internal disk as opposed to a network location. 'Local' (capitalised 'L'), refers to the protocol. The remote repository will be created using protocol Local, as opposed to ssh or http for example).

    local project location: ~/dev/github/...
    remote location: /Volumes/Capsule/dev/github/...
    

    So just to re-iterate, the problem is a project needs to be pushed to a remote location; that remote location/repo doesn't actually exist yet.

    The first thing you have to do is create the remote location. So continuing the example above, I need to create the remote canary project at the remote location. Also, since this remote location is not a working directory, it should be created as a bare repository:

    1) Create the remote folder:

    cd /Volumes/Capsule/dev/github
    mkdir canary.git
    

    (NB as indicated on page 132 in Pro Git 2nd Ed, bare repositories are named with .git suffix, eg canary.git)

    2) Initialise the remote location:

    cd canary.git/
    git init --bare --shared
    Initialized empty shared Git repository in /Volumes/Capsule/dev/github/canary.git/
    

    (The --shared option is required to ensure that the correct write permissions are created properly on the remote path allowing you to push to it without access errors occuring)

    The remote bare repo is now created and can now being configured as a push target for your local existing project (canary).

    3) Add the remote repo to local project canary

    go back to your local project

    cd ~/dev/github/canary
    

    add the remote using git remote add

    git remote add origin /Volumes/Capsule/dev/github/canary.git
    

    and check what you've done

    git remote -v
    origin  /Volumes/Capsule/dev/github/canary.git (fetch)
    origin  /Volumes/Capsule/dev/github/canary.git (push)
    

    'origin' is now the remote reference to your local canary repo.

    4) Push local canary project to the remote

    git push origin master
    Counting objects: 3, done.
    Delta compression using up to 8 threads.
    Compressing objects: 100% (2/2), done.
    Writing objects: 100% (3/3), 281 bytes | 0 bytes/s, done.
    Total 3 (delta 0), reused 0 (delta 0)
    To /Volumes/Capsule/dev/github/canary.git
     * [new branch]      master -> master
    

    And there you have it, a local project pushed to a new Local remote location.