Book Image

Heroku Cookbook

By : Mike Coutermarsh
Book Image

Heroku Cookbook

By: Mike Coutermarsh

Overview of this book

Heroku is a Platform as a Service that enables developers to rapidly deploy and scale their web applications. Heroku is designed for developer happiness, freeing developers from doing system administrative tasks such as configuring servers and setting up load balancers. Developers are able to focus on what they do best, building web applications, while leaving the details of deployment and scaling to the experts at Heroku. This practical guide is packed with step-by-step solutions to problems faced by every production-level web application hosted on Heroku. You'll quickly get comfortable with managing your Heroku applications from the command line and then learn everything you need to know to deploy and administer production-level web applications.
Table of Contents (17 chapters)
Heroku Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Introducing version control with Git


Git is one of the most popular version-control systems used in software development. It allows teams of developers to work on the same code base without overwriting each other's changes. Git is a core piece of the Heroku platform, and having a basic understanding of how it works is a prerequisite to deploy code to Heroku.

In this recipe, we'll learn enough about Git to deploy code to Heroku.

Tip

Unfamiliar with the command line? There is a great resource to quickly get up to speed on the basics at http://cli.learncodethehardway.org/book/.

How to do it…

Git allows us to track every change to our source code. This makes it simple to go back in time and revert changes as well as view the history of our code. Let's open up a terminal to get started by performing the following steps:

  1. If we've never used Git before, we'll want to tell it our name and e-mail. These will be used to identify us as the author in all our commits:

    $ git config --global user.name 'First and Last name here' 
    $ git config --global user.email '[email protected]'
    
  2. Now, let's create a new directory to practice with:

    $ mkdir LearningGit
    $ cd LearningGit
    
  3. In our new directory, we'll need to initialize a new Git repository:

    $ git init
    Initialized empty Git repository in /home/mc/LearningGit/.git/
    
  4. Now, let's create a new file in our project using touch:

    $ touch new_file.txt
    
  5. We can use the status command to check whether this file is currently untracked by Git:

    $ git status
    On branch master
    
    Initial commit
    
    Untracked files:
      (use "git add <file>..." to include in what will be committed)
    
            new_file.txt
    
    nothing added to commit but untracked files present (use "git add" to track)
    

    Tip

    The default output of the Git status can be a little verbose. Use git status -sb for a more concise output.

  6. We need to explicitly tell Git to track the file using the add command:

    $ git add new_file.txt
    

    Note

    Try to run git status again. See the difference?

  7. Our file is now created and being tracked by Git, but it is not yet committed to our repository's history. Let's commit it now:

    $ git commit -m "Adding new_file to Git"
    [master (root-commit) b79ca0b] Adding new_file to Git
    1 file changed, 0 insertions(+), 0 deletions(-)
    create mode 100644 LearningGit/new_file.txt
    

    Note

    To save time, we can combine git add and git commit in a single command. The git commit -am command will stage any currently tracked files and commit them.

  8. To view the history of our Git repository, we can use the log command. Use the q key to escape from viewing logs. Use the arrow keys to scroll:

    $ git log
    commit b79ca0b7c7671789cc8359fe43e1144af835c2d1
    Author: Mike Coutermarsh <[email protected]>
    Date:   Mon Mar 3 20:31:47 2014 -0500
    
    Adding new_file to Git
    

We now know the essential commands to use Git from the command line. We're able to create a new repository, add and track the files, as well as view our repositories' status and history. These are just a limited set of Git's commands, but they are enough for us to get started with Heroku. We'll be using Git throughout this book to track and push our code. We'll build on the skills we learned here in the later recipes.

How it works…

An easy way to understand Git commits is to think of each commit as a photograph of our project files. Each time we make a commit, Git takes a photo of our files. We are then able to view each commit to see exactly what changed in our project. If we need to go back in time, we can just revert to the state our code was in before the commit was made.

Git is a distributed version-control system. This means that there is no need for us to be online or connected to a server to use it. In Git, there is the concept of remotes. These are other instances of the Git repository on other servers or machines. If we have a Git repository on GitHub, then this is known as a remote. Having remotes is useful because they act as a central place where all the members of a team can push their changes and retrieve the changes made by others. Having the full Git repository on multiple machines and servers also makes it much more fault tolerant. If anything were to happen to our remote repository, each team member would still have a local copy that can be used to create a new remote elsewhere.

Remotes are an important concept for us to understand because they are the basis to deploy to Heroku. Each Heroku deploy is simply the push of a local Git repository to a remote provided by Heroku. It's amazingly simple; we'll learn about it in detail later in this chapter.

There's more…

Using and learning about Git from the command line can be challenging. Luckily, there are a few easy-to-use desktop apps that make using Git really simple:

See also