Gitクローンしたい(ghq

$ brew install ghq
$ ghq --version
ghq version 1.4.2 (rev:7163e61)


$ ghq get git@github.com:shotakaha/kumaroot.git
$ ghq get shotakaha/kumaroot
$ ghq get kumaroot

ghqgit clone URLを簡単にしてくれるコマンドです。 GitHubからクローンする場合は、ユーザー名リポジトリ名だけ指定してもOKです。 ユーザー名を省略した場合は、(おそらく)ghqコマンドを実行したユーザー名が適用されます。

注釈

デフォルトはhttpsでクローンします。 SSHしたい場合は-pオプションを追加します。

リポジトリをクローンしたい(ghq get

$ ghq get git@github.com:shotakaha/kumaroot.git
Cloning into '~/repos/github.com/shotakaha/kumaroot'...

ghq getコマンドで、ghq.rootで設定したパスの下に、リポジトリのURLに沿った形式でクローンできるようになります。 どのディレクトリで実行してもOKなのも、このコマンドが便利な点です。

$ ghq get shotakaha/kumaroot
    exists /Users/shotakaha/repos/github.com/shotakaha/kumaroot

すでにクローンしてあるプロジェクトを指定した場合は、 クローン済であることを教えてくれます。

注釈

ブランチ/タグを指定したり、クローン先のディレクトリ名を変えたい場合は、 素直にgit cloneのオプションを使うのがよさそうです。

リポジトリを更新したい(ghq get -u

$ ghq get -u リポジトリ名
$ ghq get -u shotakaha/kumaroot
update ~/repos/github.com/shotakaha/kumaroot/
       git pull --ff-only
...

-u, --updateオプションを使って、リポジトリを更新できます。 内部でgit pull --ff-onlyしているようです。

デフォルトディレクトリを設定したい

$ git config --global ghq.root "~/repos/"

git configコマンドを使ってghq.rootを変更できます。 デフォルトでは~/ghqに設定されています。

ヒント

ghqコマンドは、僕の使い方にとてもマッチしていたツールでした。

このコマンドを知る以前は、同じようなことを手動でやっていました。

  • Gitリポジトリはすべて~/repos/の下で管理

  • GitHubは~/repos/github/の下にクローン

  • GitLabは~/repos/gitlab/の下にクローン

  • 上記のクローンするパスは自分で指定

また、動作確認したいリポジトリは~/repos/sandbox/の下にクローンして、いつでも使い捨てられるようにしているのですが、この使い方に並行してghqコマンドを導入できました。

リポジトリを確認したい(ghq list

$ ghq list
github.com/Geant4/geant4
github.com/RedPitaya/RedPitaya
github.com/gohugoio/hugo
github.com/shotakaha/kumaroot
github.com/shotakaha/zenn-docs
github.com/typst/typst
gitlab.com/qumasan/haniwers
sandbox/getting-started
sandbox/hugo-quickstart
sandbox/hugo-sandbox

ghq listでクローンしたリポジトリのパスを表示できます。

注釈

ghqを使ってクローンしたかどうかに関係なく、ghq.rootで指定したディレクトリにあるリポジトリがすべて表示されます。

$ ghq list 検索文字列

$ ghq list shotakaha
github.com/shotakaha/hugo_sandbox
github.com/shotakaha/kumaroot
github.com/shotakaha/zenn-docs
gitlab.com/shotakaha/brewfile
gitlab.com/shotakaha/cv

ghq listコマンドの引数に検索文字列を指定できます。 管理しているリポジトリが多い場合は、ユーザー名やプロジェクト名(の一部)で絞り込むことができます。