Here is a quick table on what you need to do to get
git installed and configured on your machine:
|1. Install git||http://git-scm.com/downloads|
|2. Configure identity||
git config --global user.name "John Smith"
git config --global user.email "email@example.com"
|3. Configure ignored files||Add entries to the repository's .gitignore file.|
Here is a recap of the commands required to create your first repo:
$ mkdir git_basics $ cd git_basics git_basics/ $ echo '# README #' > README.md git_basics/ $ echo '# LICENSE #' > LICENSE.md
git_basics/ $ git init
git_basics/ $ 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
# Ignore all sqlite databases db/*.sqlite3
If you run the
git status command, you will see the following:
On branch master Initial commit 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 took a look at your repository with a git GUI application you should see something like this (we're using gitx below):
You can see that it is showing the same information. Three files with unstaged changes, and no files with staged changes.
git add <file> to now stage the affected files.
git_basics/ $ git add .gitignore git_basics/ $ git add LICENSE.md git_basics/ $ git add README.md
Now we can do
git status again after staging those files:
On branch master Initial commit 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_basics/ $ 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 master 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 <firstname.lastname@example.org> 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.
|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.