2
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?

【Git】コミットにSSH鍵で署名する方法

Posted at

はじめに

Windowsで開発する際、コミットに署名を付けたかったので設定方法をまとめました。
本記事ではSSH署名の設定方法を紹介します。

SSHキーの作成

PowerShellを起動して以下のコマンドを実行します。

ssh-keygen -t ed25519

保存先とパスフレーズを設定します。

Generating public/private ed25519 key pair.
Enter file in which to save the key (C:\Users\user\.ssh\id_ed25519): ⇐保存場所を指定(任意)
Created directory 'C:\Users\user\.ssh'.
Enter passphrase (empty for no passphrase): ⇐パスフレーズを入力(任意)
Enter same passphrase again: ⇐パスフレーズを再度入力

.sshディレクトリ内に鍵が生成されました。id_ed25519が秘密鍵、id_ed25519.pubが公開鍵です。

> ls -Name ./.ssh
id_ed25519     # 秘密鍵
id_ed25519.pub # 公開鍵

Gitに秘密鍵を登録

以下のコマンドで、コミット時に秘密鍵で署名するように設定します。

git config --global gpg.format ssh
git config --global user.signingkey C:\Users\{ユーザ名}\.ssh\id_ed25519
git config --global commit.gpgsign true

公開鍵の登録

公開鍵をGitHubに登録します。

「Settings」を選択します。

1.png

「SSH and GPG keys」を選択します。

2.png

「New SSH key」を選択します。

3.png

Titleには任意の名称を設定します。
Key typeは「Signing Key」を選択します。
Keyに公開鍵(id_ed25519.pub)の中身をコピペします。

4.png

全て設定したら「Add SSH Key」を押し、最後にアカウントの認証を行って登録完了です。

パスフレーズの入力を不要にする

秘密鍵にパスフレーズを設定している場合、コミット時に毎回パスフレーズを要求されます。
毎回入力するのは面倒なのでssh-agentを使って入力を不要にします。

PowerShellを管理者権限で起動して、以下のコマンドを実行します。

Get-Service ssh-agent | Set-Service -StartupType Automatic
Start-Service ssh-agent

署名用を秘密鍵をssh-agentに登録します。
ここでパスフレーズを要求されるので入力します。

ssh-add C:\Users\{ユーザ名}\.ssh\id_ed25519

GitにSSHの設定を追加します。

git config --global gpg.ssh.program (Get-Command ssh-keygen.exe).Source
git config --global core.sshCommand (Get-Command ssh.exe).Source

これでログイン中のパスフレーズの入力は不要になります。

コミット

実際にコミットしてGitHub上で履歴を確認します。
「Verified」というマークが付いていたら署名されています。

5.png

参考文献

2
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
2
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?