Now that you've got a working Git installed, let's create a test repository. On Linux or OS X, open a terminal (or switch to the one you used to install Git).
On Windows, open the Git shell by doubleclicking that icon on the desktop:
I like to keep all of my repositories in a "code" directory, so as not to clutter up the top level of my home directory or my desktop. You might want to do the same.
First, make an empty directory:
mkdir -p code/project cd code/project
If you do ls -a
, to list all files in the directory, you should just see .
and ..
, special files which point at the current directory (.
) and its parent (..
).
Next, initialize an empty repository with git init
, and try ls -la
to show a long listing of all the files:
The .git
directory you see is where Git will keep a record of changes you make to the project, along with metadata about things like where other copies of the repository live. (By longstanding convention on Unix systems, files that start with a leading dot are hidden from the user most of the time.)
For the most part, you don't need to worry about this directory, but it's useful to know that it exists. A Git repository is just like any other folder full of files, except that it contains a .git
.
A commit, in the world of Git, is a collection of changes to files along with a (hopefully) human-readable description of the changes and some metadata about who and when. It's a little bit like an entry in a logbook.
In order to start recording changes to files, you'll first need at least one file to work with. By convention, most repositories have a README
or README.md
(formatted with Markdown) that describes their content. Let's make one of those:
touch README.md ls
Now you'll want to add some text. On a Linux or OS X system, try nano README.md
, write a description, and hit Ctrl-X to save:
On Windows, try typing start .
to bring up an Explorer window for the current directory, and drag README.md into a text editor (Notepad will work just fine, in a pinch):
Once the file is saved, you can use git status
to see what's changed:
Before we go any further, Git wants some basic information from us. Since every commit is associated with a user's name and e-mail address, let's set those system-wide:
git config --global user.email "[email protected]" git config --global user.name "Your Name"
Additionally, Windows users may want to set up a different editor for commit messages, since otherwise you'll find yourself using Vim, which can be profoundly confusing. Here's how to use Notepad:
git config --global core.editor "notepad.exe"
With that out of the way, here's how to commit the new file:
git add README.md git commit
git add
is for adding (or "staging") files to something called the index, which is the list of changes that will be recorded in the next commit.
At this point, you should be looking at a text editor. Here is what you need to know:
- The first line is like the subject of an e-mail: A short description of the change you've just made.
- After the first line, you can add a blank line followed by a more detailed description of the change, if necessary. Sometimes people write many paragraphs here, depending on the scope of changes. Try to think like a future version of yourself who wants to remember what you were thinking at the time you did something.
- The first line should be short - 50 characters or so, if you can manage it.
- Subsequent paragraphs should be hard-wrapped (press enter) after 72 characters.
- Lines that start with a
#
are comments. They won't be recorded.
Something like "initial commit of README.md" is probably sufficient for a first commit like this one. Type that in, save the commit, and exit the eidtor (hit Ctrl-X in Nano, in other editors, make sure you save the file and then exit):
You should now find yourself back at the command prompt, looking at a summary of the commit something like this one:
Now you can use a couple of commands to see where you're at:
git status git log
git status
now lets you know that you haven't made any changes since the last time you committed.
git log
displays the recent history of commits.
Notice the first line of that log entry? Yours will be different, but it will look a lot like this:
commit 813c3f799229bd1189e61514d9bb7f0e6620c30b
That long, not-especially-meaningful-looking string of numbers and letters is a hash, specifically a SHA-1 of various things. This hash functions as a commit id, a unique identifier for each set of changes you record. From now on, you can always refer to that first commit by its hash.
You can also see the specifics of any given commit this way, with git show <commit>
:
This will display not only the author, date, and full commit message, but also a unified diff of the files that changed, with all of the changed lines.
Text editor powered by tinymce.