Here is a quick table on what you need to do to get git
installed and configured on your machine:
Install git: http://git-scm.com/downloads
Configure identity: Run these commands (substitute your name and email):
$ git config --global user.name "John Smith"
$ git config --global user.email "johnsmith444@example.com"
Here is a recap of the commands required to create your first repo:
$ mkdir git_basics
$ cd git_basics
$ echo '# README #' > README.md
$ echo '# LICENSE #' > LICENSE.md
$ git init
$ touch .gitignore
Now add entries to the .gitignore
file. In the below example, we're going to ignore all sqlite databases in our project. Add the following to the git_basics/.gitignore
file:
# Ignore all sqlite databases
db/*.sqlite3
.DS_Store
If you run the git status
command, you will see the following:
On branch main
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
.gitignore
LICENSE.md
README.md
nothing added to commit but untracked files present (use "git add" to track)
This shows us that we currently have three files with changes that are not currently staged. If you look at your repository with a git GUI application you should see something like this:
You can see that it is showing the same information. Three files with unstaged changes, and no files with staged changes.
Use git add <file>
to now stage the affected files.
$ git add .gitignore LICENSE.md README.md
Now we can do git status
again after staging those files:
On branch main
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: .gitignore
new file: LICENSE.md
new file: README.md
You can see the changes are now in a "Changes to be committed" state. If we look in the GUI application again, you will see the files are now marked as staged:
$ git commit -m 'Add first project files'
Now, if we do a git status
, our status now shows that there are no more changes that need to be committed:
On branch main
nothing to commit, working directory clean
To verify that the commit was indeed created, use git log
to see a historical version of commits, and you should see the last commit created:
commit 8466981224ab4b6837b95d9f8208cd345569267c
Author: John Smith <johnsmith444@example.com>
Date: Mon Oct 6 15:24:38 2014 -0700
Add first project files
Here is a reference to all of the commands that we have learned so far, along with their purpose. Notice that they all interact with the local system or repository. These commands are never used to interact with remote repositories.
Command | Purpose |
---|---|
git init |
Creates a new repository in the current directory. Generates a .git directory. |
git config |
Sets git configuration settings, such as author email and name. |
git status |
Shows the working directory as well as the staged changes. Run this command liberally to see which changes are ready for committing. |
git add FILENAME |
Stage file changes, which prepares them to be added to the repository. |
git commit -m "COMMIT MESSAGE" |
Creates a commit from staged files. Commits are the atomic unit in git that gets moved around between branches and repositories. |
Because git is a distributed version control system, one of the most confusing and also most important things to remember is the difference between local vs remote repositories. Local repositories are located on your computer, and remote repositories are located on a remote server. You will always issue git commands within a local git repository. Later, you will learn how to work with remote repositories from a local repository.