From FTP to Git: A Deployment Story

    From FTP to Git: A Deployment Story

    Once upon a time, there was a file. It was on your computer, and you wanted to get it on a server.

    Ever wondered why there are so many ways to do that? We’ll explain some of the basics of deployment in this article so you understand when to use what. Let’s get started!


    FTP

    FTP, or File Transfer Protocol, is considered by many people to be the old school way to “put a site up.” It is a protocol, which basically means that there are a set of rules that both the local computer and the host machine agree on, and can send messages through. FTP is not a “program,” per se, but rather more like a phone line.


    SFTP/SSH

    So, if FTP is an old-school phone line (that still works just fine), SFTP is like a 4G network

    It offers a new protocol for the two machines to talk (not that it is necessarily faster, however). SFTP is powered by SSH, which essentially encrypts the messages that are passed between the two machines; so any malicious third-party or untrusted network can’t get a hold of your raw data during the transfer.


    SFTP vs FTP

    If you didn’t quite catch that, FTP and SFTP are both file transfer protocols. However, SFTP (and other SSH-powered transfer protocols) transfers files and encrypts the transfer. “I don’t need encryption”, you may say. A lot of people think the same way; however, forward-thinking developers and modern tools will lean towards more secure methods. You’ve heard it before – Better safe than sorry.

    But I don’t feel like going through the trouble.

    First of all, if this is your job, suck it up. You can either stick with your comfort zone (you know, FTP still works, just like your landline phone). But don’t you want to get better? After all, that’s why you’re here, right?

    Now, if you are still a little lazy but like the idea of easy adoption, you can use SFTP with almost any FTP client out there. It’s more secure. Make sure your server supports SSH (port 22, commonly, needs to be open), and you should be good to go. But the point of this article isn’t to get you thinking about encryption and transfer security; it’s to get you thinking about a more robust deployment strategy.


    Deployment vs File Transfer

    “But I don’t feel like going through the trouble.” …if this is your job, suck it up

    If you’ve been developing for a while, you’ve probably gone through the drill of creating a site and constantly dragging and dropping your files to your FTP client (or double-clicking, or pressing “sync”, or…). This is technically a deployment strategy, albeit not a very robust one. Of course, a lot of times this kind of strategy will work fine, especially if you’re the only person that will ever touch the files, and you’ve magically never overwritten or deleted an important file. But, again, you’re here to get better, right? And you are a magician.

    Deployment, in its simplest form, is taking some code and making it “live” code. By transferring an index.html file into your serving directory, you are deploying. In fact, at the end of the day, all deployment strategies (unless you are using a compiled app system) essentially move files or versions of files into the “current working directory”, or change the ones that are already there. For instance, you could make changes to that same index.html file directly on the server, and that would effectively “deploy” those changes to the public. But deployment can be a lot more.

    Has your team ever set up a system that requires you to notify everyone when you have pushed a file onto the server through FTP? Or maybe you have to restart a Django or Rails server after changing code. If you do this as a part of the routine of making your site reflect the changes you’ve made, that is part of your deployment process.


    Version Control Concepts

    So, if deployment means making some set of code and files live, how can there be much more than SFTP? “What is Git, and why should you care?” you’re asking. Git is a Version Control System, or a VCS. It is one of many VCSs that we’ve shamelessly picked as our favorite. We’ll explain why later, but first let’s talk about what it is.

    Version control systems accomplish a lot of tasks, but the most important is providing a safety net to developers–especially development teams. We mentioned earlier how FTP and SFTP may be perfectly fine if you’re perfect and you’ll never overwrite a file or delete an important folder unintentionally. But if you haven’t done this yet, don’t worry – it will happen sooner or later. It’s almost certain that it has happened to your team if you haven’t figured out a workaround system. But even those workaround systems are a pain. For instance, has your CSS directory ever looked like this?

    <style href="john-styles.css" ... />
    <style href="mary-styles.css" ... />
    <style href="main-styles.css" ... />
    

    If so, you’re keeping versions of stylesheets that eventually you will merge together. Incidentally, Git does this, but it does it a lot better, faster, and more robustly than you can on your own. Do you keep a different CSS file every time you make a few changes? Just in case you want to come back to exactly how that file was at a certain point in time? Of course you don’t. But Git will do exactly that for you, and you don’t have a mess of strewn files and obscure naming systems everywhere. Beyond this, Git is smart enough to know how to take your styles.css file and merge it with Mary or John’s styles.css file. Furthermore, Git’s versions are not entire files; instead, they are stored in deltas, which is essentially a low-level code representation of the differences between one file and another file. This means that different versions are much smaller, and much faster to transfer when it comes time to deploy them.

    But none of this has anything to do with deployment, you say? You’re right, version control systems in large part simply do what their name suggests: keep versions in order. But, they also have the ability to communicate through the different protocols we’ve discussed before. Git can read through HTTP and read and write through SSH. (Read more about the transfer protocols here.)

    So if Git has these direct connections through transfer protocols, you can push versions back and forth with your team through a local server, GitHub, or your own live server. And that’s how Git fits into deployment. You push your code, Mary and John push their code, then whoever is in charge of deployment on your team will log onto the server and merge the code into the live environment. Git offers the ability to have multiple destinations and branches of code to push different versions to different places. A common practice is to have a “staging” server, where code is pushed to check it in a live environment on a private development domain. Another possible workflow is to never connect your team’s local repositories to the live repository, and instead only allow the staging machine the ability to push to the live server. This creates a system for review that can’t be skipped.

    Different versions are much smaller, and much faster to transfer, when it comes time to deploy them.

    Beyond staging, branches, multiple deployment destinations, and robust version control, Git offers hooks, like “post-receive”, that can perform any kind of action you can throw together. Maybe you want a “spider” branch, where when the code arrives at one point on your server, the post receive pushes that code across multiple locations (remote or otherwise). Even further, using a platform like GitHub offers a great graphical interface for setting up hooks. Easily tell GitHub to send you emails (or if you’re creative, hook up a Growl notification) every time you receive a push from a team member, for instance.

    There are literally thousands of ways to develop a deployment strategy with Git. You can quickly see that using a VCS for deployment offers some very extensive advantages, protection, and power to your deployment. Once you take the time to learn enough about Git to get a workflow in place (and make a few mistakes with it), your development process will benefit immensely.


    Deploying with Git: A Barebones Example

    The following commands will set up a repository on a remote machine, set up a repository on your local machine, adding the remote repository as “origin”, checkout a branch called “mybranch” that will be merged on the server, pushes “mybranch” to the remote, “origin”, and finally logs back on to the server to merge “mybranch” with the default “master” branch.

    local> ssh username@remotemachine.com
    remote> cd path/to/project
    remote/path/to/project> git init
    remote/path/to/project> exit
    local> cd path/to/local/project
    local/path/to/local/project> git init
    local/path/to/local/project> git add .
    local/path/to/local/project> git commit -m "initial commit"
    local/path/to/local/project> git checkout mybranch
    local/path/to/local/project> git remote add origin username@remotemachine.com:path/to/project
    local/path/to/local/project> git push origin mybranch
    local/path/to/local/project> ssh username@remotemachine.com
    remote> cd path/to/project
    remote/path/to/project> git merge mybranch
    

    Git + Heroku: A Match Made In the Clouds

    Beyond making your own Git-powered workflow and integrating it into your own server, you can utilize other free services, such as Heroku;. Heroku is a hosting solution that allows you to run a few lines from your Git-controlled app, and deploy it to the “cloud” (in this case, a remote cluster of Linux servers) over Git with almost no effort. Take a few minutes to get a few things configured, and you’re off to the races with just a few lines of code for simple deployment. Seriously, it looks something like this.

    # do some work, keep it version-controlled with Git... then
    > heroku create --stack cedar
    # the --stack cedar basically allows you to create multiple kinds of apps, including PHP and Node
    # ... Heroku will tell you it's creating things, then it will tell you it added a heroku remote to your git repo
    > git push heroku master
    # and that's it! Open the deployed app in your browser
    > heroku open
    

    Other similar tools include Pagoda Box, which is focused on LAMP-based architectures, and AppFog and PHPFog, which is very similar to Heroku.

    As long as there aren’t any errors in your configuration and you’ve followed Heroku’s guidelines for how to set up apps, you can have Rails, PHP, or Node apps up and running in a few moments. Oh, and did we mention that you can make simple Heroku-powered apps for free? It’s not until you upgrade to more instances or memory, or need to add a database, that you incur charges from Heroku. Other similar tools include Pagoda Box, which is focused on LAMP-based architectures, and AppFog and PHPFog, which is very similar to Heroku. These options offer similar services and Git integration, with different pricing structures. All three offer a free option for limited server power. These are clearly great benefits, as it takes a lot of time investment to get your own server set up with more involved application architectures such as Rails or Node. Beyond that, scaling becomes very simple, as all three of these services offer load-balancing and scaling to multiple instances at the click of a button. These services take care of the hefty server administration for you, so you can focus on creating apps, knowing that there is a well-documented, safe, fast deployment strategy for you to use when you’re ready to go public.


    Conclusion

    Hopefully, this article has removed some confusion about the differences between Git (and other VCSs) and FTP/SFTP, and what the concept of deployment means. Also, I hope that this has encouraged you to develop your own deployment strategy, with assistance from tools, like Git and Heroku. If you already have a strategy, what kinds of things do you do during deployment?

    Tags: git
    Note: Want to add some source code? Type <pre><code> before it and </code></pre> after it. Find out more
    • http://developernetwork.info/ David D’hont

      Just want to let you know this article is using it’s full article as the excerpt. Just saying so you can fix it ;)

    • Sam

      As a former freelancer, I used good old FTP to edit/deploy code for clients and volunteer projects. As I was always the only person working with the code, it worked just dandy.

      I recently got a job doing Rails development with a firm of about 40 people (dev team of at least a dozen). One of the most impressive things I’ve witnessed so far is the ease of collaborative development and split between local, testing, and production environments. For someone who’s used to writing PHP, throwing it on a server to test, and repeating, a solid develop-test-deploy strategy using Git and Heroku is really several steps up.

      I’m in the process of weeding out projects from my former freelancing, but for the few that I want to continue with, I’m excited to setup this kind of architecture. It’ll be an ordeal to change everything, but the end-game is *much* easier maintenance.

    • http://creolab.hr Boris Strahija

      The best deployment strategy for me is currently http://beanstalkapp.com/. Deploying to production with my iPhone, can’t beat that ;)

      • http://en.gs/ Jarrod Mosen

        Agreed! Once, the team were all out of the office for the yearly retreat… when a rogue code push decided to obliterate the production server. iPhone + Beanstalk rollback to the rescue!

    • Ian

      Being somewhat new to web development so far my clients are the kind that try to do part of the work themselves and are no where near ready to learn anything more complex to setup than filezilla. And since I’m the only actual developer working on things I use sftp.

      But this is some good info and I’m learning everything I can and setting up test situations so I can talk intelligently about the benefits of using git when I start trying to push clients to let me set it up and use it. It would be good for a couple of growing clients to have this deployment system and versioning in place for future growth even if they don’t really need it now.

      Thanks for the article.

      • TJ

        Why do your clients care what tools you use to build their product? You’re the expert, aren’t you?

        • ian

          TJ,

          That’s why I’m learning as much as I can. So I can be the expert. As far as web design/dev I’m expert enough to do what needs to be done but for a client who has a dedicated server I can recommend a deployment process but it still has to be approved so for me to recommend it I feel I should be able to come across as the expert. At this point I’m just the guy who does good work on the web site and thinks Git is really cool. I would just about bet on one concern being that the site code is being pushed out to an external cloud server somewhere instead of being kept between my home PC and their host server.

      • MichMich

        Ian, it makes no difference for your clients. Using a tool like Beanstalk allows you to deploy your GIT Repository via (S)FTP. This will make your live much easier (and safer: rollbacks are available), and your clients don’t even need to know.

        It took me some time to take the step, as well. But using a tool like Tower (Mac only), learning Git is easy.

        None the less, it might be good to know some Command Line magig behind it, which is easy to learn using this free online course: http://www.codeschool.com/courses/try-git.

    • http://www.xcellence-it.com Xcellence IT

      Hi, extremely easy and very helpful article. Thanks

    • http://www.SGITMaintenance.com SGITMaintenance

      Wonderful article that correctly articulates the differences between SFTP and FTP. In the example here, GIT is used for coding. GIT can also be used for file storage.

    • Scott

      Excellent explanation. What we’re doing at work is insane and we are looking at version control. We were going to go with Subversion but I’m going to recommend Git and PHPFog instead. Thanks for helping me to understand it. Your premium course https://tutsplus.com/course/git-essentials/ will help us to get up and running.

    • MichMich

      I recently made the move from FTP to GIT Deployment, thanks to the guys on #laravel IRC. And man, I should have made the switch way earlier. Is saves me so much time, so many headaches, and so many ‘oops!’-moments.

      I’m currently using Beanstalkapp.com as a Git server. They provide an easy way to set up deployments. So with one simple commit and push, my new site is update with a smile. Love it!

      For all you FTP’ers out there: Make the switch. Just do it. It isn’t that difficult.

    • http://gregchapple.com Greg

      I just finished changing from FTP to a git deployment set up – and it is so much better! Already saved me so much time.

      Currently I have a development server on which I created a git repo (dev) in and also a bare git repo (hub) which I set up as a remote for dev. I then cloned the hub repo onto my local machine and set up some hooks on the server. So when I push to the hub on the master branch it gets pulled into dev so I can test things out there. I also set up another git repo on my main “public” server as a clone of the development server.

      So when I am happy with the changes I make on the development server I run a bash script that pulls the files from development into the live server. It sounds like a long process but it enables me to test stuff out thoroughly in a production environment but away from my live site, then when things are ready I can move it all over to the live site with one singe command. And never have to open an FTP client :).

      I can also easily rollback to any previous commits from any point in the past – which you cant do with FTP!

      If anyone is interested, this is where I got the idea and some instructions:
      http://joemaller.com/990/a-web-focused-git-workflow/

    • http://kickstartwedding.com Hisyam

      In the “Deploying with Git: A Barebones Example”, could you explain why didn’t you just use the master branch?

      • http://whiteboard.is Jonathan Cutrell

        Definitely.

        The “live” copy (on the remote server) is on the master branch. When the master branch is checked out, you aren’t supposed to be able to push to it. (People still do it, and depending on what your Git settings are, it may still work.) There are a bunch of “correct” ways to do it, but the way I’ve outlined here keeps you from having to have a separate bare repository externally from your working directory.

        Certainly, you can override this default behavior and then run “git reset –hard HEAD” after pushing to master, but the way outlined here is a bit less prone to error, and a bit cleaner.

        All of that to say, everyone has their own way of doing it, so if you find that pushing to master has benefits for you, go for it.

    • Abdallah

      Redhat also have great PAAS compatible with GIT, https://openshift.redhat.com.

    • Mark

      We are using Springloops to deploy files from Git. It works perfectly for last 3 years!

    • http://www.monkinteractive.com Pierre

      I have to confess I’m still on the FTP process. But I’m eager to get on the GIT bandwagon! There are a few questions for all you GIT users:

      - Do I need to use an online repository? I have a server at home with many layers of backup redundancy built in. Can I just use that?

      - If I setup an account on Github is it publicly accessible by default? I really don’t want my clients files and work to be viewed by anyone but me. I’m assuming I can lock this off but want to make sure before I inadvertently make something private publicly accessible.

      - Any great windows GUIs for GIT? I’ve used Tortoise with SVN at a previous job and didn’t care for it. Are there any other good programs for GIT that aren’t command line driven?

      Thanks!

      • Mario

        I am also in the same boat.

      • http://whiteboard.is Jonathan Cutrell

        Public repositories is kind of the GitHub flagship. However, you can set up Git on just about any machine or server, so you definitely don’t have to use GitHub. There are also other services, like Beanstalk, that provide private repos. Of course, Github has private repos too, you just have to pay for them.

        Git does have guis for windows:
        http://windows.github.com/ <– probably the most popular at this point
        http://git-scm.com/downloads/guis

        • http://www.monkinteractive.com Pierre

          Thanks for the reply Jonathan! I’ll check out the GUIS you mentioned.

    • http://rogerdickeyjr.com/ Roger

      Thanks for the tip! Very timely, I was getting to the point for a direct Git deployment on a project that I’m not storing on Github.

    • http://blog.hizup.com Harry

      Great tutorial, tahnk you!

    • http://blog.hizup.com Harry

      Great tutorial, tahnk you!

    • http://www.about.me/ericclark Eric Clark

      I’ve been using SVN for so long, but this makes me want to give Git another shot. Seems like it’s easier now, especially with Heroku. Thanks for sharing!

    • http://sebastien.vaneyck.free.fr Akaryatrh

      @author : Heroku link is broken ;)

      • http://whiteboard.is Jonathan Cutrell

        Noted. :)

    • Dimitri

      Simple deployment solution. For SSH sites, I use Capistrano and for FTP, I use Dandelion.

    • http://comradedeveloper.blogspot.com/ Comrade

      Nice tip! I use SVN in all my projects, probably will start using git on the next one.

    • Svetlana

      I have silly question, can I use git along with my Illustrator files, cause I make a lot of revisions in any given AI file… I am designer, sorry if this question sounds newbish

    • http://- GD

      I have a few concerns. What if you want to restore to the previous state on the live server? Also is it logical to use branches for “versioning” the live site?

      For example, You make an update, you name a branch ver1.2, something goes bad and only thing you do is to checkout this branch and return to the master branch on the live server. Is this the way to go?

    • http://www.step4wd.com/ M. Ahmad Zafar

      So the “live” server will be just another Git repo or a “bare” repo? Plus it will have the “.git” folder? Wouldn’t that be a security problem? How to prevent access to it?

      • http://twitter.com/matthewfedak matthewfedak

        you could prevent access to the .git folder using a .htaccess file for example or just make sure it is in the directory above the public html folder

        • http://www.step4wd.com/ M. Ahmad Zafar

          Ok and what about my first part of the question. {{ So the “live” server will be just another Git repo or a “bare” repo? Plus it will have the “.git” folder? }}

        • http://twitter.com/matthewfedak matthewfedak

          typically i work on a project locally and then when time comes I initialise a bare repo on my server, add it as a remote to my local repor and push to that to deploy. All that does though is makes a repo on my site the same, I then use a git post commit hook to update the working files on my server.

    • Curt Howard
    • skube

      I got an error after doing “git checkout mybranch”:

      > error: pathspec ‘mybranch’ did not match any file(s) known to git

      Missing in the example is the actual branch creation:
      > git branch mybranch

    • http://nodws.com/ Nodws

      Not really useful for designers, only if you are coding and have your OWN git server (or ca$$$h to make your github private) and git is definatly NOT a deployment tool

      • http://twitter.com/matthewfedak matthewfedak

        you could just use bitbucket which has free private repo’s.

    • http://www.facebook.com/prashant.palikhe Prashant Palikhe

      Git does not store the differences as deltas as other VCSs do, rather, they have blobs and tree objects to reflect snapshot for each version(commit object). Each commit object has a tree object. Each tree object has blob object (Zlib compressed content of a file named by it SHA-1 hash) and pointer to other tree objects.

    • mettjus

      i love git for deployment but got a question:
      as i use a couple of preprocessors (coffee, sass) i wanted to know if there is a way to ignore compiled files in the repo but still be able to deploy them via a git push without having to install compilers on the destination server and running post-pull commands.. same thing for stuff like composer
      thanx,
      met