Sunday, March 1, 2015

Basics of Working with Git

Git is an empowering version control tool.  Everyone gets a copy of the repository.  You get freedom to experiment and take chances.  That wiggle room is helpful when working on your creation.

If you haven't yet, go install it.  Here's a link for getting a good setup for Windows. 


Setting Up Your Project

Fire up GitBash.  This gives you a Linux-like environment to work with.  Linux expertise is not needed to follow along.

To keep things simple, make a folder and initialize it.  Here's what to do....

username@YOURCOMPUTER ~
mkdir myproject

username@YOURCOMPUTER ~
cd myproject

username@YOURCOMPUTER ~/myproject
$ git init
Initialized empty Git repository in c:/Users/username/myproject/.git/

username@YOURCOMPUTER ~/myproject (master)
$


The git init command sets up your initial repository.  That repository keeps track of file changes.  A special folder called .git handles the magic of managing those changes.  You need not understand the contents of that folder.

We need a file for git to keep track of.  Make a file called hello.txt.  Put some text in it.  Something like below should do for an example...

hello there, how are you?

With that set up, you now have what you need to learn the basics of how git works.  The working directory has the current copy of the files you're working on.  Hello.txt lives there right now.  


Setting Up To Commit

You should always know the state of your project before committing things to Git.  Here's how you do that...

username@YOURCOMPUTER ~/myproject (master)
$ git status
On branch master

Initial commit

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

        hello.txt

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

username@YOURCOMPUTER ~/myproject (master)
$


Git status tells you the state of your project.  Note the "Untracked files" section.  Those are the files that are currently not being managed by Git right now.  We want hello.txt under Git management so do the following... 

username@YOURCOMPUTER ~/myproject (master)
$ git add hello.txt


Check the new state of things by running git status again...


username@YOURCOMPUTER ~/myproject (master)
$ git status
On branch master

Initial commit

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

        new file:   hello.txt


username@YOURCOMPUTER ~/myproject (master)
$


Note that hello.txt file is now under "Changes to be committed".  That's because we did a git add on that file.  That command put hello.txt in a place called a staging area.  That area holds a snapshot of the file changes about to be committed to the repository.
  

Commits and Commit Logs

Let's add our first item to that repository by doing a commit....


username@YOURCOMPUTER ~/myproject (master)
$ git commit -m "my first commit"
[master (root-commit) bbbc1cf] my first commit
 1 file changed, 1 insertion(+)
 create mode 100644 hello.txt

username@YOURCOMPUTER ~/myproject (master)
$


The git commit command sends a snapshot of changes that you staged into the repository.  The repository is a collection of those snapshots complete with information about the changes associated with them.  

Congratulation!  You succeeded in making your first commit to a git repository.  To confirm, check the commit history with the git log command.


username@YOURCOMPUTER ~/myproject (master)
$ git log
commit bbbc1cf06977dd40780430a5665f94f083e2bc98
Author: you
Date:   Sat Feb 28 15:38:47 2015 -0500

    my first commit

username@YOURCOMPUTER ~/myproject (master)
$


Summary

And so there you have it.  What we've learned so far.....

  1. We know that we can create a repos with git init.
  2. We know that you can take individual files and set up their changes to be saved using git add.
  3. We know how git commit takes the staged changes of our files and sends them to a saved collection of changes called the repository.
  4. We understand the difference between working directories, staging, and the repository.  The working directory holds the current files being edited.  Staging holds a snapshot ready to commit.  The repository holds a collection of those snapshots.

That should cover the bare basics of git to begin with.  Was this a helpful intro?  Let me know in the comments.









No comments:

Post a Comment