Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Gitのよく使うコマンド

 Gitのよく使うコマンド

Gitのよく使うコマンドを解説しました。

Avatar for YUKI Kaoru

YUKI Kaoru

March 29, 2013

More Decks by YUKI Kaoru

Other Decks in Programming

Transcript

  1. よく使うコマンド init add commit push clone branch checkout fetch merge

    rebase pull status rm reset remote tag log mergetool revert 19個
  2. push リモートサーバへの反映 git push <remotename> <refspec> ローカルブランチと同名のブランチをリモート上に 作成する git push

    <remote> <localbranch> 別名でpushする git push <remote> <localbranch>:<remotebranch> <refspec>は<localbranch>:<remotebranch>
  3. push リモートサーバへの反映 リモートブランチを削除する git push --delete <remote> <branchname> git push

    <remote> :<branchname> プッシュしたリモートブランチをトラッキングする git push -u <remote> <branchname> 強制的にプッシュ git push -f <remote> <branchname>
  4. branch ローカルブランチを作成/削除する ローカルブランチの作成 git branch <branchname> ローカルブランチの削除 git branch -d

    <branchname> git branch -D <branchname> リモートブランチの削除 git branch -r -d <remote>/<branchname>
  5. branch ローカルブランチを作成/削除する ブランチの移動(名前変更) git -m <oldbranch> <newbranch> git -M <oldbranch>

    <newbranch> トラッキング先を設定する git branch --set-upstream <local_branch> <remote_branch> これをするとgit push/pullの時にリモートブランチ 名を指定しなくても、設定したブランチに対して実 行してくれます。
  6. checkout ブランチを取得する git checkout <branch> ブランチを作成してチェックアウト git checkout -b <newbranch>

    [<startpoint>] リモートブランチのトラッキングブランチを作成してチェックアウト git checkout -t <remote>/<branch> コミット前の変更を取り消す git checkout <path> ...
  7. merge ブランチを取り込む git merge <branch> fast-forwardしない git merge --no-ff <branch>

    マージのログを1つに圧縮する git merge --squash <branch>
  8. merge squashとは squashはマージですが、通常のコミットとして扱います。 C---D---E topic / A---B---F---G master 例えば上記の状態で、git merge

    --squash topicとすると下記 の状態になります C---D---E topic / A---B-------------F master FにはC, D, Eの変更が含まれています
  9. rebase ブランチを統合する git rebase <branch> 次の状態が C---D---E topic / A---B---F---G

    master こうなります C'---D'---E' topic / A---B---F---G master
  10. pull fetch & merge git pull [<remote>] [<branch>] 次の2つは等価です git

    pull origin master git fetch origin && git merge origin/master 次の設定をしていると、git pullだけで<remote>と <branch>を自動解決してくれます。 git branch --set-upstream <localbranch> <remote>/<remoptebranch>
  11. pull fetch & rebase mergeの代わりにrebaseを使うこともできます。 git pull --rebase <remote> <branch>

    git pull --rebaseは1.7.2で導入されたもので、比較 的新しいオプションです。 今まではmergeしか選択できませんでしたが、これ にはいくつかの問題がありました。
  12. pull いちいち--rebaseを打つのがメンドイ メンドイのでデフォルトでrebaseを使うように設定します。 <branch>に対するpullでは--rebaseを行う git config branch.<branch>.rebase true トラッキングブランチの作成時の上記の設定を自動で付与する。 git

    config --global branch.autosetuprebase always 1.7.9以降の場合は次の設定も使える git config --global pull.rebase true この場合、git config branch.<branch>.rebase.falseとすると、 <branch>に対するpullだけはmergeで行われる。
  13. rm 削除する git rm <file> ディレクトリを削除する git rm -r <dir>

    ステージングから削除する git rm --cached <file>
  14. reset 現在から過去を取り消す 直前のコミットをステージングの状態まで戻す git reset --soft HEAD~ 直前のコミットを完全に戻す git reset

    [--mixed] HEAD~ 直前のコミットを完全に戻して、untrackなものは削除する git reset --hard HEAD~ <commit>まで戻す git reset ( --soft | --mixed | --hard ) <commit>
  15. remote リモートの管理 リモートの追加 git remote add <name> <url> リモートの削除 git

    remote rm <name> リモートサーバ上に存在しないリモートブランチの削除 git remote prune <name>
  16. tag タグの管理 タグの作成 git tag <tagname> [<commit>] タグの削除 git tag

    -d <tagname> 署名付きタグの作成 git tag ( -s | -u <key_id> ) <tagname>
  17. log ログを見る デフォルト git log ハッシュと派生したブランチを見やすくしたい git log --oneline --decorate

    変更されたファイルとパッチも表示しちゃう git log --stat git log --patch-with-stat
  18. これでもまだほんの一部 1.7.11.3時点でのコマンド数: 152 add diff lost-found prune-packed shortlog add--interactive diff-files

    ls-files pull show am diff-index ls-remote push show-branch annotate diff-tree ls-tree quiltimport show-index apply difftool mailinfo read-tree show-ref archive difftool--helper mailsplit rebase stage bisect fast-export merge receive-pack stash bisect--helper fast-import merge-base reflog status blame fetch merge-file relink stripspace branch fetch-pack merge-index remote submodule bundle filter-branch merge-octopus remote-ext svn cat-file fmt-merge-msg merge-one-file remote-fd symbolic-ref check-attr for-each-ref merge-ours remote-ftp tag check-ref-format format-patch merge-recursive remote-ftps tar-tree checkout fsck merge- resolve remote-http unpack-file checkout-index fsck-objects merge-subtree remote-https unpack-objects cherry gc merge-tree remote-testgit update-index cherry-pick get-tar- commit-id mergetool repack update-ref clean grep mktag replace update-server-info clone hash-object mktree repo-config upload-archive column help mv request-pull upload-pack commit http-backend name-rev rerere var commit-tree http-fetch notes reset verify-pack config http-push p4 rev-list verify-tag count-objects imap-send pack- objects rev-parse web--browse credential-cache index-pack pack-redundant revert whatchanged credential-cache--daemon init pack-refs rm write-tree credential-store init- db patch-id send-pack daemon instaweb peek-remote sh-i18n--envsubst describe log prune shell
  19.       ._        \ヽ, ,、         `''|/ノ          .|     

    _   |      \`ヽ、|       \, V          `L,,_          |ヽ、)  ,、         /    ヽYノ        /    r''ヽ、.|       |     `ー-ヽ|ヮ       |       `|       |.        |       ヽ、      |         ヽ____ノ         /_ノ ' ヽ_\       /(≡)   (≡)\      /::::::⌒(__人__)⌒::::: \      |     |r┬-|     |      \      `ー'´     /      /          \      (  |          |  )      \|    э    |/        (    ,,,,    ,ノ        \  、(U)ノ ノ          \/  /            ┼ヽ  -|r‐、. レ |          /  /\            d⌒) ./| _ノ  __ノ