Skip to content

Instantly share code, notes, and snippets.

@mugyu
Last active October 9, 2015 08:57
Show Gist options
  • Save mugyu/3473233 to your computer and use it in GitHub Desktop.
Save mugyu/3473233 to your computer and use it in GitHub Desktop.
gitの個人的虎の巻

git虎の巻

カレントディレクトリを作業ツリーにする

カレントディレクトリを作業ツリーにする。 具体的には .gitディレクトリが生成される。 まったく何もない状態から始めるならここから。

  mkdir testrepo
  cd testrepo
  git init

既存のレポジトリを複製

  git clone https://foo/path/to/path.git ./workrepo

最新1世代のみ複製

  git clone --depth 1 <URL>

作業ツリーの破損チェック

  git fsck [-full]

作業ツリーの掃除(ガーベージコレクション)

  git gc

作業ツリーの状態管理

変更したファイルを表示

  git status

変更したファイルと変更前との差分を表示

  git diff

作業ツリーのあるファイルをインデックスに登録

コマンド名は "add" だけどファイルを変更した際も "add" コマンドを実行する

  git add path [path ... ]

インデックスをリポジトリに登録

-a オプションをつけると addコマンドなしに
    変更したファイルを自動的にコミット
-m オプションを付けないと登録したエディタが
   自動的に開いてメッセージ編集
  git commit [-a] [-m "commit message"]

直前のコミットに変更をまとめる

コミットしたけど変更が足りなかったとか スペルミスが見つかったとかでちょっとしたミス を修正するだけなので履歴を作りたくないって 場合に直前のコミットにまとめる

  git commit --amend

直前のコミットに別名(タグ)を付ける

  git tag タグ名

レポジトリのコミットログを見る

  git log

直前のコミットを取り消し

UNDOみたいな感じ

  git reset

他のリビジョンを編集する

resetと違ってログは残る

  git revert

リモートリポジトリのブランチをローカルリポジトリにマージする

push = fetch + merge

  git pull 取り込むリポジトリ

リモートリポジトリのブランチを作業ツリーに取り込む

マージはしない

  git fetch 取り込むリポジトリ

リモートリポジトリにローカルリポジトリを送信する

リモートにマージさせる

  git push 送信先リポジトリ 送信元ブランチ:送信先ブランチ
  git push -u origin master

インデックスに登録している作業ツリーの内容を一時保存/適用

  git stash [save ["message"]]

一時保存していた stash を削除して適用

  git stash pop [stash@{1}]

stash を削除しないで作業ツリーに適用

  git apply [stash@{1}]

一時保存の内容を表示

  git stash show [stash@{1}]

ブランチの操作

ブランチ情報の表示

  git branch

ブランチの新規作成

  git branch ブランチ名

ブランチの切り替え

  git checkout ブランチ名

新規にブランチを作ってちゃっちゃと切り替える

  git branch   ブランチ名
  git checkout ブランチ名

or

  git checkout -b ブランチ名

ブランチのマージ

分岐元と分岐先を履歴ごとマージ

あたかも分岐先ブランチが無かったかのように

  git merge マージするブランチ名

分岐先の履歴を分けて分岐元に分岐先の変更をマージ

  git merge --no-ff マージするブランチ名

派生元ブランチの最新コミットまでも取り込む

(svn の update みたいな感じだろうか) 派生元ブランチ->派生ブランチの順番

  git rebase 派生元ブランチ名

ブランチの削除

  git branch -d ブランチ名

ブランチの履歴情報を表示

  git show-branch

コマンドの別名定義

svn(RCS)風

  git config --global alias.co checkout
  git config --global alias.ci commit
  git config --global alias.st status

共有リポジトリをつくる

  mkdir common.git
  cd common.git
  git init --bare
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment