Search code examples
gitremote-servergit-remote

Creating a remote Git repository and populating it with files already stored in the remote location


I'm new to Git and I'm going to start developing new modules for an existing web application. This application is currently hosted on a web server and I would like to use Git as a versioning tool. I have created a new bare repository using git init --bare newdev.git but I don't know how to add the files which are already on the server.

Thank you.


Solution

  • Whoa, hold on there. Let's back up some. You're "new to git" so you need some basics! :-)

    For your purposes, there are two kinds of repositories:

    • A "regular" repo, in which you work. You and everyone else make your own versions of these.
    • A "bare" repo, which is used only for you and other people to coordinate work. There is only one of these.

    A "bare" repository has no place in which anyone can do any work. That's what makes it "bare"—and what makes it a safe place to "push" to. The problem with pushing to some non-bare repo is this (git actually stops you, with error: refusing to update ..., but it's what the problem would be if git let you do it):

    1. Alice is working in her repo. She checks out some branch and edits file config.
    2. Bob is working in his repo. He checks out the same branch and also edits config.
    3. Bob finishes editing config first, and pushes his changes to Alice.
    4. Alice's changes are wiped out, replaced with Bob's changes. Ouch!

    By not letting anyone work "in" it, a bare repository sidesteps this problem.

    There's still one other problem, which is: how do you get this thing—the bare repo—started in the first place? It has no place to work!

    The answer is: you don't start there, you start with your own repo, that nobody else has a copy of. You write some initial version in there. Then you create a new bare repo, on the shared server, by copying the one you had:

    sharedserver$ cd /place/where/bare/repos/go
    sharedserver$ git clone --bare ssh://cosmins.machine.name/path/to/initial project.git
    

    Now "sharedserver" has the bare repo in /place/where/bare/repos/go/project.git, and you (and Alice and Bob and so on) can all git clone that.

    (The "shared server" can be something like github, which has a different method of creating the shared repo from your original initial non-shared one. I don't use github and can't really say anything about that.)

    (It is possible to convert a regular, non-bare, repo into a bare one, or vice versa, but it's a bit of a pain. Just create the bare one by git clone --bare.)


    Another method of side-stepping the "work gets overwritten" problem is not to use push at all. If everyone co-operates in a peer to peer sharing system, then Alice can pull from Bob and Cosmin, and Bob can pull from Alice and Cosmin, and so on. Note that this results in a very large number of people to pull from, though, which is why shared-servers come about. You can all just meet there, instead of always having to go to each others' homes.