0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【GitHub】GitHubをCLI で操作したい

0
Posted at

はじめに

転職を機にCLIネイティブを目指し日々奮闘中です。
今回は、今までブラウザ上で行なっていたgithub操作をghコマンドで実施することに取り組んでみました。
つまづく場面が多々あったので、備忘録としてまとめます。

行うこと

  1. リポジトリの作成
  2. 作成リポジトリのクローン
  3. リポジトリをブラウズ
  4. リポジトリの削除

1. リポジトリの作成

1-1. ghコマンドのインストール

macのインストール方法を記載しています。

brew install gh

1-2. GitHub認証を行う

gh auth login

# GitHub.comを選択
? Where do you use GitHub?  [Use arrows to move, type to filter]
> GitHub.com
  Other
# HTTPSを選択
? What is your preferred protocol for Git operations on this host?  [Use arrows to move, type to filter]
> HTTPS
  SSH
# Yesを選択
? Authenticate Git with your GitHub credentials? (Y/n) Y
# web browserでログインorトークンをペースト
# web browserでログインを選択
? How would you like to authenticate GitHub CLI?  [Use arrows to move, type to filter]
> Login with a web browser
  Paste an authentication token
# 認証コードとURLが表示
# URLにアクセスして認証完了!
? How would you like to authenticate GitHub CLI? Login with a web browser

! First copy your one-time code: A7EA-0949
Press Enter to open https://github.com/login/device in your browser...

HTTPS or SSHの選択は以下を参考にしました。
一般ユーザーにはHTTPSで十分とおすすめされております。

1-3.リポジトリ作成

# privateリポジトリ
gh repo create リポジトリ名 --private
# publicリポジトリ
gh repo create リポジトリ名 --public

2.作成リポジトリのクローン

2-1. ghqコマンドのインストール

brew install ghq

ghqとは

README日本語訳より引用

「ghq」は、go getのようにリモートリポジトリのクローンを整理するためのツールです。ghq getを使ってリモートリポジトリをクローンすると、ghqはリモートリポジトリのURLのホスト名とパスを使って、特定のルートディレクトリ(デフォルトでは ~/ghq)配下にディレクトリを作成します。

2-2.ghq設定

~/.gitconfigにクローン先のパスを指定

~/.gitconfig
[ghq]
 root = /Users/ユーザー名/任意のディレクトリ
[github]
	user = Githubのユーザー名

上記を指定しない場合、クローン時に下記のエラーとなってしまいました。

remote: Repository not found.
fatal: repository 'https://github.com/Githubユーザー名/リポジトリ名/' not found
     error failed to get "リポジトリ名": /usr/bin/git: exit status 128

githubユーザー名は以下の方法でも指定可能です。

# ユーザー名を設定しているか確認
git config --global github.user

# ユーザー名を設定する
git config --global github.user GitHubのユーザー名

2-3.リポジトリのクローン

# 自分が作成したリポジトリ
ghq get リポジトリ名
# 自分以外の方が作成したリポジトリ
ghq get ユーザー名/リポジトリ名

ghqでクローンすると、以下のようにURLの構造を反映したパスに配置されます。

~/.gitconfigで指定したパス
  └─ github.com
      └─ ユーザー名
          └─ リポジトリ名

GitLabなど他のホストやユーザーのリポジトリが混在しても衝突しないのがghqの特徴です。

3.リポジトリをブラウズ

# クローンしたディレクトリ外から開きたい場合
gh repo view リポジトリ名 --web
# クローンしたディレクトリ内から開く場合
gh browse

4. リポジトリの削除

gh repo delete リポジトリ名

おわりに

ブラウザ経由せず、ターミナル上で完結できるのは本当に便利だなと思いました!
ブランチ管理やコミットなどまだまだ取り組めることはあるので、本記事を通して少しずつできることを増やしていきたいと思います。

参考文献

0
0
0

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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?