見出し画像

Gist をローカルのリポジトリとして制作管理する方法を作りながらメモ

https://gist.github.com/ShinNakamura/34e4d9ab77675c982bb9bb3623126f63 から転記。

Gist をローカルのリポジトリとして制作管理する方法を作りながらメモ

こちらの Gist を参考にさせていただきました。
https://gist.github.com/t-nissie/9580883

まずは WEB の Gist 上で新規作成

Gist のサイト上でいったん新規作成し、まずはデフォの Create secret gist で保存します。

git clone

  1. ローカル環境に任意のディレクトリを作成

  2. 例えばこの Gist の URL : https://gist.github.com/ShinNakamura/34e4d9ab77675c982bb9bb3623126f63 であれば 34e4d9ab77675c982bb9bb3623126f63 の部分をメモ → `${gist_id}`

  3. クローンするディレクトリ名のために Gist 名をメモ。この Gist の場合なら CreateGistWithLocalRepo → `${gist_name}`

  4. コマンドラインにて任意のディレクトリに移動し、`git clone git@gist.github.com:${gist_id}.git ${gist_name}` を実行すれば `${gist_name}/` というディレクトリができ、その中に `${gist_name}.md` が入ってる。

コマンドライン操作をまとめると

mkdir -p ~/path/to/local_gist
cd ~/path/to/local_gist
gist_id="34e4d9ab77675c982bb9bb3623126f63"
gist_name="CreateGistWithLocalRepo"
git clone git@gist.github.com:${gist_id}.git ${gist_name}
cd ${gist_name}/
# vi ${gist_name}.md # など、好みのエディタで編集

ignore .girignore

.gitignore を作成はするが、commit では無視するようにしないと push したときに Gist 上に表示されてしまう。

ただ表示されるだけなら別に良いのだが(良いのか?)、本来表示したいファイルよりも順番的に上に来てしまう。これは避けたい。

ということで、.gitignore 内に .gitignore 自体も書き込み、commit からは除外する。

ちなみにこの Gist の .gitignore はこのような内容。Vim を編集に使っているので、.swp ファイルと .gitignore が無視の対象になっている。

*.swp
.gitignore

git push

あとは普通のリポジトリ操作と全く一緒で、

  • `git add .` or `git add filename` でステージング

  • `git commit -m "コメント"` でコミット

  • `git push` でプッシュ

おまけ gistclone 関数

Gist をクローンしたい任意のディレクトリに今いるとして、クローンする際の諸々をまとめてやってくれる関数を `~/.bashrc` や `~/.bash_profile` などに定義しておけば楽かと思いました。

function gistclone() {
    # gitclone gist-url gist-name

    # https://gist.github.com/ShinNakamura/${gist_id} という形式のURLからbasenameでgist_idを抜き取る
    gist_id="$(basename "$1")"
    gist_name="$2"
    git clone "git@gist.github.com:${gist_id}.git" "${gist_name}"
    cd "${gist_name}"/
    # ignore .swp & .gitignore -> see https://gist.github.com/ShinNakamura/34e4d9ab77675c982bb9bb3623126f63
    test -f .gitignore || (echo "# ignore on $1" >.gitignore; echo '*.swp' >>.gitignore; echo ".gitignore" >>.gitignore)
}
export -f gistclone

SN

この記事が気に入ったらサポートをしてみませんか?