LoginSignup
0
1

GitHub でよくあるトラブル対応の備忘録

Last updated at Posted at 2022-11-05

GitHub で push エラー。そのたびごとにオペレーションを確認しているので、ぱっと使えるように備忘録。

password authentication was removed

$ git push origin main
.
.
.
remote: Support for password authentication was removed on August 13, 2021.
remote: Please see https://docs.github.com/en/get-started/getting-started-with-git/about-remote-repositories#cloning-with-https-urls for information on currently recommended modes of authentication.
fatal: Authentication failed for .....

今の設定

$ git config --list
user.email=nanbuwks+github@nanbu.com
user.name=nanbuwks
credential.helper=store --file =/.git_credentials
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
remote.origin.url=https://github.com/nanbuwks/AKBONE2022.git
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
branch.main.remote=origin
branch.main.merge=refs/heads/main
  • アクセストークンを利用する方法
  • 公開鍵認証でSSH接続を行う方法

運用にめんどくさくない公開鍵認証でSSH接続を行う方法を設定することにします。
以下にアクセスします。

https://github.com/settings/keys
こちらから、SSH keysを登録します。
image.png

SSH keys はローカルマシンで以下のようにして作成します。(以下はUbuntu20.04/22.04で実験)

$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/nanbuwks/.ssh/id_rsa): 
/home/nanbuwks/.ssh/id_rsa already exists.
Overwrite (y/n)? y
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/nanbuwks/.ssh/id_rsa
Your public key has been saved in /home/nanbuwks/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:bjw9NphLCem2iuasOx5CxSGyqCD5a7xLWNyF9pA1AYo nanbuwks@nanbuwks-ThinkPad-X230
The key's randomart image is:
+---[RSA 3072]----+
|.. ...+.         |
|o+o..+ .         |
|E .o= .          |
|+o.o + .         |
|..+ . + S        |
|.+ . . + =       |
|o.=   o X =      |
|o+oo . + + o     |
|+B*.... .        |
+----[SHA256]-----+
$ ls ~/.ssh/ -alh
合計 32K
drwx------  2 nanbuwks nanbuwks 4.0K 10月 24 16:01 .
drwxr-xr-x 82 nanbuwks nanbuwks 4.0K 11月  5 13:51 ..
-rw-------  1 nanbuwks nanbuwks 2.6K 11月  5 15:18 id_rsa
-rw-r--r--  1 nanbuwks nanbuwks  585 11月  5 15:18 id_rsa.pub
-rw-r--r--  1 nanbuwks nanbuwks 1.6K 10月 29 13:03 known_hosts

できた ~/.ssh/id_rsa.pub の内容を貼り付けます。
image.png

GitHubにログオンし、右上のアイコンを押します。
image.png

登録後、アクセスするレポジトリから SSH のアドレスをコピーします。
image.png

新しくcloneする場合は

$ git clone <アドレス> 

とします。

既に clone したものを操作する場合は、
.git/config
のアドレスを変更します。
例:


 	url = https://github.com/nanbuwks/AKBONE2022.git

 	url = git@github.com:nanbuwks/AKBONE2022.git

! [rejected] main -> main (fetch first) (リモートとローカルの競合が無い場合)


$ git status
ブランチ main
このブランチは 'origin/main' よりも1コミット進んでいます。
  (use "git push" to publish your local commits)

nothing to commit, working tree clean

今回は、リモートとローカルの競合がないことがわかっていたのでそのままマージします。

$ git pull origin
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 3 (delta 1), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), 1009 bytes | 1009.00 KiB/s, done.
From github.com:nanbuwks/AKBONE2022
   c27dfe4..fc04f46  main       -> origin/main
 * [new tag]         alpha      -> alpha
Merge made by the 'recursive' strategy.
 README.md | 11 +++++++++++
 1 file changed, 11 insertions(+)

改めて push します。

$ git push origin main
Enumerating objects: 26, done.
Counting objects: 100% (20/20), done.
Delta compression using up to 4 threads
Compressing objects: 100% (12/12), done.
Writing objects: 100% (12/12), 607.34 KiB | 2.91 MiB/s, done.
Total 12 (delta 6), reused 0 (delta 0)
remote: Resolving deltas: 100% (6/6), completed with 5 local objects.
To github.com:nanbuwks/AKBONE2022.git
   fc04f46..21fe895  main -> main

! [rejected] main -> main (non-fast-forward) (リモートとローカルの競合がある場合)


To github.com:nanbuwks/oxzbox.git
 ! [rejected]        main -> main (non-fast-forward)
error: failed to push some refs to 'github.com:nanbuwks/oxzbox.git'

git pull してから改めて `git push`し直す。

git pull で以下のエラーが出たときは

$ git pull
remote: Enumerating objects: 9, done.
remote: Counting objects: 100% (9/9), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 5 (delta 2), reused 5 (delta 2), pack-reused 0
Unpacking objects: 100% (5/5), 906 bytes | 82.00 KiB/s, done.
From github.com:nanbuwks/oxzbox
   2cdd1b7..cba614f  main       -> origin/main
There is no tracking information for the current branch.
Please specify which branch you want to merge with.
See git-pull(1) for details.

    git pull <remote> <branch>

If you wish to set tracking information for this branch you can do so with:

    git branch --set-upstream-to=origin/<branch> main


別のエラーが出ました。

この場合、以下のようにすればうまくいったみたいです。

$ git pull origin main
From github.com:nanbuwks/oxzbox
 * branch            main       -> FETCH_HEAD
Removing images/Photos-001.zip
Merge made by the 'recursive' strategy.
y HowToUse.md           |   2 +-
 README.md             |  32 ++++++++++++++++++++++++++++++++
 images/Photos-001.zip | Bin 67928801 -> 0 bytes
 3 files changed, 33 insertions(+), 1 deletion(-)
 delete mode 100644 images/Photos-001.zip

error: src refspec main does not match any

$ git push origin main

とすると

error: src refspec main does not match any
error: failed to push some refs to 'https://github.com/nanbuwks/ardublock.git'

と出た。

pull しようとしても以下のようになるので

$ git pull origin main
fatal: couldn't find remote ref main

ブランチ名を確認すると

$ git branch -a
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/master

ということだったので、以下のようにして対応

$ git push origin master

fatal: Need to specify how to reconcile divergent branches.

$ git pull
hint: You have divergent branches and need to specify how to reconcile them.
hint: You can do so by running one of the following commands sometime before
hint: your next pull:
hint: 
hint:   git config pull.rebase false  # merge (the default strategy)
hint:   git config pull.rebase true   # rebase
hint:   git config pull.ff only       # fast-forward only
hint: 
hint: You can replace "git config" with "git config --global" to set a default
hint: preference for all repositories. You can also pass --rebase, --no-rebase,
hint: or --ff-only on the command line to override the configured default per
hint: invocation.
fatal: Need to specify how to reconcile divergent branches.

pull --rebase のつもりで pull してしまう事故を防止するためにこのような動作が起こるらしいです。
デフォルトのマージでいい場合は、


$ git config pull.rebase false

としてから

$ git pull

します。

また、別のパターン:

$ git push -u origin main
error: src refspec main does not match any
error: failed to push some refs to 'github.com:nanbuwks/2024NotoTopographicSurvey.git'

と出て、

$ git pull
There is no tracking information for the current branch.
Please specify which branch you want to merge with.
See git-pull(1) for details.

    git pull <remote> <branch>

If you wish to set tracking information for this branch you can do so with:

    git branch --set-upstream-to=origin/<branch> main

となったとき。

これは、レポジトリを作った最初の操作でこのようにしていた。

$ echo  # 2024NotoTopographicSurvey > README.md
$ git add .
$ git commit -m "first commit"
ブランチ master

最初のコミット

nothing to commit (create/copy files and use "git add" to track)



echo の後ろがコメントになっているのに気がついてなかった。
ちゃんと

$ echo  '# 2024NotoTopographicSurvey' > README.md

としてやり直したらOK

ローカルブランチ名を master から main に変更したい

昔のレポジトリの設定を変更したいとき

$ git branch -M main

ローカルで作ったソースコードを github に登録する

  1. github でレポジトリを作る

image.png

と出てくるので、

...or create a new repository on the command line

の通り以下のようにやってみるが

$ git init
$ git add .
$ git commit -m "2016 version"
$ git branch -M main
$ git remote add origin https://github.com/nanbuwks/orbitcalc-book.git
$ git push -u origin main

のところで以下のようにエラーになる

Username for 'https://github.com': nanbuwks
Password for 'https://nanbuwks@github.com': 
remote: Support for password authentication was removed on August 13, 2021.
remote: Please see https://docs.github.com/en/get-started/getting-started-with-git/about-remote-repositories#cloning-with-https-urls for information on currently recommended modes of authentication.
fatal: Authentication failed for 'https://github.com/nanbuwks/orbitcalc-book.git/'

当記事の冒頭に「# password authentication was removed」 での対処を既に行っているので、公開鍵認証でSSH接続を行うための、Authentication Key を GitHub に登録する作業は済ませてある。

なので .git を削除して

$ rm -rf .git/

最初からやり直し

$ git init
$ git add .
$ git commit -m "2016 version"
$ git branch -M main
$ git remote add origin git@github.com:nanbuwks/orbitcalc-book.git
$ git push -u origin main

これで登録できた

does not appear to be a git repository

$ git push origin main
fatal: 'git@github.com/nanbuwks/hogehoge.git' does not appear to be a git repository
fatal: Could not read from remote repository.

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

.git/config
のアドレスを変更したときに、間違っていた
今回は
(誤)

 	url = git@github.com/nanbuwks/hogehoge.git

(正)

 	url = git@github.com:nanbuwks/hogehoge.git

fatal: detected dubious ownership in repository at ~

# git add .
fatal: detected dubious ownership in repository at '/mnt/rpi_multichannel_ws281x'
To add an exception for this directory, call:

	git config --global --add safe.directory /mnt/rpi_multichannel_ws281x

www-data を owner にしていたファイルシステムを root で操作していたため。

出てきたアドバイスのとおりにするか、以下のようにして解消。


# git config --global --add safe.directory "*"

Auther identity unknown

Auther identity unknown

*** Please tell me who you are.

Run

  git config --global user.email "you@example.com"
  git config --global user.name "Your Name"
  
to set your account's default identity.
Omit --global to set the identity only in this repository.

fatal: empty ident name ( for <hogehoge@fugafuga>) not allowed

となったときは、

$ git config --global user.email "somebody@example.net"
$ git config --global user.name "hoge fuga"

"somebody@example.net""hoge fuga" は自分のメールアドレスと名前を入れること。GitHub に登録してあるものがいいかな。

clone でエラー

# git clone git@github.com:nanbuwks/qiita2review.git
Cloning into 'qiita2review'...
The authenticity of host 'github.com (140.82.113.4)' can't be established.
ED25519 key fingerprint is SHA256:+DiY3wvvV6TuJJhbpZisF/zLDA0zPMSvHdkr4UvCOqU.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'github.com' (ED25519) to the list of known hosts.
git@github.com: Permission denied (publickey).
fatal: Could not read from remote repository.

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

上述 の 「password authentication was removed」での「公開鍵認証でSSH接続を行う方法」
を設定します。

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