GitLab でよくある git エラー

gitlab での簡単なエラー。


# 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://gitlab.com/akbrobot/blockelevator.git'
```


以下のように確認すると

```
$ cat .git/HEAD 
ref: refs/heads/master
```

正しいブランチがわかったので以下のようにするとOK

```
$ git push -u origin master
```


# remote: HTTP Basic: Access denied.
```
remote: HTTP Basic: Access denied. The provided password or token is incorrect or your account has 2FA enabled and you must use a personal access token instead of a password. See https://gitlab.com/help/topics/git/troubleshooting_git#error-on-git-fetch-http-basic-access-denied
```
「GitLab で公開鍵認証でSSH接続を行う方法」
https://qiita.com/nanbuwks/items/3769f1aae80d6e100c70
 で対応

# Permission denied (publickey).


```
$ git push origin master
The authenticity of host 'gitlab.com (2606:4700:90:0:f22e:fbec:5bed:a9b9)' can't be established.
ED25519 key fingerprint is SHA256:eUXGGm1YGsMAS7・・・.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'gitlab.com' (ED25519) to the list of known hosts.
git@gitlab.com: Permission denied (publickey).
fatal: Could not read from remote repository.

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

GitLab に SSH キーを登録します。
以下にアクセスします。


https://gitlab.com/-/profile/keys

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/139524/4536c36a-c0dc-5c97-d500-db80f9f8e37f.png)

こちらから、SSH keysを登録します。

既に id_rsa.pub ができていたので

```
$ 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 の内容を貼り付けます。

もし id_rsa.pub が無ければ SSH keys はローカルマシンで以下のようにして作成(以下はUbuntu22.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]-----+
```


#  ! [remote rejected] main -> main (pre-receive hook declined)

```
 ! [remote rejected] main -> main (pre-receive hook declined)
error: failed to push some refs to 'git@gitlab.com:...'
```

ファイルの容量が足りないのかな? 100MBytes 未満にしてみたが解決せず。


色々調べる。
```
$ git remote -v
origin	git@gitlab.com:akbrobot/dojinshi/c99robot.git (fetch)
origin	git@gitlab.com:akbrobot/dojinshi/c99robot.git (push)
```
設定には問題ないみたい。

```


$ git status
On branch main
nothing to commit, working tree clean
```

ローカルでの git 作業も問題ないみたい。
pull したらどうかな?

```
$ git pull origin main
From gitlab.com:akbrobot/dojinshi/c99robot
 * branch            main       -> FETCH_HEAD
fatal: refusing to merge unrelated histories

```
同じエラー。
merge したらいいのかな?

```
$ git merge --allow-unrelated-histories origin/main
CONFLICT (add/add): Merge conflict in README.md
Auto-merging README.md
Automatic merge failed; fix conflicts and then commit the result.
```
README.md がコンフリクトしているみたいですね。ログを見ると・・・

```
$ git log --merge
commit 71a6279da5c43962a59af556d9b97e0c73d1d595 (HEAD -> main)
Author: nanbuwks <nanbuwks+github@nanbu.com>
Date:   Wed May 15 05:01:01 2024 +0000

    1st commit

commit 57f503bcb53683e679de39e1081e89535d824918 (origin/main)
Author: Yoshimasa Kawano <nanbuwks+gitlab@nanbu.com>
Date:   Wed May 15 04:07:19 2024 +0000

    Initial commit

$ git show 57f503bcb53683e679de39e1081e89535d824918
commit 57f503bcb53683e679de39e1081e89535d824918 (origin/main)
Author: Yoshimasa Kawano <nanbuwks+gitlab@nanbu.com>
Date:   Wed May 15 04:07:19 2024 +0000

    Initial commit

diff --git a/README.md b/README.md
new file mode 100644
index 0000000..a40d7c0
--- /dev/null

.
.
.

```
とりあえずローカルの README.md を捨てて

```
$ git rm README.md
rm 'README.md'
```
add/commit はここで必要かな?

```
$ git add .
$ git commit
```
merge して
```
$ git merge --allow-unrelated-histories origin/main
Already up to date.
```
push するとOKになった。

```
$ git push origin main
Enumerating objects: 219, done.
Counting objects: 100% (219/219), done.
Compressing objects: 100% (210/210), done.
Writing objects: 100% (217/217), 39.92 MiB | 16.08 MiB/s, done.
Total 217 (delta 30), reused 0 (delta 0)
remote: Resolving deltas: 100% (30/30), done.
To gitlab.com:akbrobot/dojinshi/c99robot.git
   57f503b..ee96e7b  main -> main
```