B-Teck!

お仕事からゲームまで幅広く

【Git】自分用チートシート

  • 設定項目やリポジトリの作成
    • configの確認
      • git config -l
        • -listと同義
    • ユーザー名とメールアドレス
      • git config --global user.name "[name]"
      • git config --global user.email "[email address]"
    • gitの改行コード自動変換をオフにする
      • git config --global core.autoCRLF false
    • エイリアスの設定(例)
      • git config --global alias.co checkout
      • git config --global alias.st status
      • git config --global alias.br branch
      • git config --global alias.ci commit
    • HEADの意味
      • HEADは現在の最新状態
    • git用のディレクトリの初期化
      • git init
    • 共有リポジトリ(bareリポジトリ)の作成時
  • リポジトリやワーキングツリーの状態確認
    • log
      • log一行表示
        • git log --oneline
      • 変更内容確認
        • git log -p
      • 変更ファイル確認
        • git log --stat
      • ツリーの分岐を表示
        • git log --graph
    • reflog
      • 今までのHEADの移動履歴を見る
        • git reflog
    • status
      • 現在の状況を確認
        • git status
      • 現在の状況を確認(簡易)
        • git status -s
        • git status --short
    • diff
      • 差分を確認する(作業フォルダ)
        • git diff
      • 差分を確認する(ステージング)
        • git diff --cached
  • 変更の保存
    • add
      • ファイルを指定してステージングに上げる
        • git add [file path]
      • 全てのファイルをステージングに上げる
        • git add .
    • commit
      • ステージングにあるものをリポジトリに反映する
        • git commit
      • エディタを開かずワンラインでメッセージ付きcommit
        • git commit -m "[message]"
      • エディタを開かずワンラインでメッセージ付きcommit(全てのファイルのaddも同時に行う)
        • git commit -am "[message]"
          • git add .
          • git commit -m "[message]"
          • と同じ意味
      • 前回のcommitを修正
        • git commit --amend
  • ワーキングツリーの状態変更や整理
    • checkout
      • 対象のファイルをHEADの状態に戻す
        • git checkout [branch name] --[file path]
      • 対象のcommitに作業ディレクトリを戻す
        • git checkout [commit id]
      • branchを切り替える
        • git checkout [branch name]
          • 作業ディレクトリの内容も選択したbranchに置き換わるので注意
      • branchを作って新しいbranchに切り替える
        • git checkout-b [branch name]
    • reset
      • ステージングをHEADの状態に戻す
        • git reset HEAD [file name]
          • 作業ディレクトリのファイルには影響しない
          • git reset --mixedの省略
      • すべて(作業ディレクトリ・ステージングエリア)を[commit id]の状態に戻す
        • git reset --hard [commit id]
      • すべて(作業ディレクトリ・ステージングエリア)を[commit id]のひとつ前の状態に戻す
        • git reset --hard [commit id]^
      • すべて(作業ディレクトリ・ステージングエリア・リポジトリの状態)を[commit id]の状態に戻す
        • git reset --hard [commit id]
      • [commit id]を取り消してHEADを[commit id]のひとつ前のcommitに戻す
        • git reset --soft [commit id]^
          • HEADの状態しか変わらない
      • 直前のHEADに戻る
        • git reset --hard ORIG_HEAD
          • 間違えてresetした時など、HEADを変更した場合にreset前のHEADの状態に戻れる
          • ORIG_HEADにはひとつ前のHEADの位置が記録されている
    • branch
      • 現在のbranchの一覧を確認する
        • git branch
      • branchを作る
        • git branch [branch name]
      • branchを削除する
        • git branch -d [branch name]
    • merge
      • branchをmerge(merge先のbranchにいる状態で)
        • git merge [branch name]
          • masterにdevelopをmergeするときはmasterにいる状態で
            • git merge develop
      • branchをmerge(ファストフォワードしない)
    • rebase
      • http://liginc.co.jp/web/tool/79390
      • http://tkengo.github.io/blog/2013/05/16/git-rebase-reference/
      • 別のコミットになる
        • rebaseは以前のコミットを取り消して、新しくコミットしなおしている
        • リモートリポジトリにコミットしている場合、巻き戻りや衝突の原因になる
      • --onto
        • $ git rebase --onto どこへ どこから どのブランチを
      • brunchの派生元を切り替える
        • git rebase [branch name]
          • 現在のブランチの参照元を[branch name]のHEADに付け替える
      • 競合を解決した後に実行して、再度rebaseする
        • git rebase --continue
      • rebaseを中止する
        • git rebase --abort
      • エラーを無視してrebaseする
        • git rebase --skip(エラーを無視する)
      • interactiveモード
        • rebase -i [commit]
          • pick(コミットを残す)
          • reword(コミットを残すが、コミットメッセージを変更)
          • edit(コミットを残すが、コミットしたファイルを修正する)
          • squash(一個前のコミットと統合してコミットメッセージを変更する)
          • fixup(一個前のコミットに統合する。コミットメッセージは統合先のものを使う)
          • exec(shellでコマンドを実行する)
    • revert
    • tag
      • 現在存在するtagの一覧を見る
        • git tag
      • 直前のcommitにtagをつける
        • git tag [tag name]
      • 特定のcommitにtagをつける
        • git tag [tag name] [commit id]
      • tagを削除する
        • git tag -d [tag name]
  • 作成中のワーキングツリーの退避
    • stash
      • 変更を一時的に退避する
        • git stash
        • git stash save
      • 変更を一時的に退避する際にメッセージをつける
        • git stash save "message"
      • 現在退避されている一覧を表示
        • git stash list
      • N番目に退避したファイルの一覧を表示
        • git stash show stash@{N}
      • N番目に退避したファイルとHEADの差分を表示
        • git stash show -p stash@{N}
      • 退避した状態を作業ディレクトリに戻し、退避リストから削除する
        • 最新を対象にする
          • git stash pop
        • 指定の番号を対象にする
          • git stash pop stash@{N}
      • 退避リストから削除せずに退避した状態を作業ディレクトリに戻す
        • 最新を対象にする
          • git stash apply
        • 指定の番号を対象にする
          • git stash apply stash@{N}
      • 退避リストから削除
        • 最新を対象にする
        • 指定の番号を対象にする
          • git stash drop stash@{N}
      • unstage ファイルを全てスタッシュ
        • git stash -k
      • untrackファイルも含めて全てスタッシュ
        • git stash -u
  • ファイルの削除や移動 名前の変更
    • rm mv
      • git rm [file path]
      • git rm --cached [file path]
        • バージョン管理からファイルを削除する
      • git mv [file path] [renamed file path]
        • ファイル名を変更してcommitする
      • git rm -r --cached
        • ステージングを綺麗にする
  • リモートへの同期や反映
    • remote
      • 共有リポジトリの場所を設定する
        • git remote add [bookmark name] [remote url]
          • [bookmark name] = originは必須の名前ではないけどよく使われる
          • git config -lでそれぞれの[remote url]がどこにあるか確認できる
      • 共有リポジトリを削除する
        • git remote rm [bookmark name]
    • clone
      • 共有リポジトリを複製する
        • git clone [remote url]
        • git clone [remote url] [local directory]
    • push
      • 共有リポジトリにpush(反映)する
        • git push [bookmark name] [branch name]
          • remoteの[bookmark name]に[branch name]branchをpushする
    • fetch
    • pull
      • fetch→mergeを自動で行う
      • 共有リポジトリからpull(取得)する
        • git pull [bookmark name] [branch name]
          • remoteの[bookmark name]から[branch name]branchを取得してmergeする
      • fetch→rebaseを自動で行う
        • git pull --rebase [bookmark name] [branch name]
          • remoteの[bookmark name]から[branch name]branchを取得してrebaseする