🐥

よく使う Git コマンドまとめ

2022/10/03に公開

よく使う Git コマンドまとめ

Git を教える機会があったので、まとめておく。

  1. セットアップ
    • git init
    • git config --local user.name <user name>
    • git config --local user.email <user email>
    • git branch -M <branch name>
    • git remote add origin <url>
  2. 現在参照しているブランチ名を変更
    • git branch -m <new branch name>
  3. 新規ブランチを任意のブランチをベースにして作成
    • git checkout -b <new branch name> <base branch name>
  4. ブランチを強制的に変更(checkout)
    • git checkout -f <branch name>
  5. インデックスに記録されているファイルをインデックスから解除
    • git restore --staged <file name>
  6. インデックスに記録されているファイルとリモートトラッキングブランチとの差分
    • git diff --staged origin/<branch name>
  7. プルリクエストで発生したコンフリクトを修正
    • merge
      • git fetch
      • git merge <branch name>
      • git add <file name>
      • git merge --continue
    • rebase
      • git fetch
      • git rebase origin/<branch name>
      • git add <file name>
      • git rebase --continue
    • pull
      • rebase
        • git pull --rebase origin <branch name>
        • git add <file name>
        • git rebase --continue
      • merge
        • git pull origin <branch name>
        • git add <file name>
        • git merge --continue
  8. インデックスに記録されている変更を直前のコミットに混ぜる
    • git commit --amend
  9. 前にいたブランチに戻る
    • git checkout -
  10. ファイルの行単位で最終変更がどのコミットで行われたのか確認
    • git blame <file name>
  11. 直前のコミットで変更したファイルの内容を見る
    • git show
  12. コミット対象外のファイルを削除
    • git clean -f
      • option
        • f 強制
        • d ディレクトリも含む
        • n 空実行
  13. ワーキングツリーの変更を元に戻す
    • git checkout .
  14. ワーキングツリーとインデックスの変更を元に戻す
    • git checkout -f
  15. リベースをキャンセル
    • git rebase --abort
  16. cherry-pick をキャンセル
    • git cherry-pick --abort
  17. マージをキャンセル
    • git merge --abort
  18. 特定のファイルをインデックスに追加
    • git add <file name>
  19. HEAD の状態をインデックスに戻す
    • git reset --mixed HEAD^
  20. ワーキングツリーとインデックスの状態を HEAD に戻す
    • git reset --hard HEAD
  21. 特定のファイルのブロックだけインデックスに追加
    • git add -p <file name>
  22. ワーキングツリーとインデックスの差分をインデックスに追加
    • git add -u
  23. インデックスと HEAD の差分を表示
    • git diff --staged
  24. 強制的に push
    • git push -f origin <branch name>
  25. リモートブランチの更新を取り込んで特定のブランチをベースにリベース
    • git pull --rebase origin <remote branch name>
  26. HEAD のコミットメッセージを変更
    • git commit --amend
  27. Git の操作履歴を閲覧
    • git reflog
  28. 前のブランチに強制的にチェックアウト
    • git checkout - -f
  29. ワーキングツリーとインデックスの変更を直前のコミットに混ぜる
    • git commit -a --amend
  30. サイズの大きなファイルをプッシュ(100MB 超)
    • Git LFS を使用
      • brew install git-lfs
      • git lfs install --local
    • 管理ファイル設定
      • git lfs track <large file path>
      • git add .gitattributes
      • git add <large file path>
    • git commit
    • git push origin <branch>
GitHubで編集を提案

Discussion