'VersionCtl/Git/' には、下位層のページがありません。
Links†
Git について†
Downloads†
使い方†
git init : 新規リポジトリ作成†
- 空リポジトリの作成
$ mkdir hoge.git # bare repository 用の空ディレクトリを作成
$ cd hoge.git
$ 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 で指定されたファイルが登録される
--shared= option†
- --shared= オプションの右辺には、true / false の他に、 UNIX permission も使用出来る。
- 同一グループ内のみで共有したい場合は、
--shared=0660
のようにする。
git checkout†
remote branch を指定して local branch を切る†
$ git checkout -b <local-branch-name> <remote-repository>/<remote-branch-name>
- <remote-repository>/<remote-branch-name> を、<local-branch-name> という名前の local branch として切る(checkout する)。
branch 作成†
- 例として新しく作成する branch の名前を "work1" とする
- (例えば masterのディレクトリ内で)次の操作を行う
$ git checkout work1
branch 切り替え†
tag 指定で branch を切る†
$ git checkout -b <new-local-branch-name> refs/tags/<tag-name>
- <new-branch-name> は <tag-name> に一致してはいけない(仮に実行してもエラーになり成功しない)
git clone : git リポジトリの複製†
git branch : branch 操作†
リポジトリの branch 名を表示†
- local の banch 一覧を表示する
$ git branch
* work
master
hoge
- current branch には、左側に "*" が表示される
- ${TERM} が xterm-256color な端末の場合、current repository は緑色で表示されることが多い
- remote repository も含む branch 一覧を表示する
$ git branch -a
* master
remotes/origin/BillyDonahue-patch-1
remotes/origin/BillyDonahue-patch-2
remotes/origin/BillyDonahue-patch-3
remotes/origin/HEAD -> origin/master
remotes/origin/master
remotes/origin/rollback_808
- local branch に加え、 git remote で表示される remote に属する branch の一覧も表示する(但し remote branch は、最低でも1回は git fetch 若しくは git pull しなければ表示されないことに注意)
branch 名を変更する†
- デフォルトでは "master" になっているが、用途に合わせて任意の名前を付けることが出来る。
- 変更するリポジトリ内にて、以下のようにする:
$ git branch -m <元の名前> <新しい名前>
branch 切り替え†
不要になった branch の削除†
- 不要になった branch work1 を削除するには、
$ git branch -d work1
branch の複製†
git commit : 登録済みのファイルに変更が加えられた場合†
git commmit --amend : commit をやり直す†
git push: リポジトリの変更を他のリポジトリに反映する†
$ git push <反映先> <反映元>
branch を明示して push する†
$ git push <src-branch-name>:<dst-branch-name>
local で新規作成した branch を remote へ push する†
$ git push <remote-repository> <local-new-branch-name>
push 出来ない†
commit の取り消しをリモートに push する†
.git†
- それぞれは、以下のように使われる
name | description |
HEAD | 現在チェックアウトされているブランチ(HEAD)がどういう参照情報かが書かれている |
config | この git リポジトリの設定内容 |
description | この git リポジトリの説明が記述される。 GitWeb でリポジトリを HTTP 公開したり、メールでコミットメッセージを送信するときなどに利用される。 |
hooks/ | このディレクトリ配下にフックスクリプトを配置する |
info/ | この git リポジトリで無視したいファイルを info/exclude に記述する |
objects/ | この git リポジトリで管理する git オブジェクトの実体がここに配置される |
refs/ | ブランチやタグが指す git オブジェクトがどれかという参照情報がここに置かれる |
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-ID]
- [commit-ID] を取り消す
- [commit-ID] を指定しなかった場合、HEAD が指定されたとみなされる
- [commit-ID] には以下が使用出来る:
- HEAD: 最新
- <commi-ID>^ or <commit-ID>~1 : <commit-ID> の1個前
- <commi-ID>^^ or <commit-ID>~2 : <commit-ID> の2個前
- 但し、remote へ push 済みの commit に対して reset を行なっても、 reset を remote には push 出来ないようなので注意が必要。
- --hard を使用すると、作業中のリポジトリに状態が反映される
- --soft (もしくは指定ナシ)の場合、git のステータスが変わるだけになる。
commit を取り消すには†
git merge : 他の branch に commit を反映する†
$ git merge <反映先 branch>
- options
option | description | remark |
--no-ff | Fast-forward merge を強制的に抑止し、merge commit が必ず発生するようにする。 | |
--squash <branch> | <branch> に commit されている複数のcommit を1つに纏める。 | merge commit は自動生成されないので、 git commit を手動で行う必要がある。 merge に際して committer や log を改変する必要がある場合にも使える |
git cherry-pick†
- git merge は branch 間差分を全て適用するのに対し、 git cherry-pick は「特定の commit のみを他の branch に適用」する
- git cherry-pick で特定 commit を取り込む場合、取り込んだ commit は、取り込み元の commit とは異なる commit ID が振られる。
つまり、 branch A の commit aa を branch B へ cherry-pick すると、 branch B 上の commit ID は aaaa ではなく別の ID (仮に aabb とする)となる。
そのため、この cherry-pick をした後で branch A を branch B に git merge しようとすると、異なる commit ID (=別々の修正)で同一箇所を変更したと認識されるため、conflict が発生する。
git cherry-pick -x <commit-ID>†
- -x オプションは、cheery-pick 先のログに、cherry-pick 元の commit ID をログに自動で付加する。
git cherry-pick -n -m1†
- git stash drop で消してしまった stash を復元する
- 使い方
- git stash drop したときに出てくる hash を控えておく(下記の()の中)
$ git stash drop
Dropped refs/stash@{0} (60f116422281cfa289409787bfab79cdedd789a8)
- 上記の stash を復元する
$ git cherry-pick -n -m1 60f116422281cfa289409787bfab79cdedd789a8
git tag†
tag を remote repository に push する†
tag の削除†
特定の commit を含む全ての tag を検索する†
tag の命名規則†
git clean : リポジトリで管理されていないファイルの表示・削除†
- リポジトリで管理されていないファイルの一覧表示
option | description | remark |
-n | リポジトリで管理されていないファイルの一覧を表示。引数なしの場合のデフォルトの動作 | |
-f | リポジトリで管理されていないファイルを削除。ディレクトリ、.gitignore で無視されているファイルは対象外 | |
-d | リポジトリで管理されていないファイルとディレクトリを削除。 .gitignore で無視されているファイルは対象外。 | |
-x | リポジトリで管理されていないファイルの削除。.gitignore で無視されているファイルも対象 | |
git fetch リモートリポジトリのデータを取得する†
- git fetch は、リモートリポジトリの情報を「リモートリポジトリの情報として」リポジトリに取り込む。これによって、 git remote で接続したリモートリポジトリのブランチ一覧が見えるようになる他、git merge でブランチ間マージなどが出来るようになる。
リモートリポジトリで削除されたブランチの情報を反映する†
$ git fetch --prune ( git fetch -p )
commit されていない変更点の確認†
- 全ての差分を表示
$ git diff
- 変更された管理対象のファイルの一覧を見る
$ git clean [-n]
- -n の代わりに -f を付けると、全ての変更を revert する
- 管理対象外のファイルも含め、変更ファイルの一覧を見る
$ git status
conflict への対処†
conflict を抑えるために†
- 未 push の commit が手許のリポジトリにある状態で remote から pull する場合は、 --rebase をつけて、同時に rebase されるようにする。
$ git pull --rebase
- 未 commit の修正がある状態で pull する必要がある場合は、stash で一旦隠したうえで pull し、その後 git stash pop で修正を復活させる。(この時に conflict が起こる場合もあるが)
git pull --rebase した時の conflict への対処†
$ git pull --rebase
- conflict が発生したら、conflict を編集する。このとき、 branch は元いたところから Detach される。
- Eitor で編集し、完了した場合は、そのファイルを逐次 git add する。←これにより git はそのファイルの conflict 編集が完了したことを認識する。
- 全ての conflict を解消し、 git add したら、以下を実行する:
$ git rebase --continue
- 但し、途中でやめたくなった場合は代わりに下記を実行すれば、それまでの作業がなかったことになる。
$ git rebase --abort
- git rebase -continue を実行した結果、まだ他に merge すべき競合がある場合は更に conflict が発生する。これを逐次編集していく。
- git rebase -continue を実行した結果、conflict が全て解消すると、変更差分が元の commit に amend され、同時に branch は Detach 状態から元いた branch に戻る。
git pull や git merge 時に発生した 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 の編集
- git mergetool を実行すると merge 用のプログラムが呼び出される。
- emacs + magit を使用している場合は、magit の画面から通常の手順で ediff を呼び出す
- conflict 解決後の処理
- conflict 編集後、add, commit, push を行う。
$ 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.
...
ファイル単位で現在の branch の内容にするか、merge する branch の内容にするのかを決める†
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 log --no-merges : branch 間の差分を commit 単位で表示する†
- 書式
$ git log --no-merges <branch-src>..<branch-dst>
- 出力例
$ git log --no-merges origin/master..origin/pu
commit 2f834a434d38765d43d2fe6b7a26eb9e89db48ef
Author: Noam Postavsky <hoge@users.sourceforge.net>
Date: Fri Jun 10 23:16:44 2016 -0400
magit-get{,-all,-boolean}: improve cache
If magit--refresh-cache is activated, cache all config values at once.
Add tests for known tricky cases.
commit 5fac7343fb0c4b396ace43debbe479942c07debe
Author: Noam Postavsky <hoge@users.sourceforge.net>
Date: Fri Aug 26 18:15:05 2016 -0400
magit-branch-spinoff: use selected commit as base
commit 43a129326412272e52d5e8529308a2601f08ba7d
Author: Jonas Bernoulli <bar@bernoul.li>
Date: Thu Aug 25 19:13:05 2016 +0200
magit-slow-confirm: new option
TODO manual
TODO relnotes
git log --pretty=<format> : 指定したフォーマットでログを出力する†
- email : email の形式で出力する
$ git log --pretty=email
From 0e818e9dfd053eed6da8a40bbf6a65f2fa4b2ed8 Mon Sep 17 00:00:00 2001
From: Hoge Fuga <hoge@fuga.com>
Date: Mon, 3 Sep 2018 19:39:36 -0400
Subject: [PATCH] magit-list-submodules: Handle buffer creation
As of 2113dfc3 (magit-create-buffer-hook: New hook, 2018-08-26),
magit-mode-get-buffer callers are responsible for generating a new
buffer if needed.
Fixes #3575.
色々組み合わせてみる†
git reflog : リポジトリ上での git 作業履歴を表示する†
消してしまった commit 履歴を元に戻す†
- git reset --hard HEAD^^ のような操作を行うと、commit 履歴がなくなる。それを元に戻したい場合、以下の2つの手段が考えられる。
- remote から pull し直す(remote に push されている変更である場合)
- reflog から復旧する(但し、それまでの間に git gc を実行していないことが条件となる)
- reflog からの復旧は、local にしか存在しなかった commit に関しても復旧できる。
- 以下、commit を消してから reflog による復旧までの手順を示す:
- 現在の最新状況
$ git log
commit 47aea4296981269c1f3e4a779602ec0f649cfe5f
Author: hoge <hoge@localhost>
Date: Sat Dec 24 00:28:19 2016 +0900
Modify email page
- input type of email-form (input type="email").
- visual in confirm page.
commit fe4cdb4c96822ab2ea5418b0627795b5bd6f83ce
Author: hoge <hoge@localhost>
Date: Fri Dec 23 15:54:32 2016 +0900
Modify comment.
commit be940a58ff8b33e33e8211b390f4538a06b1803c
Author: hoge <hoge@localhost>
Date: Fri Dec 23 15:51:12 2016 +0900
Modify Design, remove unnesessary CSS definition.
commit 85af578464c613ade32420a25c267dd2f77a31c4
Author: hoge <hoge@localhost>
Date: Thu Dec 22 01:08:21 2016 +0900
Modify design.
- git reset で履歴を消す。
$ git reset --hard HEAD^^
$ git log
commit be940a58ff8b33e33e8211b390f4538a06b1803c
Author: hoge <hoge@localhost>
Date: Fri Dec 23 15:51:12 2016 +0900
Modify Design, remove unnesessary CSS definition.
commit 85af578464c613ade32420a25c267dd2f77a31c4
Author: hoge <hoge@localhost>
Date: Thu Dec 22 01:08:21 2016 +0900
Modify design.
- やはり消した履歴を復旧したくなったので、reflog から戻すことにする。そこで reflog を確認
$ git reflog
be940a5 HEAD@{0}: reset: moving to HEAD^^
47aea42 HEAD@{1}: reset: moving to 47aea42
dc324eb HEAD@{2}: checkout: moving from 057c6eb522986eb442cfcb5a968c8467271af319 to dc324eb8481e1b0f9dd3f1ffc1b19177653bec4b
057c6eb HEAD@{3}: checkout: moving from 49e4a499eb67f0eac4898241c52a6b7287d35d86 to 057c6eb522986eb442cfcb5a968c8467271af319
49e4a49 HEAD@{4}: checkout: moving from ba052fa7aaf00cd0186cfb6b367d909f7f0f773b to 49e4a499eb67f0eac4898241c52a6b7287d35d86
ba052fa HEAD@{5}: checkout: moving from 85af578464c613ade32420a25c267dd2f77a31c4 to ba052fa7aaf00cd0186cfb6b367d909f7f0f773b
85af578 HEAD@{6}: checkout: moving from master to 85af578464c613ade32420a25c267dd2f77a31c4
47aea42 HEAD@{7}: commit: Modify email page
fe4cdb4 HEAD@{8}: commit: Modify comment.
be940a5 HEAD@{9}: commit: Modify Design, remove unnesessary CSS definition.
85af578 HEAD@{10}: commit: Modify design.
- 元々の最新の commit を上記から特定する。ここでは下記に復旧することとする:
47aea42 HEAD@{7}: commit: Modify email page
- reflog から履歴を復旧する
$ git reset --hard 47aea42
- 結果を確認。
$ git log
commit 47aea4296981269c1f3e4a779602ec0f649cfe5f
Author: hoge <hoge@localhost>
Date: Sat Dec 24 00:28:19 2016 +0900
Modify email page
- input type of email-form (input type="email").
- visual in confirm page.
commit fe4cdb4c96822ab2ea5418b0627795b5bd6f83ce
Author: hoge <hoge@localhost>
Date: Fri Dec 23 15:54:32 2016 +0900
Modify comment.
commit be940a58ff8b33e33e8211b390f4538a06b1803c
Author: hoge <hoge@localhost>
Date: Fri Dec 23 15:51:12 2016 +0900
Modify Design, remove unnesessary CSS definition.
commit 85af578464c613ade32420a25c267dd2f77a31c4
Author: hoge <hoge@localhost>
Date: Thu Dec 22 01:08:21 2016 +0900
Modify design.
- 以上。
git reflog のその他の利用方法†
git show : 詳細を表示する†
- git show <commit-ID>
- 指定した <commit-ID> の詳細を表示する
- git show <tag>
- tag が -a や -m で付加情報付きで打たれている場合、Date, Tagger などの情報を表示する。
git show-branch†
その commit が属する branch / tag の情報を表示する [#git-あ]†
git blame†
$ git blame <file-name>
- ファイルの行単位の改変履歴を表示する。svn blame とは殆ど機能的に変わらない。
- 下記のように、hash, filename, comitter, date, line-number などの情報と共に <file-name> の内容が出力される(下記は dash.el の例)
75d29a26 dash.el (Magnar Sveen 2014-12-02 07:06:35 +0100 423) `(-iterate (lambda (it) ,form) ,init ,n))
75d29a26 dash.el (Magnar Sveen 2014-12-02 07:06:35 +0100 424)
f780322b dash.el (Matus Goljer 2014-05-03 17:22:03 +0200 425) (defun -flatten-n (num list)
c6b92ae2 dash.el (Matus Goljer 2014-06-04 18:05:25 +0200 426) "Flatten NUM levels of a nested LIST.
c6b92ae2 dash.el (Matus Goljer 2014-06-04 18:05:25 +0200 427)
c6b92ae2 dash.el (Matus Goljer 2014-06-04 18:05:25 +0200 428) See also: `-flatten'"
f780322b dash.el (Matus Goljer 2014-05-03 17:22:03 +0200 429) (-last-item (--iterate (--mapcat (-list it) it) list (1+ num))))
f780322b dash.el (Matus Goljer 2014-05-03 17:22:03 +0200 430)
f1c84533 dash.el (Magnar Sveen 2012-10-22 15:42:15 +0200 431) (defun -concat (&rest lists)
c6b92ae2 dash.el (Matus Goljer 2014-06-04 18:05:25 +0200 432) "Return a new list with the concatenation of the elements in the supplied LISTS."
38028904 bang.el (Magnar Sveen 2012-10-22 15:26:40 +0200 433) (apply 'append lists))
^302c8ea bang.el (Magnar Sveen 2012-09-24 21:02:58 +0200 434)
25c114c5 dash.el (Wilfred Hughes 2014-06-26 23:44:05 +0100 435) (defalias '-copy 'copy-sequence
25c114c5 dash.el (Wilfred Hughes 2014-06-26 23:44:05 +0100 436) "Create a shallow copy of LIST.")
25c114c5 dash.el (Wilfred Hughes 2014-06-26 23:44:05 +0100 437)
174747ea dash.el (Matus Goljer 2014-06-04 17:26:41 +0200 438) (defun -splice (pred fun list)
174747ea dash.el (Matus Goljer 2014-06-04 17:26:41 +0200 439) "Splice lists generated by FUN in place of elements matching PRED in LIST.
174747ea dash.el (Matus Goljer 2014-06-04 17:26:41 +0200 440)
git stash : ローカルの変更内容を隠蔽する†
- ローカルに変更がある状態では、 git pull, git push どちらも操作不可能になることがある。こうした場合に、一旦ローカルの変更内容を隠蔽した上で git pull し、競合などを解消させてからローカルの変更を push すれば良い。
git stash : ローカルの変更を隠蔽する†
$ git stash
- オプション
option | description |
-u | Untracked file も含めた変更差分を退避保存する(-uがない場合、退避保存されるのは tracked file のみとなる) |
save <msg> | stash のコメントに <msg> を指定する |
git stash list : 隠蔽したローカルの変更一覧を表示する†
$ git stash list
git stash pop : stash の最新を1つ適用し、stash のリストから削除する†
$ git stash pop
git stash apply : stash を適用する†
$ git stash apply
git stash drop : stash のリストからエントリを削除する†
$ git stash drop
git rebase†
- branch の分岐点を変更する、履歴の順番を入れ替える
- options
分岐元のコミットを変更†
- branch_X から branch_Y が分岐した点を A から branch_X の最新に変更する場合
$ git rebase branch_X branch_Y
== A0 ==== A1 ==== A2 ===== A3 ==== A4 ===== --- branch_X
|
+== B1 == B2 == B3 == B4 === --- branch_Y
↓
== A0 ==== A1 ==== A2 ===== A3 ==== A4 ===== --- branch_X
|
+== B1 == B2 == B3 == B4 = --- branch_Y
分岐元の branch を変更する†
- branch_Z の分岐元を、元々の分岐元 branch_Y から branch_X に変更する
→ branch_Z を、branch_Y の内容を含まない、branch_X から直接分岐した branch に変更する
$ git rebase --onto branch_X branch_Y branch_Z
== A0 ==== A1 ==== A2 ===== A3 ==== A4 ===== --- branch_X
|
+== B1 == B2 == B3 == B4 === --- branch_Y
|
+=== C1 ==== C2==== --- branch_Z
↓
== A0 ==== A1 ==== A2 ===== A3 ==== A4 ===== --- branch_X
|
+== B1 == B2 == B3 == B4 === --- branch_Y
|
+=== C1 ==== C2==== --- branch_Z
rebase の再開・スキップ・中止†
$ git rebase --continue | --skip | --abort
option | description |
--continue | 競合解決中に、 rebase を再開したい場合に指定する |
--skip | 競合解決中に、 その commit を skip したいときに指定する。skip した commit は適用されない |
--abort | 競合解決中に、rebase を中止し、 rebase 開始前の状態に戻りたい場合に指定する |
git rebase -i 履歴の順番を入れ替える†
- このコマンドを実行すると、まずテキストエディタによる履歴編集画面が表示される。
pick 028c868 [Process] Fix Unit build error.
pick d4dbaa3 Modify for fix build error (In progress)
pick 9581c48 add static
# Rebase 60802d1..9581c48 onto 60802d1
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out
ここで、
- "pick" で始まる行が、各 commit のエントリになる
- "pick" で始まる行は、上から下へ行くに従い、新しい履歴という扱いになっている。これらの行を入れ替えることにより、履歴順を操作することになる。
- これを編集し終了すると、実際の履歴入れ替え処理が始まる。
- 必要に応じて conflict の解消、git rebase --continue (途中で止める場合は --continue ではなく --abort を指定する)を実行する。
git remote : リモートリポジトリ操作†
git remote add : リモートリポジトリを追加する†
- git remote add <remote-repos-name> ; git fetch <remote-repos-name> を実行すると、その後は git branch -a で <remote-repos-name> にある branch が表示されるようになる。
- <remote-repos-name> は、URL や path を指定する代わりの alias のようなもの。
- 識別出来ればどんな名前でもよい。
- 実際の <remote-repos-name> で指された repository 自体は何ら変更されるわけではない。
- <url> の書式は、実際には ssh:// http:// file:// の所謂 URI 形式の他、絶対パスでも構わない。
git remote -v : リモートリポジトリ一覧を表示する†
git remote rm : リモートリポジトリを削除する†
git remote prune†
git remote rename : リモートリポジトリに付けた名前を変更する†
- 書式
$ git remote <old> <new>
- 設定済みの remote repository の名前を <old> から <new> に変更する。
git remote set-url : リモートリポジトリの URL を変更する†
git bundle : 直接通信できないリポジトリと同期を取る†
- バンドルファイルを使った clone, fetch, pull
- clone
- clone する
$ ls
hoge.bundle
$ git clone hoge bundle hoge
Cloning into 'hoge'...
Receiving objects: 100% (1616/1616), 15.62 MiB | 0 bytes/s, done.
Resolving deltas: 100% (629/629), done.
Checking connectivity... done.
warning: remote HEAD refers to nonexistent ref, unable to checkout.
- ここで clone した中身を見ると、.git しか存在しない
$ cd hoge
$ls -a
. .. .git
- branch を checkout して、初めて管理下のファイルが姿を現す
$ git checkout master
Branch master set up to track remote branch master from origin.
Already on 'master'
$ ls
archive chat index.shtml menu.html pukiwiki-plugins wiki
bbs img linux.css menu.shtml test
git pull†
- git pull は、"git fetch; git merge" を実行するのと同じである。
git pull --rebase†
branch を指定した pull†
--all†
- 登録している全ての repository から変更を取得する。
- merge を行うのは、現在の branch に対してのみ。
--no-commit†
- merge 後に自動で commit を行わない。
--no-ff†
- Fast-Forward commit として解決出来る場合でも、強制的に merge commit を発生させる。
- merge commit を発生させることで、merge の記録を残したい場合に使用する。
fetch と pull†
- fetch と pull は、いずれも remote repository の変更を取得する
- 変更取得後、fetch はマージを行わず、 pull はマージを行う(pull = fetch + merge)
- 機械的な merge により競合が意図しない解決をされてしまうのを避けるには、pull の使用を避け、fetch と merge を別個に行うのが好ましい。
git notes : commit に注釈を付ける†
- commit に対し、commit log とは別に注釈を追加することが出来る。
- commit log に書くほどではない注釈、レビューコメントなどの追加に使用出来る。
- いずれのコマンドも、 <commmit ID> を省略した場合は、HEAD の注釈についての操作となる。
git notes add : 注釈がまだ付けられていない commit に対して注釈を追加†
git notes : リポジトリに保存されている全ての注釈の hash 一覧を表示†
git notes edit : commit の注釈をエディタで編集†
git notes remove : commit に付けられた注釈を削除†
git notes append : 注釈を追記する†
notes の同期†
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 lfs : 大容量ファイルを扱う†
使い方†
- 基本的に、通常の git と同じ引数を取る
- clone する
$ git lfs clone <clone元>
- トラックされているファイルを確認する
$ git lfs ls-files
参考リンク†
git config : 各種設定†
- 設定を記録するファイルは、下記の3つがある。
- ${HOME}/.gitconfig --- ユーザのグローバルな設定
- ${REPOSITORY_DIR}/.git/config --- git リポジトリ ${REPOSITORY_DIR} 内限定で有効な設定
- ${prefix}/etc/gitconfig --- システムワイドな設定
ユーザアカウントに関する設定†
alias の設定†
defalut の設定を無効にする†
設定項目†
configuration item | description | remark |
user.name | ユーザ名を指定する | |
user.email | ユーザのメールアドレスを指定する | |
receive.denyNonFastFowards | true に設定すると、 push 時に repository 上の commit が含まれない commit を送信しようとしたときに branch の更新を拒否する | shared repository で設定する |
receive.denyDeletes | true に設定すると、ブランチの削除を拒否する | shared repository で設定する |
receive.fsckObjects | true にすると、push されたときにデータ一貫性のチェックを行う。信頼性が向上するが、 push 処理に時間が掛かるようになる。 | shared repository で設定する |
core.pager | pager を指定する | 例えばless, lv など |
alias.<alias-name> | 任意の git コマンド(commit, checkoutなど)を <alias-name> でも呼び出せるようにする | |
fetch.prune [#git-config-prune] | true にすると、リモートリポジトリで削除されたブランチ情報が fetch 時に反映される | |
設定をエディタで編集する†
$ git config <--local|--global|--system> <-e|--edit>
現在の設定を確認する†
$ git config <-l|--list>
メール送信の設定†
- git config で、メール送信の設定が出来る。
config | description |
hooks.mailinglist | push された時にコミットメッセージを送信する ML |
hooks.announcelist | git tag <tag-name> -m <message> でタグを生成した時に通知される ML |
hooks.emailprefix | メールの Subject: の先頭につける文字列。 デフォルトは [SCM] |
hooks.envelopesender | メールの From: に記載される送信者アドレス |
hooks.showrev | メールに添付するメッセージを生成するコマンド。デフォルトでは git revlist --pretty --stdin $revspec 差分情報を送るには、 git config hooks.showrev "git show -C \$t; scho" とする |
設定を削除する†
git gc : リポジトリを最適化する†
option | description |
--aggressive | 最適化の度合いを大きくする。 処理時間がかかるが、その分最適化された状態にする。 |
--prune=<date> | <date> で指定されたものより古いオブジェクトを破棄する。 右辺に何も指定しなかった場合のデフォルト値は "2 weeks ago" となる。 また、右辺に "now" を指定すると、全ての不要なオブジェクトが削除される。 |
.gitignore : git が無視するファイルを指定する†
git fsck : git リポジトリを検査する†
- fsck はリポジトリの状態の検査のみ行い、修復・更新はしない。
option | description |
--no-full | .git/objects 配下のみを検査する |
--no-dangling | gangling object を表示しない |
- dangling とは
- どのコミットからも参照されないオブジェクト
- リポジトリが更新されるにつれて増えていく傾向にある
"missing blob <HSA1>" のようなエラーが出た場合の対処†
- git fsck で、下記のようなエラーが出る場合がある。
Checking object directories: 100% (256/256), done.
Checking objects: 100% (534/534), done.
missing blob a596d0c47dc993e0b22f533930fa73422e776b53
- この例では、a596d0c47dc993e0b22f533930fa73422e776b53 の blob(ファイル)が失われていることを報告している。
この場合、他のリポジトリに該当する blob があるなら、それを持ってきて、下記の名前で保存すると復旧出来る:
.git/objects/a5/96d0c47dc993e0b22f533930fa73422e776b53
dangling object を削除する†
- git fsck をした際に dangling object (宙ぶらりんのオブジェクト)が出てくることがある。
$ git fsck
Checking object directories: 100% (256/256), done.
Checking objects: 100% (534/534), done.
dangling commit 2a7217d80a46bf28a2e5d1dfb1eb762f40aaf7b4
dangling blob f50d7df0cfb42bd5d8aaf85b94539088ee3accee
dangling commit 2296d0c5adc7f360b00f538580fa73422e866a3a
dangling tag 385f6da23daae6d9d7f00371e89b4c353412afb1
- dangling object を削除するには、下記を実行する:
$ git reflog expire --expire=now --all
$ git gc --prune=now
リポジトリからファイルを完全削除する†
- git リポジトリで git rm すると、最新からは削除されるが履歴としては残っていて、履歴を辿れば復元することが出来る。しかし物によっては履歴にも残したくないものがある。
そういったものを消去する方法
- まず、 git filter-branch を実行
$ git filter-branch --tree-filter 'git rm --ignore-unmatch <file-to-be-deleted>' --prune-empty -- --all
- reflog にも残っているので、これも削除する
$ git reflog expire --expire=now --all
$ git gc --prune=now
$ git gc --aggressive --prune=now
エラーになる場合の対処†
Fast-forward†
- Fast-fowrward merge
- topic branch が master から分岐してから再び master にマージされる際、master に変更がなければ、マージコミットは発生せず、master が指す場所を付け替えるだけの動作をする。これを git では Fast-forward merge という。
- Fast-forward merge が行われると、「topic → master」のマージ情報が commit log に発生しないため、それがいつ行われたのかが log 上では分からなくなる。
明確にその情報を残したい場合は、 git merge に --no-ff オプションをつけることにより、強制的にマージコミットのログを発生させることが出来る。
<refspec>†
- git の説明によく出てくる <refspec> とは、pull や push で指定する
<src>:<dst>
の書式による、ブランチ指定のこと。
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 --shortstat --name-status | svn log -v | 追加・変更されたファイル名を含めたログを表示する |
git log -<number> | svn log -l <number> | 最新から <number> 個のログエントリを表示する |
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†
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 など、ある程度の操作が出来る。
Subversion (svn) からの移行†
- git-svn をインストールする
- debian などでは、git とは別パッケージになっている
- 最終的な移行先の git の bare リポジトリを作成しておく(ここでは仮に repos.git というディレクトリ名にした)。
$ git init --bare --shared=true repos.git
- 移行元の svn リポジトリに commit しているユーザのリストを下記の書式で authors.txt に書き出す(このあたりは、svn の log がユーザ名のみを記録しているのに対し、 git ではユーザ名とメールアドレスを記録することにもよる)
$ vi authors.txt
hoge = hoge <hoge@your-domain.com>
fuga = fuga <foo@example.com>
- svn リポジトリを clone する(作業リポジトリが出来る)
$ git svn clone -A authors.txt --trunk= --branches=branches --prefix=svn/ ${SVN_REPOS_URL}
- 上記で出来たリポジトリの中に入り、最終的な移行先リポジトリ(はじめの方に作成した repos.git)に push する
$ git remote add origin ../repos.git
$ git push origin --all