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?

GitLab.comからgit cloneしたいんだけどアカウントが2つあり、しかしSSH鍵を1passwordで管理していてssh-agentに2つの鍵が登録されていて、git cloneできない場合の対応法

Last updated at Posted at 2025-04-29

毎度、ググっても出てこない小ネタを取り扱っております。
本記事は個人的な見解であり、筆者の所属するいかなる団体にも関係ございません。

0. はじめに

GitLabからgit clone済みのリポジトリがありました。
そのリポジトリで git pull すると以下のエラーが出ます。

はて?git cloneできたのだからpullだってできそうな気もしますが?

$ git pull
remote: 
remote: ========================================================================
remote: 
remote: ERROR: The project you were looking for could not be found or you don't have permission to view it.

remote: 
remote: ========================================================================
remote: 
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

と思って調べたら、SSH鍵管理を鍵ファイルでの管理から 1passwordでの管理に変更していました。1Passwordで管理すると自動的にssh-agentに鍵が登録されるようになります。
以下、参照

image.png

さらに、gitlab.comには2つのアカウントがあり、それぞれ別々のSSH鍵が登録されています。
image.png

さらにGitLabのリポジトリも、アカウント毎にアクセスできる権限が違うのです。
もし、「リポジトリX」へgit pullしたときにssh-agentの「アカウントA」のSSH鍵で接続すれば問題ありません。
しかし、「リポジトリX」へssh-agentの「アカウントB」のSSH鍵で接続すると、権限がなくて最初のエラーになります。
ssh-agentに登録されている鍵を総当たりでgit pullしてくれれば良いのですが、そういうことはなく、1回マッチさせてエラーになったら別の鍵でリトライはしません。
と、このように接続できるリポジトリとできないリポジトリが出てきてしまいます。

つまり、git pull でエラーになるのは、ssh-agentの鍵が間違っていて、リポジトリへの接続で弾かれていた、ということになります。

0.5 一般的な対策

一般的な対策としては、ssh-agentに登録されている鍵を削除して、必要な鍵だけ登録する。みたいなことになります。
しかし、1Passwordで管理していると自動的にssh-agentに鍵が登録されてしまうので削除することもできません。

一般的には鍵を .ssh/config に書ければ良いのですが(下記Qiita記事参照)、1Passwordで管理しているのでこれもできません。

1. どうすればいいか?

ググったら出てきて、afsshというのを使えばよいようです。

SSHエージェント転送を使いつつ複数のGitHubアカウントとSSH鍵を使い分ける

2. ssh-agent-filterのインストール

sudo apt install ssh-agent-filter

3. git cloneする

git clone \
  --config user.name="あなたのお名前" \
  --config user.email="メールアドレス" \
  --config core.sshcommand="afssh --comment <1passwordに登録されている鍵名> --" \
  git@gitlab.com:<アカウント名>/<リポジトリ名>.git

うまく、Cloneできたようです。

image.png

4. .git/config はどうなるか

以下のようになっていました。
sshcommand を見ると分かりますが、 afssh(agent-filter ssh)で 1Passwordに登録されている鍵 だけを使ってSSH接続しているようです。

[core]
	repositoryformatversion = 0
	filemode = true
	bare = false
	logallrefupdates = true
	sshcommand = afssh --comment <1passwordに登録されている鍵名> --
[user]
	name = "<あなたのお名前>"
	email = "<メールアドレス>"
[remote "origin"]
	url = git@gitlab.com:<アカウント名>/<リポジトリ名>.git
	fetch = +refs/heads/*:refs/remotes/origin/*
[branch "main"]
	remote = origin
	merge = refs/heads/main

5. まとめ

自分向けの備忘録と .git/config がどうなるか知りたかったので書いてみました。

5-1. 補足

1Passwordでは、.ssh/config で複数のエントリーを追加して対応しているようです。

【備忘録】1Passwordでの複数Githubアカウント利用時のSSHキー管理方法 #GitHub - Qiita

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?