Skip to content

Instantly share code, notes, and snippets.

@GOROman
Created January 14, 2026 13:35
Show Gist options
  • Select an option

  • Save GOROman/0069a27b189781c67f046205f5585ff3 to your computer and use it in GitHub Desktop.

Select an option

Save GOROman/0069a27b189781c67f046205f5585ff3 to your computer and use it in GitHub Desktop.
new.sh
# 新規リポジトリ作成 or clone
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