Skip to content
This repository has been archived by the owner on Jun 25, 2018. It is now read-only.

Beginner Project: Basic Git

alexlafroscia edited this page Sep 17, 2014 · 19 revisions

Getting to Know Git

This project will walk you through getting to know Git and Github by walking through...

  1. Creating a project on your computer
  2. Stage and committing files
  3. Revert changes that you made to your project
  4. Branching your project and merging branches
  5. Storing changes on Github

Note: When the guide refers to executing a command, it means to do so in the command line. On a Mac, you'll want to use Terminal or iTerm 2. On Windows, you should be using the Git Bash program that's installed if you follow these instructions.


Vocabulary

Some terms to be aware of while reading through these instructions

Directory

  • A folder on your computer

Home folder

  • The folder on your computer that most of your personal files are kept in
  • In OSX systems, this folder can be found at /Users/*your-user-name*/
  • In Windows systems, this folder can be found at /c/Users/*your-user-name*/

Steps

1. Creating a New Project

The first thing that we'll want to do is create a new project to be tracked by Git. To start off, open your command line program and enter the following commands:

mkdir projects
cd projects
mkdir git-beginner-project
cd git-beginner-project

What this does is create a new folder called projects inside your home folder, step inside of it using the cd command, then do the same with the git-beginner-project folder.

Now that we have a place to create your project, we want Git to set itself up to track the files inside this folder. To do that, we run the following command:

git init .

This tells Git to init, or initialize, a new project inside the current directory (which is refered to by a .).

If you see a message like this:

Initialized empty Git repository in c:/Users/alex/projects/.git/

Then you're all set!

2. Staging and Committing Files

Next, we'll add some files to the project and see how Git can keep track of them. In the command line, type the following command

touch hello.txt

This will go and create a new file called hello.txt. Now, run the following command to see what changes Git is currently detecting:

git status

Let's take a look at the output and break it all down:

$ git status
On branch master

Initial commit

Untracked files:
  (use "git add <file>..." to include in what will be committed)

       hello.txt

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

This means that git sees no changes to files that it is already keeping track of, but does see a new file that it doesn't recognize. In Git, a "tracked" file is one that Git is currently watching for changes, and an "untracked" on is a file that it knows nothing about yet.

We want to get Git to start keeping track of our file. Do to that, run the following two commands: the first will add the file to those that Git keeps track of, and the second will output the status again

git add hello.txt
git status

Which should result in something like this:

$ git status
On branch master

Initial commit

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

      new file:    hello.txt

This is telling us that Git is now tracking one new file, called hello.txt. Sounds about right to me!

You'll notice that Git mentioned a few new concepts, namely commit and staged (or, as you can see in the output above, unstaged.

In Git, a staged file is one that has changes that you're going to be adding to your next commit (more on that in one second). An unstaged file is one that Git has noticed a change in, but that it is currently ignoring.

A commit is the basic building block of all of Git. By creating a new commit, you're bundling together a set of changes to your project: new files, additions, deletions, everything. By keep track of every change to every file in your project, Git can rewind your project to any point that you made a commit, almost like save points in a video game.

Now, we'll create the first commit for our project. Recall that our project has hello.txt staged, so that's what will be including within our first commit. Creating a commit can be done a few ways, but the quickest way is like this:

git commit -m "My first commit message"

Where My first commit message is a brief description of the changes you made within this commit. This message should be short and to the point -- there are ways to add more details about your changes, but we'll look at that at another time.

Installing Git

Projects

Other Resources

Clone this wiki locally