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

More than 1 year has passed since last update.

【Github】パスワードなしでgit pullしたい

Last updated at Posted at 2023-06-21

システム開発する上で必要不可欠なGit

特にgit pullコマンドは場合によっては1日に何回も打つことになるのですが、毎回パスワード入力を求められてうんざりしていました。

が、最近はちゃんと設定してパスワードなしでgit pullするようにしているので、備忘録としてその方法を記事に残そうかと思います。

①鍵をつくる

まずは鍵を作りましょう。サーバーにSSHして下記コマンドで鍵を作成します。

$ ssh-keygen -t rsa -b 4096 -f ~/.ssh/id_rsa_hogehoge

これで~/.ssh/id_rsa_hogehogeid_rsa_hogehoge.pubが作成されていると思います。

②configファイルを編集する

次にconfigファイルを作ります。なくても動く場合もあるのですが、同一サーバーでテスト環境などの複数プロジェクトが動いている場合はちゃんと指定しないと動かない気がします。

というわけで~/.ssh/configを編集しましょう。ない場合は作ってもらって大丈夫です。

~/.ssh/config
host hogehoge
 hostname github.com
 identityfile ~/.ssh/id_rsa_hogehoge
 user git

③公開鍵をGithubのリポジトリに登録する

次にパスワードなしでgit pullしたいリポジトリに公開鍵を登録しましょう。

公開鍵の登録はブラウザでGithubの対象のリポジトリに遷移し、Settings→Deploy keysから登録できます。

Add deploy keyと書いてあるボタンが画面右上にあると思うので、そこから登録しましょう。
この時

  • Title ・・・ 任意の値
  • Key ・・・ ①で作成した~/.ssh/id_rsa_hogehoge.pubの中身

で入力してください。cat ~/.ssh/id_rsa_hogehoge.pubで表示されるものをKeyに貼り付ければ大丈夫です。

④.git/configを編集する

最後にプロジェクトルートにある.git/configファイルを編集します。
編集箇所はurl = XXXXXXと書いてある部分で、これを下記のように修正します。

.git/config
[remote "origin"]
    # 修正前 httpsから始まるものだと動かない
	# url = https://github.com:ユーザー名/リポジトリ名.git
    # 修正後
	url = hogehoge:ユーザー名/リポジトリ名.git

hogehogeの部分は②で作成した~/.ssh/confighostの値と同じにして下さい。

これでめでたくパスワードなしでgit pullできるはずです!

とても快適になった

というわけでとても開発が快適になりました。めでたしめでたし。

間違っていたらどんどん指摘してもらえればと思います。ありがとうございました!

1
0
2

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