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/.
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:
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]'
Now, let's create a new directory to practice with:
$ mkdir LearningGit $ cd LearningGit
In our new directory, we'll need to initialize a new Git repository:
$ git init Initialized empty Git repository in /home/mc/LearningGit/.git/
Now, let's create a new file in our project using
touch
:$ touch new_file.txt
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)
We need to explicitly tell Git to track the file using the
add
command:$ git add new_file.txt
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
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.
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.
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:
GitHub for Mac available at http://mac.github.com/
GitHub for Windows available at http://windows.github.com/
SourceTree (Mac or Windows) available at http://www.sourcetreeapp.com/
Tower (Mac) available at http://www.git-tower.com/
SmartGit (Linux) available at http://www.syntevo.com/smartgithg/
Grab the Git cheatsheet at http://cheat.errtheblog.com/s/git
For an interactive tutorial on using Git, check out TryGit at https://try.github.io
Packt has a great beginner's guide to using Git, Git: Version Control for Everyone, Ravishankar Somasundaram