Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2012
2,981
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.43 KB | None | 0 0
  1. Git
  2.  
  3. 基本(とりあえずこれ覚えればOK)
  4. -----------------------------
  5.  
  6. $ git init hoge # git リポジトリを作成。または...
  7. $ git clone hoge@example.com:myrepo.git # 外部から取得
  8. ... some change
  9. $ git add *
  10. $ git commit -m "my commit"
  11. $ git status # 状態を見る
  12. $ git push origin master # remoteに送信
  13. $ git pull
  14.  
  15. ログ
  16. -----
  17.  
  18. http://progit.org/book/ja/ch2-3.html
  19.  
  20. $ git log -p # diffも出力する
  21. $ git log --stat # 変更したファイルの一覧
  22. $ git log --pretty=oneline #1行で
  23. $ git log --pretty=format:"%h - %an, %ar : %s" # 独自のフォーマットで
  24. $ git log --since=2.weeks # 日付指定
  25. $ git log -(n) # n件
  26.  
  27. commit --amend や reset --hard HEAD^^ とかして見えなくなったコミットを確認する
  28.  
  29. $ git reflog
  30.  
  31.  
  32. コミット
  33. -------
  34. 直前のコミットをやり直しできる!
  35.  
  36. $ git commit --amend
  37.  
  38. 直前のコミットをなかった事にして一つ前のコミット状態に戻す
  39.  
  40. $ git reset --hard HEAD^
  41. $ git reflog # HEAD履歴を確認できる。間違えた場合はそれも戻せる
  42.  
  43. ステージングの取り消し
  44.  
  45. $ git reset HEAD hoge.txt
  46.  
  47. svn revertのようなもの
  48.  
  49. $ git checkout -- hoge.txt
  50.  
  51.  
  52. ブランチ
  53. ---------
  54. http://progit.org/book/ja/ch3-1.html
  55. HEADは「あなたが作業しているローカルブランチへのポインタ」
  56.  
  57. testingブランチを作成(切り替えはしない)
  58.  
  59. $ git branch testing
  60.  
  61. 切り替える
  62.  
  63. $ git checkout testing
  64.  
  65. 作成して切り替える
  66.  
  67. $ git checkout -b issue53
  68.  
  69. マージする
  70. 通常はFast forward ポインタを前に進めるだけ。
  71.  
  72. $ git merge issue53
  73. $ git merge --no-commit issue53 # "Merged branch ..." とコミットされるが、それをしない
  74. $ git merge --no-squash issue53 # 変更をワーキングツリーに適用する。マージもコミットもしない。ブランチ上での履歴は無くなる。
  75. $ git merge --no-ff issue53 # [ToDo]
  76.  
  77. mergeは二本に分かれた枝を一本に合わせる。
  78. rebaseはmergeとrebaseの違いは後述
  79.  
  80. $ git rebase master
  81.  
  82. あるコミットの変更分だけを適用する
  83.  
  84. $ git cherry-pick {commit}
  85.  
  86. タグ
  87. -------
  88.  
  89. タグの一覧
  90.  
  91. $ git tag
  92.  
  93. タグを作成
  94.  
  95. $ git tag v0.1
  96.  
  97. 注釈付きタグの作成(コミットするかどうかの違いらしいが、どう使い分けるのか分からん)
  98.  
  99. $ git tag -a v1.4 -m 'my version 1.4'
  100.  
  101. タグの共有
  102. $ git push origin v1.5
  103. $ git push origin --tags # 共有されてないもの全部
  104. $ git push origin :missing_tag # リモートのタグを削除
  105.  
  106. 共有
  107. ----------
  108.  
  109. http://progit.org/book/ja/ch3-5.html
  110.  
  111. 「こっちの serverfix で、リモートの awesomebranch を更新しろ」
  112.  
  113. $ git push origin serverfix:awesomebranch
  114.  
  115. $ git push origin master
  116. $ git push # 省略できる
  117.  
  118.  
  119. リモートにデータを送信
  120. 「こっちの (何もなし) で、向こうの [remotebranch] を更新しろ」
  121. ドッカーン。これでブランチはサーバーから消えてしまいました。
  122.  
  123. $ git push origin :serverfix
  124.  
  125.  
  126. リモートのデータを取得
  127.  
  128. $ git fetch origin
  129.  
  130. fetchしたものとマージ
  131.  
  132. $ git merge origin/master
  133.  
  134. origin/serverfix が指す先から作業を開始するためのローカルブランチができあがりました。
  135. $ git checkout -b serverfix origin/serverfix
  136.  
  137. 追跡ブランチ: cloneすると自動的に追跡ブランチする。これが引数なしでgit pushやgit pullが動作する理由。
  138.  
  139. これは同じ意味。
  140.  
  141. $ git checkout --track origin/serverfix
  142. $ git checkout -b serverfix origin/serverfix
  143.  
  144.  
  145. fetch とmergeを自動でやってくれる
  146.  
  147. $ git pull
  148.  
  149. リモートブランチを削除しても、別の場所してクローンされてるとずっと残る。追加は自動だけど削除は自動でしてくれない。そういう場合に prune を使う。
  150.  
  151. # 状態を確認する
  152. $ git remote show origin
  153. $ git remote prune origin
  154.  
  155.  
  156.  
  157. サブモジュール
  158. ------------
  159.  
  160. http://progit.org/book/ja/ch6-6.html
  161.  
  162. $ git submodule add git://github.com/chneukirchen/rack.git rack
  163.  
  164. submoduleのあるリポジトリをcloneした後、
  165.  
  166. $ git submodule init
  167. $ git submodule update
  168.  
  169. サブモジュールの外部の変更を取り込むには毎回これをやる
  170.  
  171. $ git merge origin/master
  172. $ git submodule update
  173.  
  174. サブモジュールはあるポイントしか見ない!!親プロジェクトはそのポイントを記録する
  175.  
  176. 問題点。特定のバージョンをポイントしている(detached head)ので、git submodule updateで手元の変更が失われやすい
  177.  
  178. .gitmodulesを見ればわかる
  179.  
  180.  
  181. git-svn
  182. ------------
  183. http://progit.org/book/ja/ch8-1.html
  184. http://d.hatena.ne.jp/idesaku/20090323/1237825080
  185.  
  186. svn checkoutのようなもの
  187. svnからgitリポジトリを作成。 -s(standard)オプションはsvnのtrunk/branches/tags構成をそのままインポートする
  188.  
  189. $ git svn clone file:///tmp/test-svn -s
  190.  
  191. svnのデータだけ取得
  192.  
  193. $ git svn fetch
  194.  
  195. svn upのようなもの
  196. svnのデータを取り込む
  197.  
  198. $ git svn rebase
  199.  
  200. svn commitのようなもの
  201. svnにコミット。gitのコミットIDが書き換わる!注意!
  202. コミットできたらgit-svn-id属性が付く。
  203.  
  204. $ git svn dcommit
  205.  
  206. git-svnで削除されたリモートブランチをローカルでも削除する方法
  207.  
  208. $ git branch -r -d my-remote-branch
  209.  
  210. svnにブランチ作成
  211.  
  212. $ git svn branch new_branch_name
  213.  
  214. svnにタグ作成
  215.  
  216. $ git svn tag new_tag_name
  217.  
  218. FAQ
  219.  
  220. - git-svn dcommit できなかったのでコミットID見たら、違うIDになってた...。
  221. - > patch を作成する
  222. - ローカルに未コミットの変更があるけどgit-svn-dcommitしたい
  223. - > stashを使う
  224.  
  225. $ git stash
  226. $ git svn dcommit
  227. $ git stash apply
  228.  
  229. - svn:externalsのようなことをしたい
  230. - > git submodule
  231. 移行スクリプト。でもうまくいかなかった…
  232. http://github.com/garbas/garbasgit.svnexternals
  233. (via http://d.hatena.ne.jp/Sixeight/20090210/1234272295 )
  234. - 空のディレクトリはバージョン管理できない
  235. - > .gitignoreとか何でもいいから空ファイルを作成すればOK。
  236.  
  237. - 複数のモジュールを含むSubversionリポジトリをGitへ移行する。http://iteman.jp/blog/2009/02/subversiongit.html
  238.  
  239. 他注意点
  240.  
  241. - 部分的なチェックアウトできない
  242. - svn checkout はパスを指定すればどの階層でもワーキングコピーを作成できる。
  243. git clone すると今までの履歴含め、全てをコピーしなければならない。
  244. なのでプロジェクト/モジュールごとにリポジトリを作成するのがベストだろう。
  245. - ディレクトリ同士のマージができない。これはどうにもできないかも。
  246. - trunk, branches, tags
  247. - Subversionはディレクトリ構造として表現
  248. - Gitはbranchesとtagsが別のシステムになっている。
  249.  
  250.  
  251. パッチ
  252. ------------
  253. http://blog.s21g.com/articles/680
  254.  
  255. 直前のコミットとその前との差分でパッチを作成する
  256.  
  257. $ git format-patch -r HEAD~
  258.  
  259. パッチを適用する
  260.  
  261. $ git am 0001-hoge.patch
  262.  
  263.  
  264. git merge と git rebaseの違い
  265. ----------------
  266.  
  267. masterのHEADはポイントAにあるとする
  268.  
  269. (master) ---A
  270.  
  271. Aからブランチしたworkingを作成し、Bまで進める
  272.  
  273. (master) ---A
  274. \
  275. (working) \----- B
  276.  
  277. そこからさらに masterでも変更し A' まで進めたとする
  278.  
  279. (master) -- A ------------ A'
  280. \
  281. (working) \----- B
  282.  
  283. この状態で、
  284.  
  285. (master) git merge working した場合
  286. A'に B がマージされて C になる。
  287. C の HEAD^(一つ前) は A'
  288.  
  289. (master) ---A ------------ A' ---- C
  290. \ /
  291. (working) \-----B------------
  292.  
  293. (master) git rebase working した場合
  294. コミットA'の存在はmasterからはなくなって、
  295. Bからの続きとして AからA'の差分がマージされて A'' になる
  296. A'' の HEAD^ は B
  297.  
  298. First, rewinding head to replay your work on top of it...
  299. Applying: commit B
  300.  
  301. (master) ---A ------ A''
  302. \ /
  303. (working) \-----B
  304.  
  305. mergeはブランチの履歴を残して一本にするけど、rebaseすると履歴も含めて一本に作り直す。そのためrebaseは注意が必要。
  306.  
  307.  
  308. その他
  309. ================
  310.  
  311. #
  312. # コンフリクト状態が残ってしまった場合
  313. #
  314. $ git pull
  315. You have not concluded your merge (MERGE_HEAD exists).
  316. Please, commit your changes before you can merge
  317.  
  318. $ git reset --merge
  319.  
  320.  
  321. #
  322. # mergeを実行したらconflictが大量に出てしまったので取り消したい
  323. #
  324. $ git reset --hard ORIG_HEAD
  325.  
  326. #
  327. # Untrackedなファイルを取り除きたい
  328. #
  329. $ git clean -f
  330.  
  331. git pullでremoteを自動的に指定
  332. ----------------
  333.  
  334. #
  335. # remoteが指定されてない(trackしてない)場合はリモート指定なしで git pull しても「わからないよ」と言われる
  336. #
  337. (master) $ git pull
  338.  
  339. You asked me to pull without telling me which branch you
  340. want to merge with, and 'branch.master.merge' in
  341. your configuration file does not tell me, either. Please
  342. specify which branch you want to use on the command line and
  343. try again (e.g. 'git pull <repository> <refspec>').
  344. See git-pull(1) for details.
  345.  
  346. If you often merge with the same branch, you may want to
  347. use something like the following in your configuration file:
  348.  
  349. [branch "master"]
  350. remote = <nickname>
  351. merge = <remote-ref>
  352.  
  353. [remote "<nickname>"]
  354. url = <url>
  355. fetch = <refspec>
  356.  
  357. See git-config(1) for details.
  358.  
  359. # 解決法1
  360. # 手動で指定
  361. #
  362. (master) $ git pull origin master
  363.  
  364. # 解決法2
  365. # configで指定
  366. #
  367. $ vi .git/config
  368.  
  369. [branch "master"]
  370. remote = origin
  371. merge = refs/heads/master
  372.  
  373. # 解決法3
  374. # これだけでOK
  375. $ git push -u origin master
  376.  
  377.  
  378. コミットの圧縮
  379. ----------------
  380.  
  381. マージするときにひとつのコミットにまとめる。
  382.  
  383. git merge --squash something-new-feature
  384.  
  385. または git rebase -i でsquash する。(後述)
  386.  
  387.  
  388. すでにコミットしたものを操作する。(git rebase -i)
  389. ----------------
  390.  
  391. # コミット2つあったとする
  392. git commit -am "hoge"
  393. git commit -am "moge"
  394.  
  395. git rebase -i head^^
  396.  
  397. するとエディターが開かれる。
  398.  
  399. pick 59d6190 hoge
  400. pick 0149644 moge
  401.  
  402. # Rebase 36fde72..0149644 onto 36fde72
  403. # ...
  404.  
  405. この行を修正して保存することで、指示通りにrebaseが行われる。
  406. コマンドはコメントに書かれてある。
  407.  
  408. * pick はそのままコミットを使う
  409. * reword は保存後にrebaseが走るので、その時にコミットメッセージを変更するエディタが走る。(注:rebase -i した中でのコメントを変更するのではない。)
  410. * edit はamendするためにストップする?よくわからん。
  411. * squash は一つ前のコミットと結合する。コミットメッセージはrebaseで選択する。
  412. * fixup は一つ前のコミットと結合する。コミットメッセージは前のが使われる。
  413. * exec はshellを走らせるらしい。
  414.  
  415. で、行を消すとそのコミットが失われる。でも全部消すとrebaseは中止になる。
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement