Data Storage

Git is, at its very core, all about data storage. Let's say, for whatever reason, you have a project. Further, let's assume your project is stored in one or more files in a directory.

Maybe your project is very complicated. Maybe, during the course of your project, you'll make a change that you later regret. You may long to restore your project to the way it was moments ago, undoing your change.

Perhaps a problem will appear that you know was not there last month. Even if you have a backup from last month, you've made many changes since then. You don't want to lose precious time going back to an old backup. If only you could find the one change the broke it, and roll back only that change.

Git can help you in these situations, but only if you start using it NOW. I mean it. You probably do have a project like this, and the sooner you have it under version control, the better.

Fortunately, starting is very simple.

In your terminal, go to the directory where your project is stored:

$ cd ~/source/gitjutsu

Now run the git init command:

$ git init

You should now see something like this:

meh@kolot:~/source/gitjutsu$ git init
Initialized empty Git repository in .git/

You now have a Git repository, but it's not doing you any good just yet.

The next step is to give Git a list of the files that are important to you. These are called "tracked files". You can add files using the add command, like so:

$ git add 'filename'
(Technically, this adds them to a thing called the "staging area", but you don't need to worry about that for now.)

If all of the files in a directory are important to you, add that directory. You may use . to refer to the current directory.

Finally, to tell Git to take a snapshot of all the tracked files, use the commit command like so:

$ git commit -a

Git will open a text editor, probably nano. It expects you to write a short message describing the snapshot. The first time, you can write something like "initial version".

IMPORTANT: If you do not write a message in the text editor, Git will NOT take a snapshot of your files. You must provide a commit message if you want Git to track your changes.

You should see something like this in your terminal:

meh@kolot:~/source/gitjutsu$ git commit -a
Created initial commit 70e003e: initial commit
 1 files changed, 14 insertions(+), 0 deletions(-)
 create mode 100644 outline

If you do, that means the current state of your files has been saved in .git. Once it's saved this way, it's very hard to permanently delete it accidentally. Naturally, if you delete .git or tamper with it, you can lose your commits.

Commit early and commit often. It helps if you keep your changes in small but logical groupings so that your commit messages will make sense to you later. But above all, commit often, and don't worry too much about how the history will look. The important thing is to HAVE a history.

One more thing: if you've made a change that you haven't committed, and you want to get rid of that change, run:

$ git reset --hard HEAD

That will take you back to your most recent commit, wiping out any changes you've made.

Vincent Povirk