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?

More than 3 years have passed since last update.

「ローカルとGitHubでgitレポジトリを作成して、エディタで開く」を1行で実行

Last updated at Posted at 2021-05-24

はじめに

自分はプログラミングを学習する際、サンプルコードを独立したレポジトリで管理しGitHubに上げています。
応用的なコードを書きたくなったら、さらに新規のレポジトリを作成します。

このルーティンが楽になると、学習が捗りそうです。

ということで、1行で以下を実行できるシェル関数を定義しました。

  • ローカルにgitレポジトリを作成
  • その内容でGithubにもレポジトリを作成しpush
  • VSCodeでローカルのレポジトリ内容を開く

前提

  • ghq
  • GitHub CLI
    • v1.9.2
    • これのおかげでGitHub Enterpriseにも対応しています
  • zsh/bash

方法

zshの場合

function ghrc {
  local GITHUB_ORG_URL=github.com/your_userid
  mkdir -p $(ghq root)/${GITHUB_ORG_URL} &&
  cd $(ghq root)/${GITHUB_ORG_URL} &&
  gh repo create $argv --confirm &&
  cd ${argv[1]} &&
  echo "# ${argv[1]}" >> README.md &&
  git add . && git commit -m "first commit" && git push --set-upstream origin master &&
  code .
}

.zshrcに書き足します。your_useridはあなたのユーザidに書き換えてください

ghrc hogehogeで、以下が実行されます。

  • ghqの形式に合ったディレクトリ構成で、ローカルにhogehogeレポジトリを作成
  • Githubにhogehogeレポジトリを作成
  • README.mdを適当に書き足しGitHubにpush
  • VSCodeで開く
    • code .部分を、お好みのエディタで起動するように変更可能です
      • idea .だったり
      • open . -a "sublime text"だったり

bashの場合

function ghrc {
  local GITHUB_ORG_URL=github.com/your_userid
  mkdir -p $(ghq root)/${GITHUB_ORG_URL} &&
  cd $(ghq root)/${GITHUB_ORG_URL} &&
  gh repo create $@ --confirm &&
  argv=("$@")
  cd ${argv[0]} &&
  echo "# ${argv[0]}" >> README.md &&
  git add . && git commit -m "first commit" && git push --set-upstream origin master &&
  code .
}

argv=("$@")の行が追加されただけです。your_useridはあなたのユーザidに書き換えてください

(fishの場合)

https://qiita.com/ryo2132/items/2a29dd7b1627af064d7b で「ghcr」と検索してカスタマイズする

push時にエラーが出る人へ

こんなエラー

fatal: The current branch chore/logdir has no upstream branch.
To push the current branch and set the remote as upstream, use

    git push --set-upstream origin some

が参考になります

参考url

感想

人によっては、npx create-react-appとかvue createとかプロジェクト作成CLIと組み合わせても良いかも?
golangの場合は、go mod init ${GITHUB_ORG_URL}/${argv[0]}の1行足すのもアリ

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?