Links†
Git について†
Downloads†
使い方†
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 commmit --amend : commit をやり直す†
git revert : commit を取り消す†
- 「取り消す」とはいっても、 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
git reset: git commit や git add をなかったことにする†
$ 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 名を表示†
$ git branch
* work
branch 名を変更する†
- デフォルトでは "master" になっているが、用途に合わせて任意の名前を付けることが出来る。
- 変更するリポジトリ内にて、以下のようにする:
$ git branch -m <元の名前> <新しい名前>
リポジトリの変更を他のリポジトリに反映する†
$ git push <反映先> <反映元>
branch 操作†
branch 作成†
- 例として新しく作成する branch の名前を "work1" とする
- (例えば masterのディレクトリ内で)次の操作を行う
$ git checkout work1
branch 切り替え†
不要になった branch の削除†
- 不要になった branch work1 を削除するには、
$ git branch -d work1
他の branch に commit を反映する†
$ git merge <反映先 branch>
tag†
commit されていない変更点の確認†
- 全ての差分を表示
$ git diff
- 変更された管理対象のファイルの一覧を見る
$ git clean [-n]
- -n の代わりに -f を付けると、全ての変更を revert する
- 管理対象外のファイルも含め、変更ファイルの一覧を見る
$ git status
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.
git log : ログの表示(変更履歴の閲覧)†
- --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(-)
git reflog : リポジトリ上での git 作業履歴を表示する†
git show : 詳細を表示する†
- git show <commit-hash>
- 指定した <commit-hash> の詳細を表示する
- git show <tag>
- tag が -a や -m で付加情報付きで打たれている場合、Date, Tagger などの情報を表示する。
git submodule : リポジトリに他のリポジトリを組み込む†
- git のリポジトリの中に、他のリポジトリを組み込むためのもの
submodule の組み込み†
- 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 する†
- 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 を実行すると diff/patch 風の差分を表示するが、実はこのままでは余計な付加文字列があるために patch コマンドで使用することは出来ない。
patch で使えるフォーマットで差分を出力したい場合には
$ git diff --no-prefix
とする必要がある。
git config : 各種設定†
- 設定を記録するファイルは、下記の3つがある。
- ${HOME}/.gitconfig --- ユーザのグローバルな設定
- ${REPOSITORY_DIR}/.git/config --- git リポジトリ ${REPOSITORY_DIR} 内限定で有効な設定
- ${prefix}/etc/gitconfig --- システムワイドな設定
ユーザアカウントに関する設定†
alias の設定†
設定をエディタで編集する†
$ git config <--local|--global|--system> <-e|--edit>
現在の設定を確認する†
$ git config <-l|--list>
設定を削除する†
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†
git-sh†
- git コマンドを打つときに "git" と入力するのを省略できるようになる
- 現在のリポジトリと branch の名前がプロンプトに表示されるため、 branch コマンドを打つ必要もなくなる。
tools on emacsen†
magit†
git-gutter+†
git-gutter†
GUI tools†
gitk†
- 起動
$ gitk
- Tk で書かれている
- committer, commit 日時、 log, diff などを1画面で表示する UI になっており、個々の commit の内容を追いかける場合には非常に見やすい
- 閲覧にはいいが、commit の機能はない
git-gui†
- 起動
$ git gui
- git の公式な GUI である模様
- X 版は Tk で書かれている
- Windows 版もある
- merge, branch 操作、 commit など、ある程度の操作が出来る。