-
Notifications
You must be signed in to change notification settings - Fork 0
MR03 Local Git Repo
In this unit, we'll do the following:
- Initialize an empty Git repository
- Stage our changes
- Commit our changes
- View our project's history
We'll learn about the following concepts or tools:
- The hidden
.git
directory - Working directory
- Staging area
- UNIX:
less
We often create a Git repository on a remote server like GitHub, GitHub Enterprise, or Bitbucket, and then clone it locally. This time, let's initialize the repository on our machines. We can add a remote server later.
$ git init .
Initialized empty Git repository in /Users/dstrus/code/roster/.git/
If you've ever wondered where Git keeps track of everything about a repository, that offers a clue. It's all in that .git
directory. Since its name starts with .
, it is treated as hidden in UNIX environments, so it's easy to overlook. There's already quite a bit in that directory.
Use tree
to check out the file structure inside the .git
directory.
To inspect directory structure from the terminal, use the
tree
command. It's not included by default in OS X. If you use Homebrew, you can quickly install it withbrew install tree
.
$ tree .git
.git
├── HEAD
├── config
├── description
├── hooks
│ ├── applypatch-msg.sample
│ ├── commit-msg.sample
│ ├── post-update.sample
│ ├── pre-applypatch.sample
│ ├── pre-commit.sample
│ ├── pre-push.sample
│ ├── pre-rebase.sample
│ ├── prepare-commit-msg.sample
│ └── update.sample
├── info
│ └── exclude
├── objects
│ ├── info
│ └── pack
└── refs
├── heads
└── tags
Let's check the status of our new repo.
$ git status
On branch master
Initial commit
Untracked files:
(use "git add <file>..." to include in what will be committed)
roster.rb
nothing added to commit but untracked files present (use "git add" to track)
We don't yet have any commits, and nothing is staged to be committed. Our project's lone file, roster.rb
, is presently untracked by Git. It lives only in our working directory.
The working directory contains the files of your project as they are right now, including any changes you've made that haven't yet been added to the repository.
Before we can commit roster.rb
, we have to add it:
$ git add roster.rb
Let's check the status again.
$ git status
On branch master
Initial commit
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: roster.rb
Our new file is now staged and will thus be included if and when we make a commit. It no longer lives only in our working directory. Now it's also in our staging area, or index.
The staging area, or index, is where Git tracks the changes that are ready to be committed, but which have not actually been committed.
Let's commit it!
$ git commit -m "Display a message."
[master (root-commit) 0ecae00] Display a message.
1 file changed, 2 insertions(+)
create mode 100755 roster.rb
Now our code has moved from the staging area to the repository's history. We can confirm this by looking at the log:
$ git log
commit 0ecae00b6d96015fa19047510d2d64dd522b7399
Author: Dave Strus <[email protected]>
Date: Fri Jan 29 10:44:37 2016 -0500
Display a message.
Displaying the log will probably clear your terminal and appear with
(END)
marking the end of the log. It's being displayed withless
, a UNIX utility that displays one screen of text at a time. When you reach(END)
, pressq
to quit and return to the terminal.
Now check the status again:
$ git status
On branch master
nothing to commit, working directory clean
Since all our changes have been committed, our working directory is clean. We're ready to move on.