git のチュートリアルを試してみる

参考は git チュートリアル (バージョン 1.5.1 以降用) にする。

1.リポジトリを生成(初期化)する

% mkdir -p projects/sample
% cd projects/sample
% git init
git: 'init' is not a git-command

The most commonly used git commands are:
    add            Add files to the index file
    apply          Apply patch on a git index file and a work tree
    archive        Creates a archive of the files in the named tree
    bisect         Find the change that introduced a bug
    branch         List, create, or delete branches.
    checkout       Checkout and switch to a branch
    cherry-pick    Apply the change introduced by an existing commit
    clone          Clones a repository
    commit         Record your changes
    diff           Show changes between commits, commit and working tree, etc
    fetch          Download objects and a head from another repository
    grep           Print lines matching a pattern
    init-db        Creates an empty git repository
    log            Show commit logs
    merge          Grand Unified Merge Driver
    mv             Move or rename a file, directory or symlink
    prune          Prunes all unreachable objects from the object database
    pull           Pull and merge from another repository or a local branch
    push           Update remote refs along with associated objects
    rebase         Rebase local commits to a new head
    reset          Reset current HEAD to the specified state
    revert         Revert an existing commit
    rm             Remove files from the working tree and from the index
    show           Show one commit with difference it introduces
    show-branch    Show branches and their commits
    status         Show working tree status
    tag            Create a tag object signed with GPG
    verify-tag     Check the GPG signature of tag
(use 'git help -a' to get a list of all installed git commands)

git init と実行すると、そんな git-command はないと怒られる。
Debian にインストールした git のバージョンが古いのが原因。この場合は、git init-db で行う。

% git init-db
defaulting to local storage area
% ls -lR 
.git:
total 36
-rw-r--r-- 1 littlebuddha wheel   23 Feb 13 00:42 HEAD
drwxr-xr-x 2 littlebuddha wheel 4096 Feb 13 00:42 branches
-rw-r--r-- 1 littlebuddha wheel   53 Feb 13 00:42 config
-rw-r--r-- 1 littlebuddha wheel   58 Feb 13 00:42 description
drwxr-xr-x 2 littlebuddha wheel 4096 Feb 13 00:42 hooks
drwxr-xr-x 2 littlebuddha wheel 4096 Feb 13 00:42 info
drwxr-xr-x 4 littlebuddha wheel 4096 Feb 13 00:42 objects
drwxr-xr-x 4 littlebuddha wheel 4096 Feb 13 00:42 refs
drwxr-xr-x 2 littlebuddha wheel 4096 Feb 13 00:42 remotes

.git/branches:
total 0

.git/hooks:
total 36
-rw-r--r-- 1 littlebuddha wheel  441 Feb 13 00:42 applypatch-msg
-rw-r--r-- 1 littlebuddha wheel  588 Feb 13 00:42 commit-msg
-rw-r--r-- 1 littlebuddha wheel  152 Feb 13 00:42 post-commit
-rw-r--r-- 1 littlebuddha wheel  207 Feb 13 00:42 post-update
-rw-r--r-- 1 littlebuddha wheel  388 Feb 13 00:42 pre-applypatch
-rw-r--r-- 1 littlebuddha wheel 1696 Feb 13 00:42 pre-commit
-rw-r--r-- 1 littlebuddha wheel 4262 Feb 13 00:42 pre-rebase
-rw-r--r-- 1 littlebuddha wheel 2555 Feb 13 00:42 update

.git/info:
total 4
-rw-r--r-- 1 littlebuddha wheel 240 Feb 13 00:42 exclude

.git/objects:
total 8
drwxr-xr-x 2 littlebuddha wheel 4096 Feb 13 00:42 info
drwxr-xr-x 2 littlebuddha wheel 4096 Feb 13 00:42 pack

.git/objects/info:
total 0

.git/objects/pack:
total 0

.git/refs:
total 8
drwxr-xr-x 2 littlebuddha wheel 4096 Feb 13 00:42 heads
drwxr-xr-x 2 littlebuddha wheel 4096 Feb 13 00:42 tags


.git/refs/heads:
total 0

.git/refs/tags:
total 0

.git/remotes:
total 0

そうすると、初期化した git のリポジトリ「.git」ディレクトリが生成される。

コミットするユーザーを登録する

登録をしないと、コミットするときに下記のメッセージが出て、怒られる。

% git commit -m 'first commit.'
Committing initial tree 63d9e1459f06895eb3c13a227400e761fa75db24

*** Environment problem:
*** Your name cannot be determined from your system services (gecos).
*** You would need to set GIT_AUTHOR_NAME and GIT_COMMITTER_NAME
*** environment variables; otherwise you won't be able to perform
*** certain operations because of "empty ident" errors.
*** Alternatively, you can use user.name configuration variable.

fatal: empty ident  <littlebuddha@example.com> not allowed

ユーザー名とメールアドレスを登録する。
git のバージョンが 1.4.4.4 の場合は、

% git repo-config --global user.email youraccount@example.com
% git repo-config --global user.name "Your Name"  

とする。git のバージョンが 1.5.1 以上の場合

% git config --global user.email youraccount@example.com
% git config --global user.name "Your Name"  

とする。

コミットする((修正・変更された)データを登録する)

まずは簡単にテキストファイルを作成して、コミットしてみる。

% echo 'sample date' > sample.txt
% ls -la
total 16
drwxr-xr-x 3 littlebuddha wheel 4096 Feb 13 00:24 .
drwxr-xr-x 7 littlebuddha wheel 4096 Feb 12 21:46 ..
drwxr-xr-x 8 littlebuddha wheel 4096 Feb 12 21:47 .git
-rw-r--r-- 1 littlebuddha wheel   12 Feb 13 00:24 sample.txt

そして、コミットする。

% git add sample.txt
% git commit -m "first commit."                            
Committing initial tree 63d9e1459f06895eb3c13a227400e761fa75db24

branch を作成する

git でいう branch は、Subversion で運用する branches と tags、trunk を一緒にして運用する考え方(のようだ)。
現在、登録されている branch を表示してみる。

% git branch
* master

sample という名称の branch を生成する。

% git branch sample 
% git branch 
* master
  sample

そして、sample の branch を選択する。

% git checkout sample
% git branch
  master
* sample

選択をすると、現在選択されている branch の名称の前に「*」が移動しているのがわかる。
新たにファイルを作成し、sample の branch にコミットしてみる。

% echo 'this is sample file for a new branch.' > sample.branch.txt 
% git add sample.branch.txt 
% git commit -m 'I made a new file to commit for a new branch.'
% ls -la
total 20
drwxr-xr-x 3 littlebuddha wheel 4096 Feb 13 21:57 .
drwxr-xr-x 7 littlebuddha wheel 4096 Feb 12 21:46 ..
drwxr-xr-x 8 littlebuddha wheel 4096 Feb 13 21:58 .git
-rw-r--r-- 1 littlebuddha wheel   38 Feb 13 21:57 sample.branch.txt
-rw-r--r-- 1 littlebuddha wheel   12 Feb 13 00:24 sample.txt

commit したことを確認してみる。

% git log -p
commit ccffe14ceb9df422c302b12a781d71aeea77d53d
Author: Your name <youraccount@example.com>
Date:   Fri Feb 13 21:58:26 2009 +0900

    I made a new file to commit for a new branch.

diff --git a/sample.branch.txt b/sample.branch.txt
new file mode 100644
index 0000000..750d0da
--- /dev/null
+++ b/sample.branch.txt
@@ -0,0 +1 @@
+this is sample file for a new branch.

commit 5737ddead517b96c0826e54be476f9c5bbd6d520
Author: Your name <youraccount@example.com>
Date:   Fri Feb 13 00:56:10 2009 +0900

    first commit.

branch をマージしてみる

...