まず作業ディレクトリを作成します。
$ mkdir -p workspaces/forkwell/local_git
$ cd workspaces/forkwell/local_git$ git init
Initialized empty Git repository in /Users/forkwell/workspaces/forkwell/local_git/.git/
$ ls -a
. .. .git.git/ という隠しディレクトリがリポジトリになります。このディレクトリに履歴が保存されます。
好きなプログラミング言語で "Hello, World" を表示するプログラムを作成してください。下記は Python の例です。
print "Hello world"このファイルをバージョン管理してみましょう。まずは git status を実行してください。
$ git status
On branch master
Initial commit
Untracked files:
(use "git add <file>..." to include in what will be committed)
hello.py
nothing added to commit but untracked files present (use "git add" to track)バージョン管理していないので Untracked files に表示されています。ヘルプに従い git add を実行します。
$ git add hello.py
$ git status
On branch master
Initial commit
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: hello.py無事にインデックスに登録されました。次に git commit でコミットしましょう。
$ git commit -m "Hello Git World"
[master (root-commit) 53aab76] Hello Git World
1 file changed, 1 insertion(+)
create mode 100644 hello.py
$ git status
On branch master
nothing to commit, working tree clean最後に履歴を表示してみます。
$ git log
commit 53aab7648d48ba6f3004db1159ece77cc7cbc295
Author: sinsoku <sinsoku.listy@gmail.com>
Date: Wed Dec 21 17:57:52 2016 +0900
Hello Git World先ほど作成したプログラムにカウントダウンする処理を追加します。
for n in range(3, 0, -1):
print n
print "Hello world"git status を実行します。
$ git status
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: hello.py
no changes added to commit (use "git add" and/or "git commit -a")先ほどとヘルプが変わり git checkout の説明が表示されています。
git add <file>は変更内容をインデックスに登録しますgit checkout -- <file>は変更内容を破棄して、元に戻します
今回は git add を実行します。
$ git add hello.py
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: hello.pyコミットします。
$ git commit -m "Add countdown"ログが2件あることを確認しましょう。
$ git log
commit a7e5b767b0bb6a31d9bec2cbc71c980a3ebdc778
Author: sinsoku <sinsoku.listy@gmail.com>
Date: Wed Dec 21 18:10:42 2016 +0900
Add countdown
commit 53aab7648d48ba6f3004db1159ece77cc7cbc295
Author: sinsoku <sinsoku.listy@gmail.com>
Date: Wed Dec 21 17:57:52 2016 +0900
Hello Git Worldログを見ると分かるように、新しいコミットが上に表示されます。
git branch コマンドでブランチを作成することができます。
$ git branch new_feature
$ git branch
* master
new_featuregit checkout コマンドでブランチを切り替えることができます。
$ git checkout new_feature
$ git branch
master
* new_featureでは、ファイルに簡単な変更を加えてみましょう。(感嘆符を追加しました)
for n in range(3, 0, -1):
print n
print "Hello world!!"この変更をコミットしましょう。
$ git add hello.py
$ git commit -m "Change message"今回はログを表示するときに --graph --decorate のオプションを追加します。
$ git log --graph --decorate
* commit f0de65ea07fd227a97dbb910c650e76741ce5860 (HEAD -> new_feature)
| Author: sinsoku <sinsoku.listy@gmail.com>
| Date: Wed Dec 21 19:03:41 2016 +0900
|
| Change message
|
* commit a7e5b767b0bb6a31d9bec2cbc71c980a3ebdc778 (master)
| Author: sinsoku <sinsoku.listy@gmail.com>
| Date: Wed Dec 21 18:10:42 2016 +0900
|
| Add countdown
|
* commit 53aab7648d48ba6f3004db1159ece77cc7cbc295
Author: sinsoku <sinsoku.listy@gmail.com>
Date: Wed Dec 21 17:57:52 2016 +0900
Hello Git Worldブランチの位置が2つあるのが分かります。
元の master ブランチに切り替えてみましょう。
$ git checkout master
Switched to branch 'master'hello.py のファイルが元に戻ります。
new_feature ブランチで変更した内容を master ブランチに統合(マージ)してみます。
$ git merge new_feature
Updating a7e5b76..f0de65e
Fast-forward
hello.py | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)hello.py に new_feature 変更が反映されました。