GitHub + SSH で複数アカウント切替え

Git HTTPS Credential Helper がややこしいので SSH に切り替える。

GitHub リポジトリを指す remote origin の変更が必要となりますので、ざっくり概要を見ておきます。


# HTTPS 利用していたとする
git remote -v
> origin https://github.com/OWNER/REPOSITORY.git (fetch)
> origin https://github.com/OWNER/REPOSITORY.git (push)

# SSH 形式の URL に変更
git remote set-url origin [email protected]:OWNER/REPOSITORY.git

# 確認
git remote -v
# Verify new remote URL
> origin [email protected]:OWNER/REPOSITORY.git (fetch)
> origin [email protected]:OWNER/REPOSITORY.git (push)

👉 リモートリポジトリを管理する - GitHub Docs

🧑🏻‍💻 手順

1. 鍵をPC側で作成。


❯ ssh-keygen -t ed25519 -C [email protected] -f ~/.ssh/id_ed25519_main

👉 新しい SSH キーを生成して ssh-agent に追加する - GitHub Docs

2. GitHub にログインして貼る。Authentication keys


❯ pbcopy < ~/.ssh/id_ed25519_main.pub

👉 SSH and GPG keys - GitHub

3. ~/.ssh/config を設定する。


❯ vi ~/.ssh/config

Host github-main
  HostName github.com
  User git
  Port 22
  IdentityFile ~/.ssh/id_ed25519_main
  TCPKeepAlive yes
  IdentitiesOnly yes # need for multiple accounts

Host github-sub
  HostName github.com
  User git
  Port 22
  IdentityFile ~/.ssh/id_ed25519_sub
  TCPKeepAlive yes
  IdentitiesOnly yes # need for multiple accounts

4. PC側プロジェクト内で remote origin を書き換える。


git@github-main:your-main/Sample.git


❯ git remote -v
origin https://github.com/your-main/Sample.git (fetch)
origin https://github.com/your-main/Sample.git (push)

❯ git remote set-url origin git@github-main:your-main/Sample.git

❯ git remote -v
origin git@github-main:your-main/Sample.git (fetch)
origin git@github-main:your-main/Sample.git  (push)

❯ cat .git/config
[remote "origin"]
url = git@github-main:your-main/Sample.git
fetch = +refs/heads/*:refs/remotes/origin/*

5. 接続を確認する。


❯ ssh -T git@github-main
Hi your-main! You've successfully authenticated, but GitHub does not provide shell access.

👉 [備忘] 複数Githubアカウントでssh接続設定(config)を使い分ける手順 #GitHub - Qiita

 

🧑🏻‍💻 まとめ

URL 記述部分は、ssh:// がついてるほうが意味が分かりやすい気がします。

その場合は ~/.ssh/config の port 22 の記述は不要のようですが、

現在 GitHub ではスキーマ部分消えています。

などとダラダラ書きましたが、以下読んでみるとよく分かります。

👉 Multiple GitHub Accounts & SSH Config - Stack Overflow
👉 Using multiple github accounts with ssh keys
👉 【Git】Obsidian を GitHub と連携する
👉 初心者向け Git コマンドと領域の移動をシーケンス図で書いてみた


【Xcode】ローカルと GitHub のソース管理連携設定

GitHub で久々に新規にリポジトリ作成してみたら、

もしかして、表示されなくなりました? こういうスクリプト。


Quick setup - if you've done this kind of thing before
https://github.com/your-account/sample.git


// ... or create a new repository on the command line

echo "# sample" >> README.md
git init
git add README.md
git commit -m "first commit"
git branch -M main
git remote add origin https://github.com/your-account/sample.git
pit push -u origin main


// ... or push an existing repository from the command line

git remote add origin https://github.com/your-account/sample.git
git branch -M main
git push -u orign main

👉 Github Quick setup — if you’ve done this kind of thing before hatena-bookmark

困ったので以下のやり方でやったらいけた。

👉 XcodeとGitHubの連携方法 #macOS - Qiita hatena-bookmark

ローカルにプロジェクトのソースはある。として、

そこからの「連携」の手順を最小限で覚えておく。

Control + クリック、かまたは 右クリックから


「New "PROJECT-NAME" Remote...」 で新規作成

で、リモート側(GitHub側)のリポジトリを作成して連携できる。

リモート側(GitHub側)にすでに作成済みの場合は、


「Add Existing Remote...」で既存リモートに接続

と選択すれば、あとは流れで分かる。

GUIからのキーバインド的なやつ。知らんと見つけるのつらいので書いておこうとな。

👉 Configuring your Xcode project to use source control | Apple Developer Documentation hatena-bookmark

続いて.gitignore はこちら。



【ChatGPT】Git comit message (コミットメッセージ) をサクッと簡単に作る macOS ショートカットと OPENAI プロンプト 🤖

Git のコミットメッセージに


- 英語で書くか日本語で書くか
- 絵文字を入れるか入れないか
- prefix を付けるか付けないか

という面倒な話はすべて解決します。

以下の動画のようなショートカットツールです。



 

🤖 プロンプト

ここがポイントです。


Create 10 git commit messages in English for the following changes in the following format. The emoji should be one of the github-shortcode characters that matches your changes.

Format: "emoji :github-shortcode: prefix:commit-message" 
Change Description: "アプリ起動時間のの短縮"

これをベースに会社や上司やプロジェクトの環境によって書き換えればいけます。


- 10
- English
- emoji
- shortcode
- prefix

ここらが変更対象の単語となるでしょう。

 

🤖 ChatGPT API

ChatGPT API を使うには課金登録して OPENAI_API_KEY が必要です。

macOS/iOS ショートカットアプリは、コンポーネントに


- WEB クライアント
- JSON パーサー

の機能を含む「Get Content of URL」という部品があらかじめ用意されています。

しかも、


- リクエストメソッド
- リクエストヘッダー
- リクエストボディ

を GUI からノーコードで設定できるので便利です。

 

🤖 ダウンロード

ショートカットは無料で公開できて、だれでもダウンロードして利用することがきます。


👉 Git Commit Message 😄 hatena-bookmark

[編集] から OPENAI_API_KEY を貼り付けると利用可能になります。

 

🤖 まとめ

ChatGPT API を使うには課金登録が必要ですが、この程度の利用ではいくらコミットしてもジュース1本分の金額には届きません。

実際に使いながら WEB API クライアントのプログラミング実装の基本を学ぶにも最適な教材になると思います。

今回は、厳格なプログラム作成に集中したあとに、ふんわりとしたコミットメッセージ作成はなんとなくつらく思えるので作ってみました。

この記事は、以下のプロジェクトのプロンプトにインスパイアされています。


    Refer to the provided git diff or code snippet and provide a suitable commit message.

    When reviewing the diff or code, focus on identifying the main purpose of the changes.
    Are they fixing a bug, adding a new feature, improving performance or readability, or something else?
    Use this information to craft a concise and detailed gitmoji commit message that clearly describes what the provided code or diff does.

    Describe the change to the best of your capabilities in one short sentence. Don't go into too much detail.

    When reviewing a diff, pay attention to the changed filenames and extract the context of the changes.
    This will help you create a more relevant and informative commit message.
    Here are some examples of how you can interpret some changed filenames:
      - Files or filepaths that reference testing are usually related to tests.
      - Markdown files are usually related to documentation.
      - Config file adjustments are usually related to configuration changes.

    Here is a list of gitmoji codes and their descriptions of what they mean when they are used: """
    ${gitmojis}
    """

    Try to match the generated message to a fitting emoji using its description from the provided list above.
    So go look in the descriptions and find the one that best matches the description.

    Always start your commit message with a gitmoji followed by the message starting with a capital letter.
    Never mention filenames or function names in the message.

    Don't do this:
      - :bug: Fix issue in calculateTotalPrice function
      - :zap: Improve performance of calculateTopProducts function
      - :lipstick: Refactor styling for calculateCartTotal function
      - :memo: Update documentation for getProductById function

    Do this:
      - :bug: Fix issue with shopping cart checkout process
      - :zap: Improve performance of search functionality
      - :lipstick: Refactor styling for product details page
      - :memo: Update documentation for API endpoints

    ${
      context
        ? `
          Refer to the provided additional context to assist you with choosing a correct gitmoji
          and constructing a good message: """
          ${context}
          """
        `
        : ''
    }

    Here is the provided git diff or code snippet: """
    ${prepareDiff(diff, minify)}
    """

👉 Generate your gitmoji commit message 👋 hatena-bookmark