Skip to content

Instantly share code, notes, and snippets.

@s4na
Last active June 12, 2019 01:54
Show Gist options
  • Save s4na/5b4be57eec7b71d7480de49cdd6561a4 to your computer and use it in GitHub Desktop.
Save s4na/5b4be57eec7b71d7480de49cdd6561a4 to your computer and use it in GitHub Desktop.

Gitまとめ

repository

  • リポジトリ作成
    • $ git init
  • status
    • ワークツリーとインデックスの確認
      • addしたファイルや、競合したファイルなどを確認できる
    • $ git status
  • add
    • $ git add <ファイル名>
      • ファイルをインデックスする
  • log
    • リポジトリの変更履歴の確認
    • 使い方
      • $ git log
      • グラフとして表示
        • $ git log --graph --oneline
      • 全ブランチの状態を表示
        • --all
        • $ git log --graph --all --oneline
  • リモートリポジトリの登録
    • remoteコマンド
      • $ git remote add <name> <url>

origin, master

  • origin
    • リポジトリの場所(URL)の別名
  • master
    • ブランチ名
    • デフォルトのマスター

commit

  • $ git commit -m "<コメント>"

stash

  • コミット前のファイルが残った状態で、他のブランチにチェックアウトしたい時、コミット前のファイルを退避させることができる場所

HEAD

  • commitの位置
  • 現在位置
    • HEAD
  • 親(1個前)
    • HEAD~1 or HEAD^
  • 親の親(2個前)
    • HEAD~2 or HEAD~1^1
  • 親の派生
    • HEAD~1^2
  • 2個前
    • HEAD~~

コミットの削除

  • reset
    • 不要なコミットを捨てられる
    • 使い方
      • $ git reset --<mode> <head_name>
    • mode
      • soft
        • index change: No
        • work tree change: No
        • use case
          • 変更したインデックスを元に戻す
      • mixed
        • index change: Yes
        • work tree change: No
        • use case
          • 最近のコミットを完全になかったことにする
      • hard
        • index change: Yes
        • work tree change: Yes
        • use case
          • コミットだけを無かったことにする
  • revert
    • コミットのログを残したまま、コミットを打ち消せる
    • 使い方
      • $ git revert <branch_name>
  • cherry-pick
    • コミットの抜き取り
    • 別のブランチからコミットをコピーして、現在のブランチに組み込める
  • rebase
    • -i
      • コミットの履歴書き換え
      • rebase前のコミットはORIG_HEADという名前で残ってる。
        • rebase後で戻した楽なったら、$ gir reset --hard ORIG_HEADで戻れる
      • use case
        • push前にコミットコメントを綺麗にする
        • 意味的に同じ内容のコミットを1つにまとめる
        • コミット漏れしたファイルを後から追加
        • HEADからHEAD~~までのコミットを1つにまとめる
          • $ git rebase -i HEAD~~
          • コメント修正
          • $ git add <change_texts>
          • $ git commit --amend
          • コメント修正
          • $ git rebase --continue

コミットの書き換え

push, pull, clone

  • Push
    • 「ローカルリポジトリ → リモートリポジトリ」に変更履歴を共有
    • $ git push <repository> <refspec>...
    • -u
      • 次回以降リポジトリ名が省略できる
  • Pull
    • 「リモートリポジトリ → ローカルリポジトリ」に変更履歴を共有
    • $ git pull <repository> <refspec>...
    • pull --rebase
  • Clone
    • リポジトリの複製

merge

  • リポジトリ内の更新情報でconflictした時に差分調整を行う作業のこと
  • 使い方
    • $ git merge <branch_name>
    • 競合が発生した時
      • $ git statusで状態確認
      • ファイル修正、addしてcommitしてmerge
    • 取り消し
      • 1つ前のマージを取り消す
        • $ git reset --hard HEAD~
      • 競合箇所が発生したら
        • 競合箇所修正
        • $ git add <競合箇所>
        • $ git rebase --continue
    • ブランチ上のコミットを1つにまとめてマージ
      • $ git merge --squash <branch_name>
  • 基本2種類
    • merge
      • 別ブランチで修正箇所の競合が起きた時に、1つにまとめること
      • メリット
        • 変更内容の履歴がそのまま残る
      • デメリット
        • 履歴が複雑になる
      • non fast-foward
        • 複数ブランチの情報がそのまま残った状態でのmerge
        • 正直まだよくわからない
    • rebase
      • 複数ブランチで同一箇所を修正して競合が起きた際、ブランチ毎マージする
      • メリット
        • 履歴が1本化されて単純になる
      • デメリット
        • 元のコミットの変更内容が変更される。元のコミットが動かなくなることがある
  • GitHub Web上で行える3種類の方法
    • Create a merge commit
      • $ git merge --no-ffのような動作
      • メリット
        • 何をマージしたか記録が残る
        • マージ前のブランチがそのまま残るので変更を細かく終える
        • ブランチBが消えても、ブランチAに履歴が残る
      • デメリット
        • ログが複雑になる
      • $ git rebaseで戻る時に注意が必要
    • Rebase and merge
      • メリット
        • 履歴が一本化される
      • デメリット
        • mergeできてもrebaseできないため、実行できないことがある
    • Squash and merge
      • $ git merge --squashと同じ動作
      • メリット
        • ブランチBの作業がひとまとめにされて、ブランチAにコミットされる
      • デメリット
        • まとめる前のコミットに戻れない

branch

  • 使い方
    • 作成方法
      • $ git branch <ブランチ名>
    • 見方
      • $ git branch
    • 切り替え
      • $ git checkout <ブランチ名>
    • 作成+切り替え
      • $ git checkout -b <ブランチ名>
    • merge
      • $ git merge <commit or branch_name>
    • 削除
      • $ git branch -d <branch_name>

fetch

  • リモートリポジトリの最新の履歴の取得だけを行う
  • 使用タイミング
    • pullと違い、リモートリポジトリの内容のマージを行って欲しくない時

tag

  • 軽量タグ
    • 名前を付けられる
  • 注釈付きタグ
    • 名前・コメント・署名を付けられる
  • 使い方
    • 作成方法
      • 軽量タグ
        • $ git tag <tagname>
      • 注釈付きタグ
        • $ git tag -a <tagname>
        • $ git tag -am "<comment> <tagname>"
    • 確認の仕方
      • 簡単に確認
        • $ git tag -n
      • 細かく確認
        • $ git log --decorate
    • 削除
      • $ git tag -d <tagname>

diff

  • 差分表示
    • ブランチ間の差分表示
      • $ git diff <branch_name> <branch_name>

pull request

  • Forkしない方法(リポジトリ共有式)
    • GitHubでrepository作成
    • ローカルでPR用ブランチ作成してpush
    • GitHub上でPull Request
    • GitHub上でPull Requestmerge
  • Forkする方法
    • オリジナルリポジトリを自分のアカウントにfork
    • ローカルにclone
    • clonecommit
    • forkしたrepositoryからPull Request

参考

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment