1

I have a Ruby on Rails server with Development and Production environments running on it. I have recently created a remote GitHub repository based on the Development code, and now I want to deploy this code to Production.

I thought that using git clone would be the way to do this, but this gives:

fatal: destination path already exists and is not an empty directory.

The target directory is indeed present and non-empty, because my (currently ummanaged) Production code lives there. I thought that using git clone would allow me to write my master code into my Production environment. What am I not understanding?

Neil
  • 21
  • 3

2 Answers2

1

Additional information based on comments:

Some comments have clarified that the production directory cannot be deleted as I originally suggested. Furthermore, the code in the production directory is identical to the code in the development directory. This suggests creating a git repo directly in the production directory and then cloning it as the development folder.

On the production machine:

$ cd <production directory>
$ git init
$ git add .
$ git commit

On the development machine:

$ cd <parent of development directory>
$ rm -rf <development directory>
$ git clone <URL to the production directory>

Note I am assuming that the current development repo does not contain any history which you want to keep. If it does, you can instead add the new production repo as a remote and merge the histories.

On the development machine:

$ cd <development directory>
$ git remote add prod <URL to production directory>
$ git checkout master
$ git pull prod master

Original answer:

You should delete the production directory and recreate it as a git repo. Then connect your development repo the production repo.

One way to do this is simply clone as you have done:

$ cd <parent of production directory>
$ rm -rf <production directory>
$ git clone <development directory or URL> <production directory>

This might be difficult if the development directory and production directory are on different machines, depending on the network configuration. Even if you do this, you will need to connect the development directory to the production one in order to push updates:

$ git remote add prod <production directory or URL>

Since you need to do this anyway, you can do it from the beginning. On the production server. First create an empty production repo:

$ cd <parent of production directory>
$ rm -rf <production directory>
$ mkdir <production directory>
$ cd <production directory>
$ git init

Now push the development code to production:

$ cd <development directory>
$ git remote add prod <production directory or URL>
$ git push prod master

Note that the location of the production code is mostly irrelevant. You configure a so-called "remote" with this location. This means that the directories do not need to have the same name.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
  • I don't think that there is any need to delete the production directory. Without deleting, the code in production directory can be linked to git directory. Discussion at https://stackoverflow.com/questions/3311774/how-to-convert-existing-non-empty-directory-into-a-git-working-directory-and-pus might be more relevant answer to the question. – Ayushya Jul 04 '17 at 17:18
  • @AyushyaChitransh The proposed solution in your link is to do `git init`, `git add .` and `git commit`. This is fine if you are starting from scratch. However, since the OP has an existing repo with some commit history, starting another repo with the same content but a different history will cause many headaches when configuring a remote and pushing. – Code-Apprentice Jul 04 '17 at 20:02
  • I agree, it is not clear in the question, whether the existing directory is a **git** repo or not. If it is, then I believe your answer is more appropriate, if not, then there wont be any need to delete the existing directory, and doing `git init`, `git add .` and `git commit` would be a better option. – Ayushya Jul 05 '17 at 03:22
  • @AyushyaChitransh From my reading there are two existing directories: one with a git repo and one without. The OP wants to make the one without a repo into one. Further, he wants to be able to push from the existing repo. init-add-commit will create a new repo in that second directory with a commit history different from the existing repo in the first directory. – Code-Apprentice Jul 05 '17 at 08:12
  • I don't like the idea of removing the production directory, but if OP is comfortable with it then it's OK. I guess, there should be a way to get that done without deleting production directory. Would it be better to use this: `git init` `git remote add origin PATH/TO/REPO` `git fetch` `git checkout -t origin/master` – Ayushya Jul 05 '17 at 08:25
  • @AyushyaChitransh If the OP has not made any changes yet in the development directory, that sounds like a better option. – Code-Apprentice Jul 05 '17 at 16:23
  • @AyushyaChitransh to clarify, the existing Production directory contains code which is not yet a git repo. Also, the Production directory contains more than just code - it also contains logs and media which cannot be deleted. Finally, the Production code is currently exactly the same as the Development code which is a repo and is being managed using Git. With all this in mind, I'm still not sure if any of the solutions above are quite what I need. Thanks to both of you for your help so far. – Neil Jul 06 '17 at 14:33
  • @Neil How many commits have you made in the repo for the development code? Is there only a single commit? Or have you made several commits? – Code-Apprentice Jul 06 '17 at 15:55
  • @Code-Apprentice I have made just a few commits, as part of getting this set up. I am happy to delete the remote repo and start over if required. Your revised answer looks like it will do the job. I will just need to backup any non-code files and directories in the Development environment before deleting everything, and then restore them afterwards - I guess I'll do that using gzip. Thank you! – Neil Jul 06 '17 at 18:27
  • @Neil My "Additional information" section should work without needing to delete any non-code files since it creates the repo on the production server and deletes the existing repo from the development machine. However, I left out the step of creating a `.gitignore` file that ignores all of the non-code files that you don't want in the repo. – Code-Apprentice Jul 06 '17 at 19:48
  • @Code-Apprentice and AyushyaChitransh, many thanks for your help. I have now got this working. My solution was to create a brand new remote repo based on a local Production repo. I then zipped up my Development directory and deleted it. Then I cloned my repo into Development, and restored the rest of the directory by unzipping my backup and choosing to not overwrite any files that had been generated from the clone. – Neil Jul 08 '17 at 11:23
0

Method 1: Your git repository has the same name as the folder which contains "unmanaged" Production code. Either rename the existing folder and then clone.

OR

Method 2: perform clone in another directory.

OR

Method 3: Best you can do is to specify a destination directory:

git clone <Repo> <DestinationDirectory>

OR

Method 4: As you mentioned that it is the same code, then you can just attach the existing git repository as given here and in the example shown below:

cd <localdir>
git init
git add .
git commit -m 'message'
git remote add origin <url>
git push -u origin master

Edit 1: Added method 4.

Edit 2: Modified git instructions so that they don't create any conflict when new code is combined to the code present in remote git repo. But as comments suggested, they would end up with errors, so removed them.

Edit 3: Changed back to original version of method 4.

Ayushya
  • 9,599
  • 6
  • 41
  • 57
  • Hi Ayushya. I can't rename the existing Production folder because that is the code which runs the website. If I perform clone in another directory or specify a different directory, then the repository it creates won't be linked to my Production code will it? Maybe this is what I'm not understanding properly. Grateful for any further help you can give. – Neil Jul 04 '17 at 16:23
  • @Neil I have updated my answer and along with that included a link which can help you better. – Ayushya Jul 04 '17 at 16:34
  • @Neil the directory names do not matter. You can clone your development repo to any directory you want and the settings in git will link them – Code-Apprentice Jul 04 '17 at 17:00
  • The most recent version of method 4 will most likely fail with an error. Something like "untracked files would be overwritten". – Code-Apprentice Jul 06 '17 at 16:15
  • Thanks @Code-Apprentice I tried it and yes, it behaved exactly as you said. So I have reverted back to original version of method 4. And your answer is well described and should be able to help Neil so I haven't made any further changes in mine. – Ayushya Jul 06 '17 at 17:11