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:
- Copied all the code (both version) on my local
- Created a new folder and initialize a new git repository in it by running
- Created a new branch
and committed everything as "Version 1":
git checkout -b version1 git commit -am "Version 1"
- Copied older version files into that folder
- Cleaned up folder by deleting everything except
.gitfolder. This way you will be able to track files deleted in the newer version.
- Copy newer version files into that folder
- 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:
- Initialized a new git repository in the current webroot
git initdirectly on production
- Checkout to a new branch:
git checkout -b legacy
- Add everything and commit:
git commit -am "Initial version"
- 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.
- Add the remote repository:
git remote add origin firstname.lastname@example.org/...
- Make sure you have all the references from remote server:
git remote fetch origin
- 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
- At this stage:
- no files where touched or changed
- you can run
git statusto see all changes compared to remote (Version 1 from git)
- you can switch to branch version 1 by running
git checkout version1
- 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
- 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.