【Git】Obsidian を GitHub と連携する

Obsidian では、クラウド連携は有料ですが、

GitHub リポジトリと連携してバックアップ保存できるらしいです。

これを、

こうすることができる。

GitHub と連携しておけば、複数の端末からアクセスできますし。

 

🧑🏻‍💻 初期設定

Git のプラグインを入れるだけなんです。これが。

👉 「Obsidian Git」を導入してみた。|devlive

分かりづらかったのは、

「Vault」という Obsidian で利用するディレクトリに

きちんと

「Git のローカルリポジトリディレクトリを設定しておく」

ということ。

あとは、そのディレクトリを以下画面が表示されたときに Open するだけ。

 

🧑🏻‍💻 Git ローカルリポジトリの作成と設定

ざっくり3つある。

リモートリポジトリを clone する場合。


$ git clone git@github-superman:org1/project1.git /path/to/project1
$ cd /path/to/project1
$ git config user.email "superman@example.com"
$ git config user.name  "Super Man"

すでに、リポジトリを設定している場合。
ssh 設定のあとに origin URL を変更する必要があります。


$ cd /path/to/project2
$ git remote set-url origin git@github-superman:org2/project2.git
$ git config user.email "superman@example.com"
$ git config user.name  "Super Man"

ローカルからリポジトリを設定、PUSH する場合。


$ cd /path/to/project3
$ git init
$ git remote add origin git@github-superman:org3/project3.git
$ git config user.email "superman@example.com"
$ git config user.name  "Super Man"
$ git add .
$ git commit -m "Initial commit"
$ git push -u origin master

👉 Using multiple github accounts with ssh keys
👉 【Xcode】ローカルと GitHub のソース管理連携設定

 

🧑🏻‍💻 まとめ

- Git プラグインを利用する。
- Git は Obsidian の GUI 上から操作可能。
- git clone してそれを Obsidian で Open するのが簡単。

【参考】👉 GitHub + SSH で複数アカウント切替え


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 git@github.com:OWNER/REPOSITORY.git

# 確認
git remote -v
# Verify new remote URL
> origin git@github.com:OWNER/REPOSITORY.git (fetch)
> origin git@github.com:OWNER/REPOSITORY.git (push)

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

🧑🏻‍💻 手順

1. 鍵をPC側で作成。


❯ ssh-keygen -t ed25519 -C your-main@email.com -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 と連携する


【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 はこちら。