How add git to an existing project

Submitted by luxian on Tue, 12/05/2017 - 08:51

Recently I started working on web project that had no versioning system added and to make things worse, the project had two versions running in production. 

The first thing I wanted to do was to add everything to git. Here are the steps I took to accomplish that:

  1. Copied all the code (both version) on my local
  2. Created a new folder and initialize a new git repository in it by running
    git init
  3. Created a new branch version1 and committed everything as "Version 1":
    git checkout -b version1
    git commit -am "Version 1"
  4. Copied older version files into that folder
  5. Cleaned up folder by  deleting everything except .git folder. This way you will be able to track files deleted in the newer version.
  6. Copy newer version files into that folder
  7. Create a new branch and commit everything as "Version 2"

After this, I was able to continue to work normally. I used Version 1 as a main development branch, and most of the times my changes/fixed can be ported to Version 2 by a simple merge.

My next challenge was to deploy this on production with no downtime. During the first phase (create git repository) I also did small changes (cleaned up things) and I was a little concerned that things might break if I put them on production directly.

Cloning the git repository into a new directory on production server and switching the webroot was too hard, so I come up with a different approach:

  1. Initialized a new git repository in the current webroot git init directly on production
  2. Checkout to a new branch: git checkout -b legacy
  3. Add everything and commit: git commit -am "Initial version"
  4. At this point everything that's on production webroot is in git. In case something goes wrong I can checkout to this branch to revert my changes.
  5. Add the remote repository: git remote add origin
  6. Make sure you have all the references from remote server: git remote fetch origin
  7. Now you have to switch to remote branch, but still want to evaluate the changes one by one, to do so run git reset origin/version1
  8. At this stage:
    • no files where touched or changed
    • you can run git status to see all changes compared to remote ¬†(Version 1 from git)
    • you can switch to branch version 1 by running git checkout version1
  9. I ran through all the differences showed by git status. That's how I realized some files where missing from git repository (an exclude rule when copied things on my laptop) so I committed them directly from prod server. All the changes that I wanted to apply were applied one by one with git checkout -- [filepath] and tested afterwards
  10. In case something didn't work as expected I could always return to initial version by switching to legacy branch. Wasn't the case so I deleted that branch after everything was working and missing files were committed to git.

source: how to switch to git branch without files checkout

Add new comment

Restricted HTML

  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.