2012年4月22日日曜日

Gitのインストール - Mac OS X Lion

Git(ギット)とは

主に文章(ソースコードなど)のバージョンを管理するためのソフトウェアです。もっぱらの主流ということもあり、「cvs」「svn」「git」という流れで利用している方も多いはずです。公式サイトを見ると分かる通り、以下のような大規模プロジェクトに採用されることも多く、その利便性の高さを証明しています。
  • Git(自身もまたgitで管理されています)
  • Linux Kernel
  • Perl
  • Eclipse
  • Gnome
  • KDE
  • Qt
  • Ruby on Rails
  • Android
  • PostgreSQL
  • Debian
  • X.org
もちろん、Subversionを利用しているプロジェクト全てがGitに置き換えられている訳ではなく、SubversionはSubversionで良い所も多いと感じています。しかし、Gitで管理する方が自分のソースコード管理のやり方に合っているということもあるはずなので、まずは使ってみてはいかがでしょうか。


この記事の前提

  • Mac OS X Lion を利用しています
  • Mac Ports をインストールしている必要があります
    • まだの場合は、以前の記事を参照してインストールしてください
    • 公式サイトのパッケージ置き場にMac OS X用のパッケージもあるのですが、手動でしなければならないことも多く、また今回はbash completionもインストールするため、Mac Portsを利用しています。

手順

  1. Mac Portsを使ったインストール
  2. 簡単なコマンド紹介

1. Mac Portsを使ったインストール

1-1. 非常に簡単です。以下のコマンドを入力するだけでインストールできます。
$ sudo port install git-core +bash_completion
Password:
--->  Computing dependencies for git-core

... (snip) 少し時間がかかります ...

--->  Installing git-core @1.7.10_0+credential_osxkeychain+doc+pcre+python27
--->  Activating git-core @1.7.10_0+credential_osxkeychain+doc+pcre+python27
--->  Cleaning git-core

1-2. bash completionの設定をします。
$ vim ~/.bash_profile
if [ -f /opt/local/etc/bash_completion ]; then
    . /opt/local/etc/bash_completion
fi
$ source ~/.bash_profile

1-3. PATHとバージョンを確認します。
$ echo $PATH
/opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin:
$ git --version
git version 1.7.10

2. 簡単なコマンド紹介

詳しいコマンドの説明は「Git Reference site(英語)」「official Git tutorial(英語)」「Pro Git」「Git入門」「Gitを使いこなすための20のコマンド - SourceForge.JP Magazine : オープンソースの話題満載」が分かりやすく参考になります。

2-1. まずはプロジェクトの複製を取得してみます
$ mkdir ~/Desktop/git
$ cd ~/Desktop/git
$ git clone git://github.com/git/hello-world.git
$ cd hello-world
$ ls
BIT.bit                 coldfusion.cfm          javascript.js
Eiffel.eiff             csharp.cs               linux-x86.nasm
Io.Io                   d.D                     lua.lua
ada.ada                 delphi.delphi           nu.nu
apc.apc                 delphi.pas              ocaml.ml
applescript.scpt        dos.bat                 pascal.p
awk.awk                 dylan.dl                perl.pl
befunge.be              erlang_hw.erl           php.php
boo.boo                 focal.fc                python.py
brainf*ck.bf            fortran.f90             ruby.rb
c++.cpp                 fortran77.f77           scheme.scm
c.c                     haskell.hs              shell.sh
clipper.prg             hq9+.h                  telephone
clisp.lisp              java.java               turing.oot
色々な言語でのHello Worldが含まれています。

2-2. 適当に編集してみましょう
$ vim c.c
#include <stdio.h>

int main(void) {
  printf("Hello Git\n");  //<=編集します
  return 0;
}

2-3. 差分を見てみます
$ git diff
diff --git a/c.c b/c.c
index 36465c8..0b5fa83 100644
--- a/c.c
+++ b/c.c
@@ -1,6 +1,6 @@
 #include <stdio.h>
 
 int main(void) {
-   printf("Hello World\n");
+   printf("Hello Git\n");
   return 0;
 }

2-4. Objective-Cのファイルが無いので追加してみます(ほぼCと同じで恐縮です)
$ vim objective-c.m
#include <stdio.h>

int main(void) {
    puts("Hello World");
    return 0;
}

2-5. 変更点を確認します
$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       new file:   objctive-c.m
#
# 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:   c.c
#

2-6. c.c がコミット対象となっていないので追加します
$ git add c.c
$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       modified:   c.c
#       new file:   objective-c.m
#

2-7. コミットします
$ git commit -m 'first commit'
[master 4ba67e2] first commit
 2 files changed, 7 insertions(+), 1 deletion(-)
 create mode 100644 objective-c.m
コミットしましたが、実際に変更されているのはローカルのリポジトリだけなので、もちろん github.com の方には何も影響ありません。

0 コメント:

コメントを投稿