#topicpath();
/////////////////////////////////////////////////////////////////////////////////
#contents();

/////////////////////////////////////////////////////////////////////////////////
* Links [#links]
** Git について [#about-git]
- [[Gitを使いこなすための20のコマンド>http://sourceforge.jp/magazine/09/03/16/0831212]]
- [[サルでもわかるGit入門>http://www.backlog.jp/git-guide/]]
- [[もっと早く知りたかった! Gitが鬼のようにわかるスライド厳選7選>http://www.find-job.net/startup/7-git-slides]]
- [[Git の基礎勉強 ~ Git によるバージョン管理>http://tracpath.com/bootcamp/learning_git_firststep.html]]
- [[Gitによるバージョン管理入門 for windows>http://www.plowman.co.jp/school/Git/Git.html]]

** Downloads [#xb3ff64e]
- [[Git 配布元>http://git-scm.com/]]



/////////////////////////////////////////////////////////////////////////////////
* 使い方 [#qe3c4374]
//===============================================================================
** git init : 新規リポジトリ作成 [#git-init]
+ 空リポジトリの作成
 $ mkdir hoge.git         # bare repository 用の空ディレクトリを作成
 $ cd hoge
 $ git init --bare --shared=true   # bare repository として初期化。管理ファイル群が生成される。共有にするには --shared=true にする
+ 作業用リポジトリの作成
 $ cd ..
 $ git clone hoge          # hoge.git から hoge という名前で bare ではないリポジトリとして複製する
+ 作業用リポジトリでファイルを登録
 $ cd hoge
 /* 何かしらの方法で新しいファイル fuga を置く */
 $ git add fuga           # ファイル "fuga" が登録対象になる(まだ登録されていない)
 $ git commit             # 前の git add で指定されたファイルが登録される 



//===============================================================================
** git commit : 登録済みのファイルに変更が加えられた場合 [#git-commit]
- 再び add, commit を行う
 $ git add <変更を加えたファイル>
 $ git commit

//-------------------------------------------------------------------------------
*** git commmit --amend : commit をやり直す [#git-commit--amend]
- 既に commit 済みのものを修正して commit する場合、
 (1)再度 commit する。履歴は全て別々に残る
 (2)commit をやり直す。履歴はやり直した分だけが残る
の2つの選択肢がある。やり直しの対象となる commit のログを残したくない場合、やり直し時に "--amend" オプションをつけることで実現できる。
 $ git add hoge
 $ git commit --amend



//===============================================================================
** git revert : commit を取り消す [#git-revert]
- 「取り消す」とはいっても、 commit の痕跡自体は消せるわけではなく、単に元に戻すための commit ができますよ、という機能。
+ まず、git log で対象 commit の hash を確認
 $ git log
 commit bdf1da8aecdfbe5a83ce93e78e2ad8a241b1530b
 Author: hogehoge <hogehoge@fuga.com>
 Date:   Mon May 4 13:33:13 2015 +0900
 
     fix build break for System
 
 commit 1072326e20c9dd482d5e1db46419b0412317d693
 Author: hogehoge <hogehoge@fuga.com>
 Date:   Mon May 4 13:12:33 2015 +0900
 
     delete Makefile.bak
 
 ...
+ 対象を確認したら、 hash を指定して revert する
 $ git revert bdf1da8aecdfbe5a83ce93e78e2ad8a241b1530b
-- 即座に commit log 編集画面になる。

//===============================================================================
** git reset: git commit や git add をなかったことにする [#git-reset]
 $ git reset [commit-hash]
- [commit-hash] を取り消す
- [commit-hash] を指定しなかった場合、HEAD が指定されたとみなされる
- [commit-hash] には以下が使用出来る:
-- HEAD: 最新
-- <commi-hash>^ or <commit-hash>^1 : <commit-hash> の1個前
-- <commi-hash>^^ or <commit-hash>^2 : <commit-hash> の2個前
- 但し、remote へ push 済みの commit に対して reset を行なっても、 reset を remote には push 出来ないようなので注意が必要。
- --hard を使用すると、作業中のリポジトリに状態が反映される
- --soft (もしくは指定ナシ)の場合、git のステータスが変わるだけになる。


//===============================================================================
** branch の名前 [#branch-name]
*** リポジトリの現在の branch 名を表示 [#g036243a]
 $ git branch
 * work

*** branch 名を変更する [#x3a53663]
- デフォルトでは "master" になっているが、用途に合わせて任意の名前を付けることが出来る。
- 変更するリポジトリ内にて、以下のようにする:

 $ git branch -m <元の名前> <新しい名前>



//===============================================================================
** リポジトリの変更を他のリポジトリに反映する [#git-push]
 $ git push <反映先> <反映元>


//===============================================================================
** branch 操作 [#y0bb3352]
*** branch 作成 [#t63da454]
- 例として新しく作成する branch の名前を "work1" とする
- (例えば masterのディレクトリ内で)次の操作を行う
 $ git checkout work1
*** branch 切り替え [#d2701e8e]
- 切り替えの時も git branch を実行する。操作が成功すれば以下のようにブランチが切り替わったことを通知するメッセージが表示される
 $ git checkout work1
 Switched to branch 'work1'
-- この時、元の branch と work1 の間に差分があれば、現在のディレクトリの内容も work1 の最新の状態に切り替わる
-- 但し、元の branch で修正して commit さてていない変更点がある場合、その差分はそのまま残される。つまりその変更点が存在する状態で work1 に切り替わっている

*** 不要になった branch の削除 [#wdab01db]
- 不要になった branch work1 を削除するには、
 $ git branch -d work1

//===============================================================================
** 他の branch に commit を反映する [#git-merge]
 $ git merge <反映先 branch>


//===============================================================================
** tag  [#git-tag]
- 現在のリポジトリの最新に対してタグ <tag-name> を付ける場合
 $ git tag <tag-name>
- 特定の commit <commit-hash> に tag  を付ける場合
 $ git tag <tag-name> <commit-hash>
- 既に親 repository に push してしまった commit に後から tag を打っても、親 repository には反映できない。~
→ git push --tags を使う
 $ git push --tags # 全ての tag 情報を push する
- tag の削除
 $ git tag -d <tag-name>

//===============================================================================
** commit されていない変更点の確認 [#v1309767]
- 全ての差分を表示
 $ git diff
- 変更された管理対象のファイルの一覧を見る
 $ git clean [-n]
-- -n の代わりに -f を付けると、全ての変更を revert する
- 管理対象外のファイルも含め、変更ファイルの一覧を見る
 $ git status


//===============================================================================
** conflict への対処 [#conflict]
- 競合する修正が入っている場合、手元のファイルが最新ではないと判断されるため、push に失敗する。そこで一旦 pull をすることになるが、ここで下記のようなエラーメッセージが表示される:
 $ git pull
 remote: Counting objects: 5, done.
 remote: Compressing objects: 100% (2/2), done.
 remote: Total 3 (delta 0), reused 0 (delta 0)
 Unpacking objects: 100% (3/3), done.
 From /home/kazu/work/git_test/a
    1413a9c..ac702ad  master     -> origin/master
 Auto-merging 1.txt
 CONFLICT (content): Merge conflict in 1.txt
 Automatic merge failed; fix conflicts and then commit the result.

- conflict の発生例
 AAAAA
 <<<<<<< HEAD
 BBBBBBBBB
 CCCCCCCCCCCCCCCCCCCCCCC
 DDDD
 EEEEEEEEEEEEE
 =======
 111111111111111111
 22222222222
 33333333333333333333
 4444444
 >>>>>>> ac702ad3f09388ac676c926b7a03c2a97a77f94f
-- 書式
  <<<<<<< HEAD
 (手元での修正)
 =======
 (<commit-name> によって変更された修正)
 >>>>>>> <commit-name>

- conflict の編集
-- git mergetool を実行すると merge 用のプログラムが呼び出される。
--- emacs + magit を使用している場合は、magit の画面から通常の手順で ediff を呼び出す
-- conflict 編集後、 git add <file-name>; git commit すると、conflict を解消した旨のコメントが予め用意された log 記入画面が表示される:
  Merge branch 'master' of /path/repos-dir
  
  Conflicts:
      1.txt
  #
  # It looks like you may be committing a merge.
  # If this is not correct, please remove the file
  #   .git/MERGE_HEAD
  # and try again.
  ...


//===============================================================================
** git log : ログの表示(変更履歴の閲覧)[#git-log]
- オプション無し
 commit 772a23b56ce10fd0b986d84a265ed25d5723cee8
 Author: user-name <hoge@example.com>
 Date:   Sun Jul 5 12:16:20 2015 +0900
 
     [mod] disc.shtml: modify link URI

- --stat ファイル名を表示する
 commit 772a23b56ce10fd0b986d84a265ed25d5723cee8
 Author: user-name <hoge@example.com>
 Date:   Sun Jul 5 12:16:20 2015 +0900
 
     [mod] disc.shtml: modify link URI
 
  disc.shtml |    2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)

- --name-status : ファイル名とステータスを表示する(svn log -v とほぼ同内容の情報)
 commit 772a23b56ce10fd0b986d84a265ed25d5723cee8
 Author: user-name <hoge@example.com>
 Date:   Sun Jul 5 12:16:20 2015 +0900
 
     [mod] disc.shtml: modify link URI
 
 M       disc.shtml

- --numstat
 commit 772a23b56ce10fd0b986d84a265ed25d5723cee8
 Author: user-name <hoge@example.com>
 Date:   Sun Jul 5 12:16:20 2015 +0900
 
     [mod] disc.shtml: modify link URI
 
 1       1       disc.shtml

- --name-only : ファイル名の情報のみ追加する
 commit 772a23b56ce10fd0b986d84a265ed25d5723cee8
 Author: user-name <hoge@example.com>
 Date:   Sun Jul 5 12:16:20 2015 +0900
 
     [mod] disc.shtml: modify link URI
 
 disc.shtml

//===============================================================================
** git reflog : リポジトリ上での git 作業履歴を表示する [#git-reflog]
- [[git log>#git-log]] が commit 履歴のみを表示するのに対し、git reflog では、 commit 以外の作業履歴、例えば pull reset の履歴も表示する。
- 操作例
 $ git reflog
 fff7841 HEAD@{9}: pull: Merge made by the 'recursive' strategy.
 9fdb195 HEAD@{10}: commit: Modify Link URI
 e56109d HEAD@{11}: reset: moving to HEAD^
 d5df2c7 HEAD@{12}: reset: moving to d5df2c7858f198496fca335d22dec36a50bb91b0
 69ab6a1 HEAD@{13}: pull: Fast-forward
 d5df2c7 HEAD@{14}: reset: moving to d5df2c7858f198496fca335d22dec36a50bb91b0
 69ab6a1 HEAD@{15}: pull: Fast-forward
 d5df2c7 HEAD@{16}: reset: moving to d5df2c7858f198496fca335d22dec36a50bb91b0
 69ab6a1 HEAD@{17}: commit: 再修正
 d5df2c7 HEAD@{18}: commit: リンク集 URI 修正

//===============================================================================
** git show : 詳細を表示する [#git-show]
- git show <commit-hash>
-- 指定した <commit-hash> の詳細を表示する
- git show <tag>
-- tag が -a や -m で付加情報付きで打たれている場合、Date, Tagger などの情報を表示する。


//===============================================================================
** git submodule : リポジトリに他のリポジトリを組み込む [#git-submodule]
- git のリポジトリの中に、他のリポジトリを組み込むためのもの

//-------------------------------------------------------------------------------
*** submodule の組み込み [#git-submodule-add]
- 既存のリポジトリ foo のディレクトリ構造の中に、別のリポジトリ bar を組み込む場合
 foo: http://foobar.com/foo.git   # aa.txt が commit されている
 bar: http://foobar.com/bar.git   # ps-ax.log が commit されている
+ foo.git の clone
 $ git clone http://foobar.com/foo.git
+ foo 内にディレクトリ baz を作り、外部リポジトリの http://foobar.com/bar.git を組み込む
 $ cd foo
 $ git submodule add http://foobar.com/bar.git baz  # ここでディレクトリ foo/baz が作成される
 Cloning into 'baz'...
 done.
 $ ls -a
 .  ..  .git  .gitmodules  aa.txt  baz  # .gitmodules と baz が追加された
 
 # ここで、baz が初期化される場合とされない場合がある。
 # git submodule とtype したときに表示される hash に "-" がついていれば、 baz は初期化されていない。
 # この場合は次のように手動で初期化する必要がある
+ baz の初期化
 $ git submodule init baz
 Submodule 'baz' (http://foobar.com/bar.git) registered for path 'baz'
 $ git status
 # On branch master
 # Changes to be committed:
 #   (use "git reset HEAD <file>..." to unstage)
 #
 #	new file:   .gitmodules
 #	new file:   baz
 #
+ 組み込んだ submodule を commit & push する。この時 git add は不要。
 $ git commit
 $ git push
 
//-------------------------------------------------------------------------------
*** submodule を組み込んだリポジトリを clone する [#la4acbc3]
+ foo を clone する
 $ git clone http://foobar.com/foo.git new-foo
 $ cd new-foo
 $ ls
 aa.txt  baz  # 先ほど追加し、 commmit & push した baz が出来ている
 $ ls -a baz
 .  ..     # 単にからディレクトリが出来ただけ。
 $ git submodule
 -386b630fd974b3f3147d462cda2c1f50813131fc baz    # hash に "-" が付いており、初期化されていないことが分かる
+ submodule を初期化する
 $ git submodule init baz
 Submodule 'baz' (http://foobar.com/bar.git) registered for path 'baz'
 $ ls -a baz
 .  ..     # この段階でも、まだ baz は空ディレクトリのままになっている
+ submodule の中身を最新にする
 $ git submodule update
 Cloning into 'baz'...
 done.
 Submodule path 'baz': checked out '386b630fd974b3f3147d462cda2c1f50813131fc'
 $ ls -a baz/
 .  ..  .git  ps-ax.log    # bar.git の中身が baz に clone される



//===============================================================================
** git diff : 差分を表示する [#git-diff]
- git diff を実行すると diff/patch 風の差分を表示するが、実はこのままでは余計な付加文字列があるために patch コマンドで使用することは出来ない。~
patch で使えるフォーマットで差分を出力したい場合には
 $ git diff --no-prefix
とする必要がある。

//===============================================================================
** git config : 各種設定 [#git-config]
- 設定を記録するファイルは、下記の3つがある。
-- ${HOME}/.gitconfig --- ユーザのグローバルな設定
-- ${REPOSITORY_DIR}/.git/config --- git リポジトリ ${REPOSITORY_DIR} 内限定で有効な設定
-- ${prefix}/etc/gitconfig --- システムワイドな設定

//-------------------------------------------------------------------------------
*** ユーザアカウントに関する設定 [#git-config-user]
- ユーザアカウント、メールアドレスを登録出来る。これらは commit log で commit したユーザの情報として使用される。
- global で設定しても、 local で設定しても良い(が、通常は global になるだろう)。
 $ git config --global user.name "UserName"
 $ git config --global user.email "account@example.com"


//-------------------------------------------------------------------------------
*** alias の設定 [#ld74f497]
*** alias の設定 [#git-config-alias]
- Subversion における "svn commit" に対する "svn ci" のようなエイリアスは git にはデフォルトでは存在しないが、しかし自分で好きなように設定することが出来る。
 $ git config --global alias.ci "commit"   # "git commit" を "git ci" でも実行できるようにする
 $ git config --global alias.co "checkout"
 $ git config --global alias.st "status"

//-------------------------------------------------------------------------------
*** 設定をエディタで編集する [#hde2f8b0]
 $ git config <--local|--global|--system> <-e|--edit>

//-------------------------------------------------------------------------------
*** 現在の設定を確認する [#vc27d4b7]
 $ git config <-l|--list>

//-------------------------------------------------------------------------------
*** 設定を削除する [#delete-config]
- 指定した <key> の設定を削除する
 $ git config [--local|global|--system] --unset <key>
- 全ての設定を削除する
 $ git config [--local|global|--system] --unset-all


/////////////////////////////////////////////////////////////////////////////////
* [[Subversion>VersionCtl/Subversion]] とのコマンド対比 [#q65add11]
|~Git |~Subversion |~Description |
|git init |svnadmin create |リポジトリ作成 |
|git clone |svn checkout |リポジトリのコピー |
|git add <file> |svn add <file> |ファイルの追加 |
|git add <file>; git commit |svn commit [file] |ファイルの更新をリポジトリに反映 |
|git rm <file> |svn rm <file> |ファイルの削除(別途、要 commit) |
|git status |svn status | |
|git log [file] |svn log [file] |ログを表示する |
|git log --name-status | svn log -v |追加・変更されたファイル名を含めたログを表示する |
|git blame <file> |svn blame <file> |<file> の各行がいつ誰によって変更されたかを表示する |
|git checkout <branch> |svn switch <branch> | |
|git merge <branch> |svn merge <branch> | |
|git checkout <file> |svn revert <file> |ファイルに対して行った(リポジトリに未反映の)変更を元に戻す |
|git clean -f |svn revert [dir] |~|
|git pull <remote> <local> | svn up[date] |<remote> の最新状態を <local> に merge する |


/////////////////////////////////////////////////////////////////////////////////
* tools [#tools]
//===============================================================================
** git-sh [#y50d6b1c]
- git コマンドを打つときに "git" と入力するのを省略できるようになる
- 現在のリポジトリと branch の名前がプロンプトに表示されるため、 branch コマンドを打つ必要もなくなる。



/////////////////////////////////////////////////////////////////////////////////
* tools on emacsen[#tools]
//===============================================================================
** magit [#magit]
- リポジトリ: https://github.com/magit/magit.git
- debian の場合、 magit パッケージを install すると、即使えるようになる。
- psvn.el のように、 "e" (※注: "E" ではない)で差分を ediff で閲覧・ merge 出来る

//===============================================================================
** git-gutter+ [#git-gutter-plus]
- リポジトリ: https://github.com/nonsequitur/git-gutter-plus.git
- [[git-gutter>#git-gutter]] よりも高速らしい

//===============================================================================
** git-gutter [#git-gutter]
- [[git-gutter.el>http://emacs-jp.github.io/packages/vcs/git-gutter.html]]


/////////////////////////////////////////////////////////////////////////////////
* GUI tools [#gui_tools]
//===============================================================================
** gitk [#gitk]
- 起動
 $ gitk
- Tk で書かれている
- committer, commit 日時、 log, diff などを1画面で表示する UI になっており、個々の commit の内容を追いかける場合には非常に見やすい
- 閲覧にはいいが、commit の機能はない


//===============================================================================
** git-gui [#git-gui]
- 起動
 $ git gui
- git の公式な GUI である模様
- X 版は Tk で書かれている
- Windows 版もある
- merge, branch 操作、 commit など、ある程度の操作が出来る。


トップ   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS