0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Obsidian Git の 認証エラーを HTTPS → SSH に切り替えて解決した(macOS)

0
Posted at

背景

普段は自宅のデスクトップ(Windows)で Obsidian を使っていて、Vault は GitHub で管理している。
年末年始の帰省で実家に MacBook Pro を持って帰ったので、この MacBook Pro でもデスクトップと同じように GitHub 経由で Vault を管理できるようにしたかった。

そこで既に使っているVaultを Mac 側にもCloneして、Obsidian Git プラグインで回す構成を作ろうとしたところ、認証エラーに遭遇した。
結論としては、接続方式が HTTPS のままだったのを SSH に切り替えることで解決できたので、ログと手順をまとめる。

発生したエラー(ログ)

GitHub Desktopを使ってGitHubから既に使っているVaultをCloneし、ObsidianでそのフォルダをVaultとして読み込んだ。
リポジトリ名称はmy_vaultということにして、適宜読み替えてください。
その後、Obsidian Gitを設定したところ、Obsidianのポップアップで次のようなエラーが出て止まった。


Pushing to [https://github.com/username/my_vault.git](https://github.com/username/my_vault.git)
remote: Invalid username or token. Password authentication is not supported for Git operations.
fatal: Authentication failed for '[https://github.com/username/my_vault.git/](https://github.com/username/my_vault.git/)'

Chat GPTに聞いてみたところ、以下の2つがポイントとのこと。

  • https://github.com/... なので、この Vault は HTTPS 接続で push/pull しようとしている
  • Password authentication is not supported と出ており、ユーザー名/パスワードでは Git 操作できない

HTTPS のまま進める場合は Personal Access Token(PAT)をきちんと扱う必要があるとのことで、今回は認証方式をSSHに変更することにした。(この記事のメイン)

現状確認:remote が HTTPS になっているかを見る

ChatGPTは上記のように言っていたが、本当にそうなっているかを確認するために、まずはこの Vault が GitHub にどう接続しようとしているかを確認した。

git remote -v

自分の環境ではこうなっていた。

origin  https://github.com/username/my_vault.git (fetch)
origin  https://github.com/username/my_vault.git (push)

https://... になっているので、HTTPS 経由で通信しようとしていることが確定。
ここから SSH に切り替える。

SSH 鍵を作って GitHub に登録する(macOS)

1) SSH鍵(ed25519)を作る

ssh-keygen -t ed25519 -C "examle_mail@example.com"
  • 保存先はデフォルトでOK(Enter)
  • パスフレーズは好み(空でも動く/入れるなら忘れないこと)

作成が終わると、だいたい次の2つができる。

  • ~/.ssh/id_ed25519(秘密鍵:GitHubに貼らない)
  • ~/.ssh/id_ed25519.pub(公開鍵:GitHubに登録するのはこっち)

2) 公開鍵をコピーして GitHub に登録

pbcopy < ~/.ssh/id_ed25519.pub

上記のコマンドを行うとクリップボードに公開鍵がコピーされるので、GitHub の以下に貼り付ける。

  • GitHub → SettingsSSH and GPG keysNew SSH key

    • Title:端末名(例:macbook-pro)など
    • Key:貼り付け

SSH 接続をテストする(初回は known_hosts の確認が出る)

次に、GitHub への SSH 接続をテストする。

ssh -T git@github.com

初回はこういう確認が出る。

The authenticity of host 'github.com (...)' can't be established.
...
Are you sure you want to continue connecting (yes/no/[fingerprint])?

ここは yes と入力して進める。(←ここで横着してEnterだけ押して1敗)

Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'github.com' (ED25519) to the list of known hosts.

これで接続先確認は完了なのだが、自分の場合は Permission denied (publickey). が出た。(先述の1敗)

git@github.com: Permission denied (publickey).

自分では意味不明なのでChat GPTに聞いたところ、「これはGitHub がこちらの公開鍵を受け付けていない状態」とのこと。以下を確認した。

  • 公開鍵(.pub)を GitHub の SSH keys に登録したか
  • GitHub にログインしているアカウントが目的のアカウントか

どちらも正しかったので再度 ssh -T git@github.com を実行して"yes"をしたら成功した。
サボりはだめですね。

remote を HTTPS → SSH に切り替える

SSH が通る状態になったら、Vault の remote を SSH に変更する。

変更前(HTTPS)

まずは変更前の状況確認のためにターミナルで以下を実行。

git remote -v

結果はこのようにhttpsであると言う表示。

origin  https://github.com/username/my_vault.git (fetch)
origin  https://github.com/username/my_vault.git (push)

変更(SSHへ差し替え)

次のようにターミナルで実行。

git remote set-url origin git@github.com:username/my_vault.git

変更後(SSH)

以下のコマンドをターミナルで実行して、変更できているか確認

git remote -v

すると、きちんとgit@github.com:... に変更されていた。

origin  git@github.com:username/my_vault.git (fetch)
origin  git@github.com:username/my_vault.git (push)

これで「GitHubへの接続先(remote URL)」を HTTPS から SSH に変更できた。

Obsidian Git から push して確認する

Obsidian Git から通常どおり

  • commit
  • push

を実行して、エラーが出ずに GitHub に反映されたので完了

0
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?