官术网_书友最值得收藏!

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 'yourname@example.com'
    
  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 <coutermarsh.mike@gmail.com>
    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

主站蜘蛛池模板: 蕲春县| 普宁市| 祁门县| 屯昌县| 信丰县| 牙克石市| 达拉特旗| 岐山县| 雅安市| 邓州市| 蛟河市| 贡觉县| 寻乌县| 永宁县| 广州市| 依安县| 八宿县| 安徽省| 临城县| 嘉禾县| 渭南市| 侯马市| 克什克腾旗| 西昌市| 突泉县| 济宁市| 安塞县| 水富县| 阳原县| 延寿县| 通许县| 兴宁市| 廉江市| 江安县| 奎屯市| 奇台县| 新田县| 龙江县| 吐鲁番市| 罗甸县| 潢川县|