5

I have decided that it's time for me to start using Git on a PHP project that I have been developing casually for over a decade. (Please, no lectures from the version control police!) Due to the complex setup required on my VPS to do everything the project needs (esp. single-codebase-multiple-client structure and a Japanese-capable installation of TeX to create specialty PDFs), it is not possible to set up a development environment on my local Windows box. But I do have a testbed area on the server that I can play in, so it's my development area. Currently I use Filezilla to access the server and open files directly into Notepad++, and when I'm ready to see my edit in action, I just save and let Filezilla upload. When everything looks good on the testbed, I copy the files to the production codebase area. Yeah, that gives me no history of my changes other than my own comments, and I have to be careful not to mix bug fixes with half-finished new features. I can see the value of Git's branches for different upgrades in progress.

Yesterday I got my toes wet. First I created a Github account, and then (at the recommendation of a tutorial) installed Git For Windows (with its own Bash and tiny-looking GUI) and Kdiff3, and followed some instructions for configuring Git Bash. After all that, though, I ended up having to install something else in order to interface with my Github account (appropriately named Github for Windows), which seem to do all the stuff the other two programs were supposed to do for me. Anyway, then I did a simple task as my first foray into the Github world - I had added functionality to someone else's jQuery plugin and wanted to share it with the developer, so I forked his repo, cloned it to my machine, overwrote the file I had previously edited and tested, synced to my Github account, and sent a pull request. All the terminology in that last sentence was brand new to me, so I was pretty proud of myself that I got that far. ;) But I guess I only needed the Github software, not the Git software - it's hard to know what tutorials to believe.

Anyway, now I want to figure out a workflow for my own stuff, which is my actual question for you guys. From what I can tell, having the master repo anywhere but the public Github costs money, and I don't care if others see my code (I don't expect anyone else to work on my oddball project made of spaghetti code, but if they want to, that's great). Okay, but then what? Perhaps one of these scenarios, or something else:

  1. Clone branches of the repo to my PC, do edits on the local files, and upload them in Filezilla for testing (a couple more clicks than my current workflow because Filezilla doesn't automatically see the relationship between the local file and the remote file, but not a big deal). Then when I'm happy with the code, commit locally, sync to Github, and copy the files (from somewhere - not sure on this point) to the production area.

  2. Install the Linux flavor of Git on my VPS so that the "local" Git file location is the testbed, and use Git through PuTTY to do the local commits. Simpler for file structure (no need for a copy on my PC at all) but more cumbersome to use Git:

    • I'm not on PuTTY very frequently, and for some reason the connection often dies on me and I have to restart.
    • Even though the Linux command line is Git's native habitat, I am probably more comfortable with a GUI (because I forget command syntax quickly - old brain, I guess).

    Also, since I never ended up using the Git program I installed here, I'm not sure whether it would be Git or Github I would be using on the server.

  3. Some other scenario, since neither #1 or #2 uses Git/Github to manage the production file area at all, which would probably be a good idea so that I don't forget to copy everything I need.

I tried to research the possibility of a PHP-based GUI to go with idea #2 (so I don't have to use PuTTY for day-to-day operations), but it seems that the discussions of such tools all assume either that you are trying to create your own Github service, or that the "local" cloned repo is physically on your local PC (with xAMP running on whatever OS it is). But maybe the Github software I used is enough to do all that - it's hard to tell. I don't yet understand the interplay between a master public repo on Github, branches somewhere (on Github also?), at least two sets of files on my web server (the testbed and the production area), Github software, Git software, and the keyboard/screen of the computer I'm sitting at.

So pardon my newbie ramblings, but if someone out there has a similar development situation, What's your workflow? Or what would you suggest for me?

OsakaWebbie
  • 645
  • 1
  • 7
  • 21
  • 2
    TLDR - but if you need a local setup to mirror your remote one, check out vagrant. – Paul Dixon Apr 03 '13 at 08:55
  • It's always possible to set up a development environment, and it's the first thing you should look into. VirtualBox and vagrant will help. – deceze Apr 03 '13 at 08:58
  • Although you said that you don't mind your git repos being public in github, you can have private git repositories on http://bitbucket.org (up to 5 developers IIRC) – Carlos Campderrós Apr 03 '13 at 10:37
  • Paul: I don't yet know what I need - that's why I explained my situation. Sorry it was too long for you, but I look forward to hearing from people who DID read it. deceze: Sorry I said, "it's not possible". It's probably possible for a Linux guru, but it took me months to get it all working on my VPS. I can't copy my entire VPS to a "box", so I would have to install CentOS and most of the other stuff, in the same versions (and then keep them in sync). I don't even remember how I did most of it (esp. TeX). It doesn't seem worth it just to be able to test offline - am I wrong? – OsakaWebbie Apr 03 '13 at 10:39
  • Carlos: Yeah, a few minutes ago I saw a tutorial of using only git completely locally - I didn't realize that possibility (I keep confusing git and github). But I don't know if that's the wise way to go or if I should keep my VPS's code store in the loop somehow. – OsakaWebbie Apr 03 '13 at 10:58
  • @Osaka If you are not able to reproduce your setup, that should be ***extremely worrying***. If *anything*, anything at all, happens to your live server... you're screwed? If the hard disk it runs on craps out (and it *will* one day), you're simply screwed? You're much better off investing some time to be able to reproduce your setup than to scrap 10 years of work on the whim of a piece of hardware. You can create scripts that automate the setup of a server. That's a one-time investment with infinite payoff. Look into Ansible, Chef, Puppet or similar tools. – deceze Apr 03 '13 at 15:09
  • I guess I'm depending on the hoster's backups too much - I hear you, and I'll look at tools. I'm just spread way too thin - I can't keep up on new standards and expectations (I'm still a jQuery/AJAX novice at best, my PHP code isn't OOP yet, and a few pages even still have table-layout!), the to-do list of things I've promised my users gets longer instead of shorter, I'm weak at sysadmin (as y'all have surmised by now), I've never had time to write user docs, etc. (I do this part-time, volunteer, and my last formal schooling was in the 80's.) Stop the world spinning - I need to catch up! :-o – OsakaWebbie Apr 03 '13 at 15:37

3 Answers3

3

Here's one way to aproach the issue:

You will need three repositories:

  • a local repo to edit code. [1]
  • a bare remote repository on your server. This will be in a location that in not publicly viewable, but you can ssh in to. [2]
  • The production environment. [3]

Here's the implementation:

workstation$ cd localWorkingDirectory/
workstation$ git init
workstation$ git add .
workstation$ git commit -m 'initial commit'
workstation$ ssh login@myserver
myserver$ mkdir myrepo.git
myserver$ cd myrepo.git
myserver$ git init --bare
myserver$ exit
workstation$ cd localWorkingDirectory/
workstation$ git remote add origin login@myserver:myrepo.git
workstation$ git push origin master

every time you make a commit on any branch, back it up with:

workstation$ git push origin BRANCH

When you are ready to move branch version2 into production: do this

workstation$ git push origin version2
workstation$ ssh login@myserver
myserver$ git clone path/to/myrepo.git productionDirectory
myserver$ cd productionDirectory
myserver$ git checkout version2

Oh no! It dsoesn't work! better switch back to version1!

workstation$ ssh login@myserver
myserver$ cd productionDirectory
myserver$ git checkout version1
Hal Canary
  • 2,154
  • 17
  • 17
  • Thanks for the examples - I don't understand every command yet, but I get the gist of the flow. You even included a way to do dev->production if I'm careful with branches. Question: What's the relationship between your #2 repo and the dev testbed directory? Since I'll be doing my testing (including the trial-and-errorrrrr type done long before any commits are needed) in the dev dir (at least for now, until troelskn and Nick convince me to recreate it locally [wink]), what if I just do my "local" repo there? (i.e. just have #2 and #3 but not #1) – OsakaWebbie Apr 03 '13 at 15:10
  • 1
    repo #2 is a bare repo (look up what I mean by that); this makes it easier to push to from your workstation. Make your testbed directory another clone of repo #2. – Hal Canary Apr 03 '13 at 15:39
  • Haha! I googled "bare repo", and [the first page I looked at](http://www.saintsjd.com/2011/01/what-is-a-bare-git-repository/) not only explained it nicely, but check out the comments - Luis sounds like he's doing exactly what I'm doing! You still mentioned my "workstation" (presumably meaning my local PC), but right now while I don't yet have a local dev environment, is there any reason to pull/push anything to/from my PC? "*#1: bare, #2: clone to dev, #3 clone to production*" sounds pretty good to me. – OsakaWebbie Apr 03 '13 at 16:24
  • 2
    “…is there any reason to pull/push anything to/from my PC?” Not really. I like to use a local editor because it is more responsive. It also means that there is an additional redundant copy of the entire repository somewhere. – Hal Canary Apr 03 '13 at 17:28
  • I too use a local editor - Filezilla handles the download/upload internally with a temp folder. In Filezilla I right-click on the remote filename and choose View/Edit, and the file appears in Notepad++. After I save my changes, when I change focus to the Filezilla window it automatically offers to make the same save on the remote side. I can solve the redundancy issue simply by making a habit of backing up the repo when I checkout to production. I do something similar to that now, but it's a little less organized. Thanks, everyone, for your thoughts and help - I think I have a plan! – OsakaWebbie Apr 04 '13 at 00:45
  • One more quick question: Does it matter where the bare repo is located? I have installed git on my CentOS 5.6 VPS, and am about to create a user for it, which I would use via SSH to do the git commands. I'm guessing that once I choose a home directory for that user, the repo would automatically go there (although that's just a guess), so I thought I'd pause at this point and ask. – OsakaWebbie Apr 04 '13 at 09:44
  • 1
    Traditionally, the bare repository directory is called ProjectName.git. I'd recommend $HOME/repository/ProjectName.git . – Hal Canary Apr 04 '13 at 10:51
  • And I usually put my working repositories in $HOME/src/ProjectName . – Hal Canary Apr 04 '13 at 10:52
1

You don't need github (or any other central store) to start using git. Especially since you're a lone developer. Git runs directly on your own machine, without any server component (unlike for example subversion). Just git init and start committing away.

I agree with the other commenters here, that you should aim to get a local development environment up and running. Even if it takes some effort, it's certainly worth it. One of the side effects of doing so may be that you are forced to decouple some of your current hard dependencies and thereby getting a better overall application architecture out of it. The things that can't easily be replicated in your development environment could instead be replaced with mock services.

Once that is in place, look into a scripted deployment process. E.g. write a shell script that syncs your development machine's codebase with the production server. There are many ways to do this, but I suggest you start really simple, then revise your options (Capistrano is one option).

troelskn
  • 115,121
  • 27
  • 131
  • 155
  • What do you mean by a mock service? The two biggest things that currently require weirdness on the server are: (1) the code expects a subdomain in the URL to determine the "client" (database, file storage, custom CSS if any), so the dev server would need to be set up that way; (2) Japanese version of TeX with embeddable fonts. I suppose I could just not test the TeX stuff locally, but the vhost-based, single codebase/multiple DB environment would need to be simulated pretty well for the code to be happy. Perhaps there is a better architecture, but it's what I have at the moment. – OsakaWebbie Apr 03 '13 at 12:53
  • Regarding scripted deployment: I could easily write a shell script to sync the whole thing, but when I have things on the dev side that are still in progress (which is almost all the time) I'd need to be careful. That's why I thought git might be able to help with that somehow. – OsakaWebbie Apr 03 '13 at 13:08
  • I would suggest that you at least need a developement environment which reflects live as close as possible as a minimum. There's no real correct answer here. I sometimes have multiple local vm's and sometimes just replicate my live environment on aws and use scripts/cap to deploy dev code. The seperation of dev and live code is accomplised using different branches. So you have master/live and develop. Then when you're ready to release dev code into live you would simply merge this into live/master. – Nick Apr 03 '13 at 13:14
  • 1
    Re deployment, a really simple solution is to write a script that makes a new clone of your repo in a temp folder, `rm -rf .git`, zip it all up and copy it to the server, then extract it there. That way you won't get any uncommitted changes out. – troelskn Apr 03 '13 at 14:32
  • @Nick: VM's are local, but AWS is a type of remote server, so how is replicating a live environment on AWS different from my replicating it on the same server as my live environment so it can use the exact same tools? Pardon me if this is a dumb question, but except for deceze's point about safety, it just sounds like more work. – OsakaWebbie Apr 03 '13 at 16:00
  • Yeah fundamentally they are exactly the same thing. However from my point of view having my environments setup remotely mean that i totally decouple my work from my local machines. Having a couple of laptops and desktop machines it becomes irrelevant where i'm working to what is setup where. Coupled with github i can develop anywhere and everywhere. But you're totally correct - it doesn't really matter where it's setup. Helps me too when i need to show work to others etc. – Nick Apr 04 '13 at 10:04
  • What i would generally do when starting or taking over a project would be to get the environment setup (without a codebase where possible) and save that as an image on aws. Then it's a trivial task to create new instances using a preconfigured environment. So if you have a live environment setup then this means dev is already configured etc.. – Nick Apr 04 '13 at 10:05
0

I'd certainly look at something like capistrano for your current development setup.

I can understand why you might have a reticence to use the terminal but it would probably help your understanding of git in context. Doesn't take long to get to grips with the commands and when tied into a system such as capistrano you'll be rocking development code up to your environment in no time:

git commit -a 
git push origin develop
cap deploy:dev

When i'm working on windows i generally try to replicate the deployment environment i have with virtual machines locally using something like sun's virtualbox. That way you can minimise potential environment issues while still developing locally. Then you can just use putty to ssh to your local vm. Setup sharing between the vm and your host OS and all your standard IDEs/editors will work too. I find this preferable to having to setup a vps remotely but whatever works.

Nick
  • 589
  • 1
  • 7
  • 28
  • Capistrano is a fine tool, but for someone just getting started with VC, and presumably without any ruby experience, I think it might be a bit of an overkill. A simple shell script might be a better place to start. – troelskn Apr 03 '13 at 11:14
  • This is true but i think it's one of those things with a small subset of commands and options rather than learning bash/ssh/scp etc.. But a valid point. – Nick Apr 03 '13 at 12:30
  • I don't even have Ruby installed. Plus, I only have one place I deploy my code (it's a single-codebase service, not a multi-installation web app), so it wouldn't be very useful. But I agree that I probably need to use the command line to get a feel for Git - I've been getting that impression from the tutorials. Should I make my testbed the location of the repo, then? – OsakaWebbie Apr 03 '13 at 12:39
  • As said previously github isn't necessary but it and other services certainly does simplify things. A git repo is by it's nature standalone but github (or your own server/other service) is needed if you want to update your code on your remote server via git. It it just as easy to use something like scp/rsync/ftp to get this code up however. The problem comes with how available your local repo is to the development vps. Since it's always going to need to be 'pulled' from what you consider authorative. – Nick Apr 03 '13 at 12:46
  • @nick - or you could just push over ssh to the production server, rather than pull from the server. git over ssh is dead simple. – troelskn Apr 03 '13 at 14:26
  • I'm currently wondering if I can have my "local repo" be actually on my VPS - in that case, it would be **quite** available to the development directory. (Just to clarify: the dev and production areas are just neighboring directories on the same server with slightly different vhost rules - it's not two servers.) If I did that, is git enough? Or does your second sentence mean I need github if I'm not sitting physically at the computer where the repo is? – OsakaWebbie Apr 03 '13 at 14:49