Găsiți următorul dvs. carte preferat

Deveniți un membru astăzi și citiți gratuit pentru 30 zile
Ry's Git Tutorial

Ry's Git Tutorial

Citiți previzualizarea

Ry's Git Tutorial

Lungime:
245 pages
2 hours
Lansat:
Dec 2, 2014
ISBN:
9780985072315
Format:
Carte

Descriere

Git is a free version control system known for its speed, reliability, and non-linear development model. Its popularity among open-source developers makes Git a necessary tool for professional programmers, but it can also do wonders for your personal coding workflow. You’ll be able to experiment with new ideas, radically refactor existing code, and efficiently share changes with other developers—all without the slightest worry towards breaking your project.

This comprehensive guide will walk you through the entire Git library, writing code and executing commands every step of the way. You'll create commits, revert snapshots, navigate branches, communicate with remote repositories, and experience core Git concepts first-hand.

Lansat:
Dec 2, 2014
ISBN:
9780985072315
Format:
Carte

Despre autor

Ryan is a technical author and developer based in Boulder, CO. He's worked for NCSA, Beyond Design, Syncfusion, and Atlassian, creating everything from Flash visualizations to Git documentation. Between RyPress.com and freelance work, his writing reaches hundreds of thousands of developers a year. His hobbies include reading technical specifications just for kicks, playing chess, animating pixel art, and bespoke shirtmaking. He's also a trained linguist.

Legat de Ry's Git Tutorial

Cărți conex
Articole conexe

Previzualizare carte

Ry's Git Tutorial - Ryan Hodson

it!

The Basics

Now that you have a basic understanding of version control systems in general, we can start experimenting with Git. Using Git as a VCS is a lot like working with a normal software project. You’re still writing code in files and storing those files in folders, only now you have access to a plethora of Git commands to manipulate those files.

For example, if you want to revert to a previous version of your project, all you have to do is run a simple Git command. This command will dive into Git’s internal database, figure out what your project looked like at the desired state, and update all the existing files in your project folder (also known as the working directory). From an external standpoint, it will look like your project magically went back in time.

This module explores the fundamental Git workflow: creating a repository, staging and committing snapshots, configuring options, and viewing the state of a repository. It also introduces the HTML website that serves as the running example for this entire tutorial. A very basic knowledge of HTML and CSS will give you a deeper understanding of the purpose underlying various Git commands but is not strictly required.

Create the Example Site

Before we can execute any Git commands, we need to create the example project. Create a new folder called my-git-repo to store the project, then add a file called index.html to it. Open index.html in your favorite text editor and add the following HTML.

en>

 

 

A Colorful Website

 

 

utf-8 />

 

 

color: #07F>A Colorful Website

 

 

This is a website about color!

   

 

 

color: #C00>News

 

 

 

   

  • Nothing going on (yet)
  •  

     

    Save the file when you’re done. This serves as the foundation of our example project. Feel free to open the index.html in a web browser to see what kind of website it translates to. It’s not exactly pretty, but it serves our purposes.

    Initialize the Git Repository

    Now, we’re ready to create our first Git repository. Open a command prompt (or Git Bash for Windows users) and navigate to the project directory by executing:

    cd /path/to/my-git-repo

    where /path/to/my-git-repo is a path to the folder created in the previous step. For example, if you created my-git-repo on your desktop, you would execute:

    cd ~/Desktop/my-git-repo

    Next, run the following command to turn the directory into a Git repository.

    git init

    This initializes the repository, which enables the rest of Git’s powerful features. Notice that there is now a .git directory in my-git-repo that stores all the tracking data for our repository (you may need to enable hidden files to view this folder). The .git folder is the only difference between a Git repository and an ordinary folder, so deleting it will turn your project back into an unversioned collection of files.

    View the Repository Status

    Before we try to start creating revisions, it would be helpful to view the status of our new repository. Execute the following in your command prompt.

    git status

    This should output something like:

    # On branch master # # Initial commit # # Untracked files: #  (use git add ... to include in what will be committed) # #      index.html

     

    nothing added to commit but untracked files present (use git add to track)

    Ignoring the On branch master portion for the time being, this status message tells us that we’re on our initial commit and that we have nothing to commit but untracked files.

    An untracked file is one that is not under version control. Git doesn’t automatically track files because there are often project files that we don’t want to keep under revision control. These include binaries created by a C program, compiled Python modules (.pyc files), and any other content that would unnecessarily bloat the repository. To keep a project small and efficient, you should only track source files and omit anything that can be generated from those files. This latter content is part of the build process—not revision control.

    Stage a Snapshot

    So, we need to explicitly tell Git to add index.html to the repository. The aptly named git add command tells Git to start tracking index.html:

    git add index.html git status

    In place of the Untracked files list, you should see the following status.

    # Changes to be committed: #  (use git rm --cached ... to unstage) # #      new file:  index.html

    We’ve just added index.html to the snapshot for the next commit. A snapshot represents the state of your project at a given point in time. In this case, we created a snapshot with one file: index.html. If we ever told Git to revert to this snapshot, it would replace the entire project folder with this one file, containing the exact same HTML as it does right now.

    Git’s term for creating a snapshot is called staging because we can add or remove multiple files before actually committing it to the project history. Staging gives us the opportunity to group related changes into distinct snapshots—a practice that makes it possible to track the meaningful progression of a software project (instead of just arbitrary lines of code).

    Commit the Snapshot

    We have staged a snapshot, but we still need to commit it to the project history. The next command will open a text editor and prompt you to enter a message for the commit.

    git commit

    Type Create index page for the message, leave the remaining text, save the file, and exit the editor. You should see the message 1 files changed among a mess of rather ambiguous output. This changed file is our index.html.

    As we just demonstrated, saving a version of your project is a two step process:

    Staging. Telling Git what files to include in the next commit.

    Committing. Recording the staged snapshot with a descriptive message.

    Staging files with the git add command doesn’t actually affect the repository in any significant way—it just lets us get our files in order for the next commit. Only after executing git commit will our snapshot be recorded in the repository. Committed snapshots can be seen as safe versions of the project. Git will never change them, which means you can do almost anything you want to your project without losing those safe revisions. This is the principal goal of any version control system.

    The stage/commit process

    View the Repository History

    Note that git status now tells us that there is nothing to commit, which means our current state matches what is stored in the repository. The git status command will only show us uncommitted changes—to view our project history, we need a new command:

    git log

    When you execute this command, Git will output information about our one and only commit, which should look something like this:

    commit b650e4bd831aba05fa62d6f6d064e7ca02b5ee1b Author: unknown Date:  Wed Jan 11 00:45:10 2012 -0600

     

     

        Create index page

    Let’s break this down. First, the commit is identified with a very large, very random-looking string (b650e4b...). This is an SHA-1 checksum of the commit’s contents, which ensures that the commit will never be corrupted without Git knowing about it. All of your SHA-1 checksums will be different than those displayed in this tutorial due to the different dates and authors in your commits. In the next module, we’ll see how a checksum also serves as a unique ID for a commit.

    Next, Git displays the author of the commit. But since we haven’t told Git our name yet, it just displays unknown with a generated username. Git also outputs the date, time, and timezone (-0600) of when the commit took place. Finally, we see the commit message that was entered in the previous step.

    Configure Git

    Before committing any more snapshots, we should probably tell Git who we are. We can do this with the git config command:

    git config --global user.name Your Name git config --global user.email your.email@example.com

    Be sure to replace Your Name and your.email@example.com with your actual name and email. The --global flag tells Git to use this configuration as a default for all of your repositories. Omitting it lets you specify different user information for individual repositories, which will come in handy later on.

    Create New HTML Files

    Let’s continue developing our website a bit. Start by creating a file called orange.html with the following

    Ați ajuns la sfârșitul acestei previzualizări. Înscrieți-vă pentru a citi mai multe!
    Pagina 1 din 1

    Recenzii

    Ce părere au oamenii despre Ry's Git Tutorial

    0
    0 evaluări / 0 Recenzii
    Ce părere aveți?
    Evaluare: 0 din 5 stele

    Recenziile cititorilor