2
4

git checkoutとgit switchの違いは把握していますか?

Posted at
  1. 導入時期:

    • git checkout: Gitの初期からある古いコマンド
    • git switch: Git 2.23(2019年8月)で導入された新しいコマンド
  2. 主な用途:

    • git checkout: ブランチの切り替え,ファイルの復元,新しいブランチの作成など多機能
    • git switch: 主にブランチの切り替えと作成に特化
  3. 構文の違い:

    • 新しいブランチを作成して切り替える場合:
      git checkout -b <new-branch>
      git switch -c <new-branch>
      
    • 既存のブランチに切り替える場合:
      git checkout <existing-branch>
      git switch <existing-branch>
      
  4. ファイル操作:

    • git checkout: ファイルを特定のバージョンに戻すことができる
    • git switch: ブランチの切り替えのみを行い,ファイル操作機能は持たない
  5. 安全性:

    • git switch: より安全性が高く,誤ってファイルを上書きするリスクが低い
    • git checkout: 多機能であるが,誤用のリスクがやや高い
  6. 明確さ:

    • git switch: 目的が明確で,ブランチ操作に特化している
    • git checkout: 多目的で柔軟だが,時に意図が不明確になることがある
  7. git checkout でできること:

    a. ブランチの切り替え:

    git checkout <branch-name>
    

    b. 新しいブランチの作成と切り替え:

    git checkout -b <new-branch-name>
    

    c. 特定のコミットへの移動:

    git checkout <commit-hash>
    

    d. ファイルを特定のバージョンに戻す:

    git checkout <commit-hash> -- <file-path>
    

    e. 直前のブランチに戻る:

    git checkout -
    

    f. リモートブランチをトラッキングして切り替え:

    git checkout -t origin/<branch-name>
    
  8. git switch でできること:

    a. ブランチの切り替え:

    git switch <branch-name>
    

    b. 新しいブランチの作成と切り替え:

    git switch -c <new-branch-name>
    

    c. 直前のブランチに戻る:

    git switch -
    

    d. 新しいブランチを作成し,特定のコミットから始める:

    git switch -c <new-branch-name> <commit-hash>
    

    e. ブランチを作成せずに特定のコミットに移動(detached HEAD状態):

    git switch --detach <commit-hash>
    

    f. リモートブランチをトラッキングして切り替え:

    git switch -c <branch-name> origin/<branch-name>
    
2
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
4