Git Version 2.23.0よりswitchコマンドとrestoreコマンドが追加されました。両コマンドはこれまでcheckoutコマンドが担ってきた機能を整理し、コマンドとして理解しやすくする目的で追加されています。
switch
switchはブランチの切り替えと新規作成の機能を担います。今までのbranchコマンドとcheckout -bを置き換えます(それぞれのコマンドは今でも有効)。
Git - git-switch Documentation
例
masterブランチへの切り替え
<span class="hljs-string">$ </span>git switch master
<span class="hljs-string">$ </span>git switch master
$ git switch master
ブランチの切り替えで、ローカルで変更したファイルに問題が起きた場合には次のようにエラーになります。
$ git switch mytopic
<span class="hljs-keyword">error: </span>You have local changes to 'frotz'; not switching branches.
$ git switch mytopic
<span class="hljs-keyword">error: </span>You have local changes to 'frotz'; not switching branches.
$ git switch mytopic
error: You have local changes to 'frotz'; not switching branches.
この場合には、-m
オプションで3者間マージにする事ができます。
<span class="hljs-string">$ </span>git switch -m mytopic
<span class="hljs-type">Auto</span>-merging frotz
<span class="hljs-string">$ </span>git switch -m mytopic
<span class="hljs-type">Auto</span>-merging frotz
$ git switch -m mytopic
Auto-merging frotz
-c
オプションを使用することで新しいブランチを作成できます(-c
は--create
の短縮形です)。
$ git <span class="hljs-keyword">switch</span> -<span class="hljs-built_in">c</span> good-surprises
$ git <span class="hljs-keyword">switch</span> -<span class="hljs-built_in">c</span> good-surprises
$ git switch -c good-surprises
任意のコミットから新しいブランチを成長させることができます。たとえば、「HEAD〜3」に切り替えて、ブランチ「fixup」を作成します。
$ git <span class="hljs-keyword">switch</span> -c fixup HEAD~<span class="hljs-number">3</span>
Switched <span class="hljs-built_in">to</span> <span class="hljs-keyword">a</span> <span class="hljs-built_in">new</span> branch <span class="hljs-string">'fixup'</span>
$ git <span class="hljs-keyword">switch</span> -c fixup HEAD~<span class="hljs-number">3</span>
Switched <span class="hljs-built_in">to</span> <span class="hljs-keyword">a</span> <span class="hljs-built_in">new</span> branch <span class="hljs-string">'fixup'</span>
$ git switch -c fixup HEAD~3
Switched to a new branch 'fixup'
restore
復元ソースからの内容を使用して、作業ツリーの指定されたパスを復元します。パスが追跡されているが、復元ソースに存在しない場合は、ソースに一致するように削除されます。
Git - git-restore Documentation
例
インデックス内のバージョンと一致するようにすべてのCソースファイルを復元する場合、次のようにできます。
<span class="hljs-string">$ </span>git restore <span class="hljs-string">'*.c'</span>
<span class="hljs-string">$ </span>git restore <span class="hljs-string">'*.c'</span>
$ git restore '*.c'
現在のディレクトリ内のすべてのファイルをインデックスの内容で復元するには
<span class="hljs-string">$ </span>git restore .
<span class="hljs-string">$ </span>git restore .
$ git restore .
インデックスのファイルを復元して、HEADのバージョンと一致させる
$ git <span class="hljs-keyword">restore</span> <span class="hljs-comment">--staged hello.c</span>
$ git <span class="hljs-keyword">restore</span> <span class="hljs-comment">--staged hello.c</span>
$ git restore --staged hello.c
または、インデックスと作業ツリーの両方を復元できます
<span class="hljs-comment">$</span> <span class="hljs-comment">git</span> <span class="hljs-comment">restore</span> <span class="hljs-literal">-</span><span class="hljs-literal">-</span><span class="hljs-comment">source=HEAD</span> <span class="hljs-literal">-</span><span class="hljs-literal">-</span><span class="hljs-comment">staged</span> <span class="hljs-literal">-</span><span class="hljs-literal">-</span><span class="hljs-comment">worktree</span> <span class="hljs-comment">hello</span><span class="hljs-string">.</span><span class="hljs-comment">c</span>
<span class="hljs-comment">$</span> <span class="hljs-comment">git</span> <span class="hljs-comment">restore</span> <span class="hljs-literal">-</span><span class="hljs-literal">-</span><span class="hljs-comment">source=HEAD</span> <span class="hljs-literal">-</span><span class="hljs-literal">-</span><span class="hljs-comment">staged</span> <span class="hljs-literal">-</span><span class="hljs-literal">-</span><span class="hljs-comment">worktree</span> <span class="hljs-comment">hello</span><span class="hljs-string">.</span><span class="hljs-comment">c</span>
$ git restore --source=HEAD --staged --worktree hello.c
感想
どこまでわかりやすくなったのかは疑問だが、今までよりは良いと思う。
コメント