LoginSignup
2
1

More than 1 year has passed since last update.

UnityをGitHubでちゃんと管理する

Last updated at Posted at 2021-07-24

UnityをGithubで管理したい。基本は下記リンクに倣って進めていくだけ。

参考:UnityでGitHubとGit LFSを使ったバージョン管理初期設定まとめ

(参考その2:UnityプロジェクトをGit LFSでバージョン管理する 個人的なオススメの設定

手順

上記リンクから、筆者好みにアレンジした手順を以下に記しておく。

  1. Unityでプロジェクト作成
  2. Unityの「Edit」→「Project Settings」で以下を設定

    • Editor / Asset Serialization / Mode ->「Force Text」
    • Version Control / Model ->「Visible Meta Files」
  3. リモートから新規のリポジトリを作成(README gitignore等のチェックは外しとく)

  4. 作成したプロジェクトディレクトリをターミナルから $ git init (必要に応じて $ touch README.md)

  5. Unity用gitignoreのテンプレートをDLし.gitignoreに改名してから同ディレクトリに加える

  6. Unity用gitattributesのテンプレートをDLし.gitattributesに改名してから同ディレクトリに加える

    • Git LFSで管理する場合はbinaryの行をすべてfilter=lfs diff=lfs merge=lfs binaryに更新すること
    • それが面倒ならこっちを使う
  7. Git LFSを使うなら $ git lfs install

  8. $ git add .

  9. $ git commit -m "first commit"

  10. $ git branch -M main

  11. $ git remote add origin git@github.com:[Githubのユーザ名]/[リポジトリ名].git

  12. $ git push -u origin main

つまづいたこと

筆者の環境ではいろいろと厄介なことが起こってしまった。それをメモしておく。

1. masterとmain問題

$ git branch -M mainをしなかったせいでこうなった。

$ git push -u origin main
error: src refspec main does not match any

Githubの仕様変更により、masterは廃止されmainへと置き換えられた…はずであった。
そのバージョン違い?で起こってしまうのがこの食い違い。

  • 生成したリモートリポジトリのブランチ名はmain
  • git initするときにできたブランチ名はmaster
$ git branch
* master  # mainじゃない!

という状況になっている。

$ git branch -m main  # masterをmainにリネーム
$ git config --global init.defaultBranch main  # ついでに今後git initしたときに同じ症状が起こらないようにしておく

こうすれば解決。もう一回pushしてみよう。

参考:Githubのデフォルトブランチがmainになった

2. WSL問題

SSHが通らない問題が発生して、pushできない人。

$ git push -u origin main
The authenticity of host 'github.com (13.114.40.48)' can't be established.
RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'github.com,13.114.40.48' (RSA) 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.

ちなみにsshではなく$ git remote add origin https://github.com/[Githubのユーザ名]/[リポジトリ名].gitでやろうとしても弾かれた。

$ git push -u origin main
Username for 'https://github.com': [Githubのユーザ名]
Password for 'https://[Githubのユーザ名]@github.com':
remote: Invalid username or password.  # パスワード正しくてもこうなる
fatal: Authentication failed for 'https://github.com/[Githubのユーザ名]/[リポジトリ名].git/'

これを機に、以前から逃げ続けてきたGithubのSSH問題にケリをつけることにした。
とりあえず下記リンクに従ってSSHキーを生成する。

GitHubにssh接続できるようにする

リンク先についてのメモ(思ったことの覚え書き)↓

  • 秘密鍵はわざわざid_rsa_githubなんてせず、普通にid_rsaでいい。また、もし既にid_rsaが生成済みならそれを使い回せばOK。(セキュリティ的に大丈夫かは知らん。)
  • WSL使ってる人は、Windows側のSSHキー(C:\Users\[Windowsのユーザ名]\.ssh)とは別に、WSL側のホームディレクトリに別途SSHキーが必要(~/.ssh)。

リンク先の手順に従っていくと、「WSL使ってる」かつ「WSLのホームディレクトリの位置を変更してる」という人は以下のようになってしまう。
(パワーシェル使ってる人と、WSL使っててもホームディレクトリの変更をしてない人は大丈夫なはず。)

$ ssh-add ~/.ssh/id_rsa
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@         WARNING: UNPROTECTED PRIVATE KEY FILE!          @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Permissions 0777 for 'XXX/.ssh/id_rsa' are too open.
It is required that your private key files are NOT accessible by others.
This private key will be ignored.
Load key "XXX/.ssh/id_rsa": bad permissions

どうやら、Windowsのパーミッションの仕様が(WSL視点だと)ガバガバなのが原因らしい。

解決法1

WSLでもgitでsshを使いたい

基本的には上に従えばOK。この手順だと、SSHキーは作り直しになるので注意。
仕組みをよく分かってない人はおとなしく解決法2へレッツゴー。

解決法2

基本的には解決法1とやってることは同じ。
だが、すでに生成したSSHキーをそのまま移行したい場合(筆者はそうだった)は以下のようにする。

参考:[WSL] Windows支配下に公開鍵, 秘密鍵を作ってしまうとpermissionを変更できない!

$ mkdir /home/[WSLのユーザ名]/.ssh
$ mv ~/.ssh/* /home/[WSLのユーザ名]/.ssh/
$ cd  /home/[WSLのユーザ名]/.ssh/
$ chmod 644 id_rsa.pub
$ chmod 644 known_hosts
$ chmod 600 id_rsa
$ chmod 700 ../.ssh/
$ cd ~
$ rmdir .ssh/  # 消せなかったら再起動してリトライ
$ ln -s /home/[WSLのユーザ名]/.ssh 

ここまでできたら、もう一回以下を試してみる。

$ ssh-add ~/.ssh/id_rsa
$ ssh -T github
Hi [Githubのユーザ名]! You've successfully authenticated, but GitHub does not provide shell access.

こうなれば成功。今度こそpushできるはず!

まとめ

WSLってめんどいね。でもパワーシェルは使いたくないから仕方ないね。

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