Skip to content

Instantly share code, notes, and snippets.

@GOROman
Created February 24, 2026 01:42
Show Gist options
  • Select an option

  • Save GOROman/50a48cd36b20dd7f7c3b811ce5765164 to your computer and use it in GitHub Desktop.

Select an option

Save GOROman/50a48cd36b20dd7f7c3b811ce5765164 to your computer and use it in GitHub Desktop.
new — GitHub リポジトリを作って ghq 管理下に落として開く

new — GitHub リポジトリを作って ghq 管理下に落として開く

new は、指定したリポジトリを (必要なら GitHub 上で作成し)ghq で取得し、ローカルの作業ディレクトリに cd するための zsh 関数です。

  • 引数に応じて owner/repo の解決clone URL の正規化を行います
  • 既存リポジトリでもOK(作成はしない/取得して移動するだけ)
  • repo を作るケースでは --public / --private を指定できます(デフォルトは --private

必要なもの(依存)

  • zsh
  • GitHub CLI: gh(認証済みであること)
  • ghqghq rootghq get が使えること)
  • Git

使い方

new <repo-name|owner/repo|git-url> [--public|--private]
  • --public / --private のどちらか(省略時は --private
  • 可視性フラグが不正な場合はエラーで終了します

引数パターンと挙動

1) repo-name(例: mytool

  • gh api user -q .login で自分の GitHub ログイン名を取得し owner にする
  • gh repo create owner/repo --private|--public --confirm を試みる
    • すでに存在する場合でも、gh repo view owner/repo が通ればそのまま続行
  • ghq get で取得し、ghq root/github.com/owner/repo に移動

2) owner/repo(例: octo-org/infra

  • リポジトリ作成はしない(既存前提)
  • git@github.com:owner/repo.git に正規化して ghq get
  • 取得後に同様に移動

3) Git URL

  • SSH形式: git@github.com:owner/repo(.git)
  • HTTPS形式: https://github.com/owner/repo(.git)
  • どちらも owner / repo を抽出し、ghq get して移動

使用例

# 自分のリポジトリを private で作って開く(デフォルト private)
new my-new-repo

# public で作って開く
new my-open-repo --public

# 既存リポジトリを取得して開く
new cli/cli
new git@github.com:cli/cli.git
new https://github.com/cli/cli

エラーと注意

  • 引数が空の場合は usage を表示して終了します
  • 可視性フラグは --public または --private のみ受理します
  • ghq get が失敗しても || true で握りつぶすので、
    • すでに取得済み(あるいは失敗した)ケースでも次へ進みます
    • 最終的に cd に失敗するとそこで止まります(setopt err_return
  • cd 先は $(ghq root)/github.com/$owner/$repo 固定です
    ghq の配置ルールを変えている場合は期待どおりにならない可能性があります

インストール(.zshrc への追加)

~/.zshrc に関数定義を貼り付けたら、シェルを再読込します。

source ~/.zshrc
unalias new 2>/dev/null
new() {
emulate -L zsh
setopt err_return pipefail
local arg="$1"
[[ -z "$arg" ]] && { echo "usage: new <repo-name|owner/repo|git-url> [--public|--private]"; return 1; }
local vis="${2:---private}"
case "$vis" in
--public|--private) ;;
*) echo "visibility must be --public or --private"; return 1 ;;
esac
local owner repo url
if [[ "$arg" == git@github.com:* ]]; then
owner="${arg#git@github.com:}"; owner="${owner%%/*}"
repo="${arg#git@github.com:${owner}/}"; repo="${repo%.git}"
url="$arg"
elif [[ "$arg" == https://github.com/* ]]; then
owner="${arg#https://github.com/}"; owner="${owner%%/*}"
repo="${arg#https://github.com/${owner}/}"; repo="${repo%.git}"
url="git@github.com:${owner}/${repo}.git"
elif [[ "$arg" == */* ]]; then
owner="${arg%%/*}"
repo="${arg#*/}"
url="git@github.com:${owner}/${repo}.git"
else
owner="$(gh api user -q .login)" || return 1
repo="$arg"
url="git@github.com:${owner}/${repo}.git"
gh repo create "${owner}/${repo}" "$vis" --confirm 2>/dev/null || {
gh repo view "${owner}/${repo}" >/dev/null 2>&1 || return 1
}
fi
ghq get "$url" || true
local root
root="$(ghq root)" || return 1
cd "$root/github.com/$owner/$repo" || return 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment