LoginSignup
149
90

More than 3 years have passed since last update.

git checkout -t でちょっと幸せになれる

Last updated at Posted at 2018-11-08

概要

みなさまは git checkout -t を使ったことはありますか?これを使うと既存のリモートブランチをローカルに作成する作業が少し楽になります。

以下の3つのコマンドは全て同じ結果になります。リモートブランチから新しいブランチをローカルに生成し、さらにそのブランチへ移動する という動作をさせるコマンドです。

branch + checkout

最初に覚えたのはこれです。

git branch proj/exampleWorks origin/proj/exampleWorks
git checkout proj/exampleWorks

checkout -b

次に「それコマンド1回で済むよ」と教えてもらったのがこれです。

git checkout -b proj/exampleWorks origin/proj/exampleWorks

checkout -t

そして今回紹介するコマンドです。さらにタイプ量が少なくなりました。

git checkout -t origin/proj/exampleWorks

*さらに追加情報

@uasi さんより proj/exampleWorks が複数の remote に存在する状態でなければ、下記コマンドだけで checkout が可能であることをコメントでご教示いただきました。origin のみで開発しているリポジトリであれば、こちらが一番スマートかと思います。

git checkout proj/exampleWorks

解説

If no -b option is given, the name of the new branch will be derived from the remote-tracking branch, by looking at the local part of the refspec configured for the corresponding remote, and then stripping the initial part up to the "*". This would tell us to use "hack" as the local branch when branching off of "origin/hack" (or "remotes/origin/hack", or even "refs/remotes/origin/hack"). If the given name has no slash, or the above guessing results in an empty name, the guessing is aborted. You can explicitly give a name with -b in such a case.

-t オプションは対象のリモートリポジトリとブランチを指定するオプションです。そして、-b オプションは実は省略が可能です。 -b オプションを省略すると、ローカルに作成されるブランチ名はリモートリポジトリのブランチの名前がそのまま使われます。

これを利用して リモートブランチから新しいブランチをローカルに生成し、さらにそのブランチへ移動するという動作をより少ないタイプ量でサクッと行うことができます。-b オプションの省略分だけタイプ量を減らすことができるわけですね。

最後に

知らなくてもたいして困ることはないですが、知っているとほんのすこしブランチ作成が楽になります。もしご存知なかったかたはぜひ試してみてください。

参考

149
90
4

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
149
90