Really Friendly Git Intro
Really Friendly Git Intro
Really Friendly Git Intro
Tracy Osborn
This book is for sale at
http://leanpub.com/really-friendly-git-intro
1
Hi friends, I’m Tracy! 2
What’s Git?
Say you want to learn to program. You’re going to start
with Python, Javascript, or another language (doesn’t mat-
ter which!) and start creating little programs or scripts.
Think of Git as a way to create little waymarkers, or
save points, at points in your code’s history. When you
finish a specific feature, or finish a chapter, you can save
the current version of your code. As you check-in your
changes, you’ll create a log of your history.
Create a directory and a file in your command line or in your code editor,
whichever works best for you.
4
Let’s start playing! 5
Git is letting us know that nothing has been committed and that we have
one untracked file.
git status will tell you what files are being tracked and
whether there are changes made that haven’t been “checked-
in” yet. Here, we can see that Git is running for this
directory, but it isn’t currently tracking any files. It wants
to be smart and not assume all files in a directory should
be tracked, leaving it up to you to tell it what to follow.
Now that we’ve told Git to track some files, run git status
again to see what changed. We can see that the file we
created earlier is here, and Git is letting us know that it
hasn’t been saved yet into our Git history.
Let’s commit our current status into the Git history, and
write a message to go along with it:
git commit -a -m "First commit"
Let’s start playing! 9
A lot of gobbledegook! It’s comparing the before and after of the file and
at the bottom of the output, you can see I added a new line to my file.
If you leave off the filename, git diff will show you all the
changes across all your files. This is a nice way to review
your work before making a commit and check it for things
that you actually don’t want to check in (like debugging
statements and whatnot.)
Let’s start playing! 12
Nice! To exit this screen, just press “q” to quit. We’ll return
back here soon.
Tada: After checking out our file, it’s returned back to the state it was in
when it was last committed.
Remember when you ran git log and every commit had
a random string assigned to it that looked like commit
5e4943f93ad5c242a51c1d6a19d6be5d09c938fd? You can also
use git checkout COMMITHASH to go back in time to that
commit entirely. This is how you can hop back and forth
in time to different states of your code, if you like.
Let’s start playing! 14
We want to save the history of our code, but there are a lot
of other files involved in programming that we don’t want
to save or share. For example, the plugins we install or files
with sensitive information that we don’t want to publish
on GitHub.
We can list out those files and directories in our .gitignore
file. These aren’t added by Git when we initialize the
repository with git init — we’ll create the file manually
and save it in the right place, and Git will automagically
read the file and ignore files and directories that match
what’s in our ignore file.
The name of the file is important (make sure to include
the “.” at the front of “.gitignore”). It should live next to the
hidden .git directory.
To test this out, let’s create a dummy file called “ig-
noreme.txt.” First, check that Git sees it with git status:
Let’s start playing! 15
You can also create the file using your text editor if that’s easier.
17
Intermediate Git: Creating branches! 18
Now that we can see what branches are available with git
branch, we can switch between the two with git checkout
BRANCHNAME.
Intermediate Git: Creating branches! 20
When you run git merge it’ll let you know if there were
any conflicts and will list out the files with those conflicts.
In those files, Git will have added >>>>> marks where there
is a conflict, and will include both versions of the conflicted
code, so you can manually pick and choose what to keep
and what to lose.