I thought I’d do something fun for the holidays this year. To help people learn Git, I’m writing a super-simple introduction to Git, one day at a time, over the course of 12 days.
If you’ve done any programming or web development over the last few years, you’ve at least heard about Git. It has become the version control system of choice, supplanting older programs like Subversion, and winning far more mindshare than other modern distributed version control systems like Mercurial. Even though I had learned to use both of those other version control tools, I remember having a hard time picking up Git—I ended up forcing myself to read the Pro Git book in order to feel comfortable using it.
So, I thought it would be nice to contribute back to the developer community by making a Git tutorial based on the song “The Twelve Days of Christmas”.
Two things to know: First, to follow along with the tutorials each day, you will need to have Git installed on your computer, but the installation instructions in the Pro Git book make that easy. Second, these holiday hints show you how to use Git from the command-line. While there are several excellent GUI tools for Git, the best way to really learn it is by typing the commands yourself.
On the first day of Christmas, my true love gave to me… a folder with a repository.
In order to get all of the advantages of Git, you have to start by preparing your folder to track versions of your program, website, cookbook, or whatever it is that you want to keep a change history of. This is called “initializing a Git repository”.
This is actually the easiest Git command you can run. Let’s say we’re building a Santa-tracker app, and we’re going to store the source code in a folder in our Documents folder. In a terminal while in your home folder, type the following to create that folder and make it your current working directory:
mkdir Documents/santa-tracker cd Documents/santa-tracker/
Here’s the big takeaway from the first day’s lesson! While that folder is your current working directory, type:
You will see a message in your terminal similar to the following:
Initialized empty Git repository in /Users/vwilson/Documents/santa-tracker/.git/
You have just created a hidden sub-folder, named
.git, in this folder. Inside this new sub-folder are all the things Git needs to track the changes you make to your files.
Eventually, you may end up delving into the contents of that folder to do all sorts of cool and magical things, but you never have to look in it at all to harness the full power of Git. Just know it’s there, and don’t accidentally delete it.
(There may be that rare time when you need to delete your
.git folder. If you manage to corrupt your local copy of a Git repository so badly that no Git command can bring it back to a good state, you can delete that hidden sub-folder to start over. More likely, if you save a copy of that folder somewhere else, say, in an online service like GitHub or Bitbucket, you could just delete the whole folder and pull down the online copy of your repository. But that’s in another verse of the song…)
To see all the options available for the command we’ve learned today, check them out in the online Git reference:
Come back tomorrow and learn how to start using your new repository.
The next day’s lesson is: Tracking Files with Git
2 thoughts on “The 12 Days of Git: Learn Git over the Holidays”
“but you never have look in it” -> “but you never have to look in it”
Thanks for catching that typo, Jon Forrest. I’ve fixed the post. And thanks for reading.