Links†
Git について†
Downloads†
使い方†
新規リポジトリ作成†
- 空リポジトリの作成
$ 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 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 出来ないようなので注意が必要。
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 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 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†