Github認証まとめ

# Githubなどで認証情報エラーにつまづく場合

```shell:Terminal
remote: Invalid username or password.
fatal: Authentication failed for 'https://github.com/webmaster-patche/test_cicd_pilot.git/'
```
* この様なエラーが出ると萎えますよね
* この手のエラーは、そもそもユーザーのsshのキー登録を済ませていない場合や、PCユーザーとgitのpush権限のあるユーザーが異なっていたりするケースで発生します。

## Secretを登録しておくパターン

* sshキー登録を済ませていない場合の手法で、PCユーザーとgitのpush権限のあるユーザーが同じ場合にこのパターンが有効です。（異なる場合、このパターンでは認証エラーが起きます）

```shell:Terminal
webmaster-patche-no-MacBook-Pro:~ webmaster-patche$ ssh-keygen -t rsa -b 4096 -C "%mail_address%"
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/webmaster-patche/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /Users/webmaster-patche/.ssh/id_rsa.
Your public key has been saved in /Users/webmaster-patche/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:ku2lqNzzS5+******************************** 
%mail_address%
The key's randomart image is:
+---[RSA 4096]----+
|  ..oo==+=o.o. .o|
|   + .==+ ..  o.*|
|    o oo .    E=+|
|     o o       ..|
|      = S .      |
|     o + o       |
|      o + .      |
|   . o.o o +     |
|    o .=*o*o.    |
+----[SHA256]-----+
webmaster-patche-no-MacBook-Pro:~ webmaster-patche$ pbcopy < ~/.ssh/id_rsa.pub
```

![スクリーンショット 2020-07-14 4.39.52.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/179927/ed3fa526-595a-0b16-1623-8f80eaa90363.png)
Settings > Secretsを開きます

![スクリーンショット 2020-07-14 3.34.00.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/179927/54fb65e1-e506-6c22-12b2-9c320348aa9b.png)
New secretボタンを押下し、pbcopyしたキー情報を登録します

![スクリーンショット 2020-07-14 3.34.28.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/179927/a67386c1-858b-7f6a-2474-45f6451e2caa.png)
Nameには分かりやすい規則を、Valueにはpbcopyキー情報を入力します

![スクリーンショット 2020-07-14 4.44.27.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/179927/792795e5-08e6-c193-15e7-63f0fcfe0b3b.png)

![スクリーンショット 2020-07-14 4.46.21.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/179927/6ddeff67-7ab7-93b2-8c36-9b4d3bfb3a8a.png)
これで設定完了です


## 認証情報をgit/configに登録しておくパターン

```shell:Terminal
webmaster-patche-no-MacBook-Pro:capistrano_sample_app_v1 webmaster-patche$ cat .git/config 
[core]
	repositoryformatversion = 0
	filemode = true
	bare = false
	logallrefupdates = true
	ignorecase = true
	precomposeunicode = true
[remote "origin"]
	url = https://github.com/webmaster-patche/capistrano_sample_app_v1.git
	fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
	remote = origin
	merge = refs/heads/master

webmaster-patche-no-MacBook-Pro:smocca_v3_ webmaster-patche$ vi .git/config 
[core]
	repositoryformatversion = 0
	filemode = true
	bare = false
	logallrefupdates = true
	ignorecase = true
	precomposeunicode = true
[remote "origin"]
	url = https://%user_name%:%password%@github.com/webmaster-patche/capistrano_sample_app_v1.git
	fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
	remote = origin
	merge = refs/heads/master
```
なお、[user]情報が無く、追加したい場合は以下のコマンドを実行します

```shell:Terminal
webmaster-patche-no-MacBook-Pro:capistrano_sample_app_v1 webmaster-patche$ git config --local user.name webmaster-patche
webmaster-patche-no-MacBook-Pro:capistrano_sample_app_v1 webmaster-patche$ git config --local user.email webmaster.patche@gmail.com

webmaster-patche-no-MacBook-Pro:capistrano_sample_app_v1 webmaster-patche$ cat .git/config 
[core]
	repositoryformatversion = 0
	filemode = true
	bare = false
	logallrefupdates = true
	ignorecase = true
	precomposeunicode = true
[remote "origin"]
	url = https://%user_name%:%password%@github.com/webmaster-patche/capistrano_sample_app_v1.git
	fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
	remote = origin
	merge = refs/heads/master
[user]
	name = webmaster-patche
	email = webmaster.patche@gmail.com
```

### 番外編：authorを変える

* 認証情報をgit/configに登録しておくパターンの場合、userとauthorが一緒になるとは限りません。
* この場合、authorを指定することで解消できます

```shell:Terminal
webmaster-patche-no-MacBook-Pro:hoge_repository webmaster-patche$ git add .
webmaster-patche-no-MacBook-Pro:hoge_repository webmaster-patche$ git commit --amend --author="webmaster-patche <webmaster.patche@gmail.com>"
[feature/development_yyyyMMdd 594e75b20] 修正
 Date: Tue Jul 15 13:24:20 2020 +0900
 2 files changed, 2 insertions(+), 2 deletions(-)
webmaster-patche-no-MacBook-Pro:hoge_repository webmaster-patche$ git push -f origin feature/development_yyyyMMdd
Enumerating objects: 15, done.
Counting objects: 100% (15/15), done.
Delta compression using up to 8 threads
Compressing objects: 100% (8/8), done.
Writing objects: 100% (8/8), 634 bytes | 634.00 KiB/s, done.
Total 8 (delta 7), reused 0 (delta 0)
remote: Resolving deltas: 100% (7/7), completed with 7 local objects.
To https://github.com/webmaster-patche/hoge_repository.git
 + 1332gl234...521e75b10 feature/development_yyyyMMdd -> feature/development_yyyyMMdd (forced update)
```
これでlogを確認してみてください。