LoginSignup
680
421

More than 3 years have passed since last update.

git switchとrestoreの役割と機能について

Last updated at Posted at 2019-08-19

先日8/16にGitバージョン2.23.0がリリースされました。
今回の目玉機能と言えば、新しいコマンド git switchgit restore ですね!
本稿ではこちらの2つに絞ってどういう役割・位置づけの機能なのか英語ソースの引用も含めてご説明します。

TL;DR

  • ブランチの変更は git switch
  • ファイルの変更は git restore
  • 今まで通り git checkout は使える
  • 新機能は「実験的機能」なので今後変更の可能性あり

新機能が追加された背景

Highlights from Git 2.23によると、 git checkout に出来ることがあまりに多いため(ブランチ操作のほか、indexされたファイルの復旧、履歴上のファイルの取得など)、役割を明確に分けるためのコマンドが追加されたとのことです。

It turns out git checkout can do quite a lot.
(中略)
The new commands, by contrast, aim to clearly separate the responsibilities of git checkout into two narrower categories: operations which change branches and operations which change files. To that end, git switch takes care of the former, and git restore the latter.

ブランチの変更は git switch に、ファイルの変更は git restore に役割を分けています。
git 2.23.0のドキュメントを見ても、各コマンドの役割が分けられたことで分かりやすくなっています。

公式ドキュメント(英語)
git-checkout | git-switch | git-restore

git checkoutのオプション

git checkout [-q] [-f] [-m] [<branch>]
git checkout [-q] [-f] [-m] --detach [<branch>]
git checkout [-q] [-f] [-m] [--detach] <commit>
git checkout [-q] [-f] [-m] [[-b|-B|--orphan] <new_branch>] [<start_point>]
git checkout [-f|--ours|--theirs|-m|--conflict=<style>] [<tree-ish>] [--] <paths>…​
git checkout [<tree-ish>] [--] <pathspec>…​
git checkout (-p|--patch) [<tree-ish>] [--] [<paths>…​]

git switch

git switch [<options>] [--no-guess] <branch>
git switch [<options>] --detach [<start-point>]
git switch [<options>] (-c|-C) <new-branch> [<start-point>]
git switch [<options>] --orphan <new-branch>

restore

git restore [<options>] [--source=<tree>] [--staged] [--worktree] <pathspec>…​
git restore (-p|--patch) [<options>] [--source=<tree>] [--staged] [--worktree] [<pathspec>…​]

特に git restore についてはオプションも含めて意味が明確で分かりやすくなったと感じます。

各コマンドと git checkout との比較

基本的には「機能を分けること」が目的であり、使い勝手は大きく変わらないです。

git switch

git switch はその名の通り、 checkout で行っていたブランチの変更を担当します。

# ブランチの切り替え
git checkout <branch>
git switch <branch>

# ブランチを新規作成して切り替え
git checkout -b <branch>
git switch -c <branch>

git restore

git restore はファイルの変更に使用します。ファイルを復旧させる時に使うことが多そうなので、良いネーミングですね。

# ファイルの変更を取り消す
git checkout -- <filename>
git restore <filename>

# 特定ファイルを特定のコミットの状態にする
git checkout <commit> -- <filename>
git restore --source <commit> <filename>

--worktree と --staged オプション

git restore コマンドは git reset のようにステージングエリアも変更出来るようになっています。

# ステージングエリアにあるファイルを復旧する(実ファイルへの変更はそのまま)
git reset <filename>
git restore --staged <filename>

# ワークツリー上のファイルを復旧する(実ファイルの変更がリセットされる。checkoutの例と同様)
git reset --hard <filename>
git restore --worktree <filename> # オプションがない場合、デフォルトで --worktree オプションが付きます

まとめ

個人的には git checkout の「機能全部乗せ感」がちょっと使いづらく感じていたので、今回の新機能は率先して使っていくつもりです。
ただ、これらの機能はあくまで "Experimental alternatives(実験的な代替手段)" ということなので(マニュアルにも書いてある)、今後仕様変更される可能性もありますので、使用される方は気に留めるようにしてください。

680
421
1

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
680
421