gh repoコマンド

gh repoコマンドは、リポジトリの作成やclone、forkなどのリポジトリ関連の操作を行うことができます。

gh repo <command> [flags]で実行し、<command>には以下の4つが利用できます。

clone:      Clone a repository locally
create:     Create a new repository
fork:       Create a fork of a repository
view:       View a repository

gh repo clone

gh repo cloneは、リポジトリのcloneを行うことができます。

gh repo clone <repository> [<directory>] [-- <gitflags>...]で実行します。

$ gh repo clone cli/cli

Cloning into 'cli'...
remote: Enumerating objects: 21, done.
remote: Counting objects: 100% (21/21), done.
remote: Compressing objects: 100% (18/18), done.
remote: Total 13758 (delta 9), reused 11 (delta 3), pack-reused 13737
Receiving objects: 100% (13758/13758), 32.50 MiB | 4.07 MiB/s, done.
Resolving deltas: 100% (9177/9177), done.

リポジトリの後ろにcloneしたいパスを指定することもできます。

$ gh repo clone cli/cli path/to/repos
Cloning into 'path/to/repos'...
remote: Enumerating objects: 21, done.
remote: Counting objects: 100% (21/21), done.
remote: Compressing objects: 100% (18/18), done.
remote: Total 13758 (delta 9), reused 11 (delta 3), pack-reused 13737
Receiving objects: 100% (13758/13758), 32.50 MiB | 3.77 MiB/s, done.
Resolving deltas: 100% (9174/9174), done.

$ ls !$
ls path/to/repos
LICENSE   README.md auth      command   docs      go.mod    internal  script    update    wix.json
Makefile  api       cmd       context   git       go.sum    pkg       test      utils

help

$ gh repo clone --help
Clone a GitHub repository locally.

If the "OWNER/" portion of the "OWNER/REPO" repository argument is omitted, it
defaults to the name of the authenticating user.

Pass additional 'git clone' flags by listing them after '--'.


USAGE
  gh repo clone <repository> [<directory>] [-- <gitflags>...]

INHERITED FLAGS
  --help   Show help for command

LEARN MORE
  Use 'gh <command> <subcommand> --help' for more information about a command.
  Read the manual at https://cli.github.com/manual

gh repo create

gh repo createは、リポジトリの新規作成を行うことができるコマンドです。

gh repo create [<name>] [flags]で実行します。

フラグ

以下のフラグが利用できます。

flags short flags long 説明
-y --confirm 確認なし
-d --description リポジトリの説明を追加
  --enable-issues issueを有効にするか(デフォルトtrue)
  --enable-wiki wikiを有効にするか(デフォルトtrue)
-h --homepage ホームページURL
  --internal リポジトリをinternalで作成するか
  --private リポジトリをprivateで作成するか
  --private リポジトリをpublicで作成するか
-t --team アクセスできるorganizationチーム
-p --template テンプレートリポジトリから作成

対話形式で作成

fooというリポジトリを作ってみましょう。 以下の質問がされます。

  1. Visibility(publicなのかprivateなのかとか)どうする?
  2. currentディレクトリに作って良い?(Noを指定するとリポジトリ作らない。yesならこの時点でGitHub上に作成される)
  3. ローカルプロジェクトディレクトリをつくる?(Noを指定するとローカルのカレントディレクトリには作られない)
$ gh repo create foo
? Visibility Public
? This will create 'foo' in your current directory. Continue?  Yes
✓ Created repository JIIOryo/foo on GitHub
? Create a local project directory for JIIOryo/foo? Yes
Initialized empty Git repository in /path/to/repos/foo/.git/
✓ Initialized repository in './foo/'

ローカルプロジェクトディレクトリを作るを選択すると、ローカルにディレクトリを作成してgit initをしてくれてるっぽい。 https://github.com/cli/cli/blob/b2e36a0979a06b94bf364552a856c166cd415234/pkg/cmd/repo/create/create.go#L291

コマンド一発で作成

例えば、hoge-repoリポジトリを公開で説明付きで作成したい場合...

$ gh repo create hoge-repo -y -d 'This is description!' --public

✓ Created repository JIIOryo/hoge-repo on GitHub
Initialized empty Git repository in /path/to/repos/hoge-repo/.git/
✓ Initialized repository in './hoge-repo/'

こんな感じにリポジトリが一発で作成されました😲

スクリーンショット 2020-09-20 2.46.11.png

help

$ gh repo create --help
Create a new GitHub repository.

USAGE
  gh repo create [<name>] [flags]

FLAGS
  -y, --confirm              Confirm the submission directly
  -d, --description string   Description of repository
      --enable-issues        Enable issues in the new repository (default true)
      --enable-wiki          Enable wiki in the new repository (default true)
  -h, --homepage string      Repository home page URL
      --internal             Make the new repository internal
      --private              Make the new repository private
      --public               Make the new repository public
  -t, --team string          The name of the organization team to be granted access
  -p, --template string      Make the new repository based on a template repository

INHERITED FLAGS
  --help   Show help for command

ARGUMENTS
  A repository can be supplied as an argument in any of the following formats:
  - <OWNER/REPO>
  - by URL, e.g. "https://github.com/OWNER/REPO"

EXAMPLES
  # create a repository under your account using the current directory name
  $ gh repo create

  # create a repository with a specific name
  $ gh repo create my-project

  # create a repository in an organization
  $ gh repo create cli/my-project

LEARN MORE
  Use 'gh <command> <subcommand> --help' for more information about a command.
  Read the manual at https://cli.github.com/manual

gh repo fork

gh repo forkコマンドは、リポジトリのforkの新規作成を行うことができます。

gh repo fork [<repository>] [flags]で実行します。

フラグ

以下のフラグが利用できます。

flags short flags long 説明
  --clone cloneするかどうか
  --remote リモートを設定するか

対話形式でforkしてみます。(cloneするかどうか聞かれる)

$ gh repo fork cli/cli

- Forking cli/cli...
✓ Created fork JIIOryo/cli
? Would you like to clone the fork? Yes

Cloning into 'cli'...
remote: Enumerating objects: 21, done.
remote: Counting objects: 100% (21/21), done.
remote: Compressing objects: 100% (18/18), done.
remote: Total 13758 (delta 9), reused 11 (delta 3), pack-reused 13737
Receiving objects: 100% (13758/13758), 32.50 MiB | 3.77 MiB/s, done.
Resolving deltas: 100% (9174/9174), done.
Updating upstream
From github.com:cli/cli
 * [new branch]      auth-check              -> upstream/auth-check
 * [new branch]      branch-switcher         -> upstream/branch-switcher
 * [new branch]      bulk-cmd                -> upstream/bulk-cmd
 * [new branch]      custom-commands         -> upstream/custom-commands
 * [new branch]      debians                 -> upstream/debians
 * [new branch]      default-cfg             -> upstream/default-cfg
 * [new branch]      fix-query               -> upstream/fix-query
 * [new branch]      ghe-conditional-request -> upstream/ghe-conditional-request
 * [new branch]      gist-list               -> upstream/gist-list
 * [new branch]      linux-repos             -> upstream/linux-repos
 * [new branch]      migrate-repo-clone      -> upstream/migrate-repo-clone
 * [new branch]      more-gists              -> upstream/more-gists
 * [new branch]      multi-graphql           -> upstream/multi-graphql
 * [new branch]      pager-docs              -> upstream/pager-docs
 * [new branch]      patch-review-prototype  -> upstream/patch-review-prototype
 * [new branch]      relective-pr-lookup     -> upstream/relective-pr-lookup
 * [new branch]      run-checks              -> upstream/run-checks
 * [new branch]      search-cmd              -> upstream/search-cmd
 * [new branch]      the-newlygit-game       -> upstream/the-newlygit-game
 * [new branch]      trunk                   -> upstream/trunk
 * [new branch]      ubuntus                 -> upstream/ubuntus
 * [new branch]      web-flag                -> upstream/web-flag
✓ Cloned fork

$ ls cli

LICENSE   api       command   git       internal  test      wix.json
Makefile  auth      context   go.mod    pkg       update
README.md cmd       docs      go.sum    script    utils

help

$ gh repo fork --help
Create a fork of a repository.

With no argument, creates a fork of the current repository. Otherwise, forks the specified repository.

USAGE
  gh repo fork [<repository>] [flags]

FLAGS
  --clone    Clone the fork {true|false}
  --remote   Add remote for fork {true|false}

INHERITED FLAGS
  --help   Show help for command

LEARN MORE
  Use 'gh <command> <subcommand> --help' for more information about a command.
  Read the manual at https://cli.github.com/manual

gh repo view

gh repo viewコマンドは、リポジトリのdescriptionとREADMEを参照することができます。

gh repo view [<repository>] [flags]で実行します。

フラグ

以下のフラグが利用できます。

flags short flags long 説明
-w --web ブラウザで開く

リポジトリを明示的に指定しなければ現在いるリポジトリのREADMEが表示されます。ちゃんとマークダウンを認識して色が付けられていてとてもみやすいです😊

スクリーンショット 2020-09-19 22.53.49.png

ただ注意点としては、あくまでGitHub上の情報を参照しているので、ローカルでREADMEを作成、変更しても、pushしないとgh repo viewではみることができません。

-wフラグをつけることで、ブラウザを開いて参照することができます。一発でジャンプできて便利。

$ gh repo view cli/cli -w
Opening github.com/cli/cli in your browser.

help

$ gh repo view --help
Display the description and the README of a GitHub repository.

With no argument, the repository for the current directory is displayed.

With '--web', open the repository in a web browser instead.

USAGE
  gh repo view [<repository>] [flags]

FLAGS
  -w, --web   Open a repository in the browser

INHERITED FLAGS
  --help   Show help for command

LEARN MORE
  Use 'gh <command> <subcommand> --help' for more information about a command.
  Read the manual at https://cli.github.com/manual