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

StackOverflow https://stackoverflow.com/questions/21464722

  •  05-10-2022
  •  | 
  •  

Question

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.

Était-ce utile?

La 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.

Autres conseils

What you want to do is just git init (no --bare) in the folder where the existing files are.

Then git add . (or git add filename.ext for individual files). Finally git commit.

Once you have your files committed, you can use git clone to get the repository onto another machine, probably through an ssh url, like git clone ssh://example.com/path/to/repo/.git ./


Edit from comments:

You cannot push from one repository to another if both are on the same branch. You have three options:

  1. Check out a different branch on the repository you are trying to push to
  2. Make the repository you are trying to push to a "bare" repository
  3. pull changes from the source repository to the target repository

Out of the three options, option #3 is the most common practice when using git.

Option #2 is more similar to non-distributed systems like Subversion, or git systems with an "authoritative" copy of the code, like Github.com.

Option #1 is really a hack and shouldn't be used unless you really have no other choice.


To create a bare repository from an existing one, you can either manually convert the repository in place (existing SO question w/good answer), or you can git clone --bare /path/to/my_repo/.git ./my_repo.git to create a new clean bare repository. I would recommend the second option, it is much safer/cleaner to do and you don't mess up your existing repository, but you can do whatever seems best.

Yes, git init in the sources folder, add the files you want to the repository then clone the repository to the local machine. Since you already created a bare repository you should move the .git folder out of the way and you can delete it in the end when you are sure you don't need it.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top