LoginSignup
7
4

More than 1 year has passed since last update.

Unityプロジェクトをgitでpushする際の注意事項

Posted at

はじめに

1年ほどUnityを触っていなかったのだが、新しくUnity(2021.3.10f1)でProjectを作ってgitHubにpushしようとするとLibrary以下のファイルが100MBを超えているのでpushできなかった。
「え??最近のUnityはまっさらなProjectでもgit lfsを使わないとpushできないの??」 ってなりました。
git lfs : gitは100MB以上のファイルをpushする際にブロックするため、そういった大きなファイルを指定して特別にpushするツール。大きなモデルデータとかpushするときにお世話になった。

ここまで読んでわかっている人は「こいつ何してんだ?」と思うだろうが、自分と同じような初学者()は一緒の戸惑いを持つかもしれない。

そのため、今回は

  • Unity Projectをpushする際の注意事項(自分が把握している情報)
  • コマンドラインでUnityProjectをgitHubにpushする手順
  • 余談()

についてまとめようと思う。
未来の自分が同じ問題で時間を潰さないように、そして同じ疑問を抱いた方々へ少しでも助けになることをお祈りしてます。

  • 使用環境
    MacbookAir(M1, 2020), MacOS Monterey 12.2.1, Unity(2021.3.10f1), git version 2.30.1 (Apple Git-130)

UnityProjectをpushする際の注意事項(自分が把握している情報)

今回の問題というか答えとして、UnityProjectのgit管理ではLibraryはpushしないというか推奨されていない。
なぜか?
ビルド対象を選択するSwitch Platformをすると色々と処理が起きて少し待たされる。
これはLibraryがそのプラットフォームにあわせて書き換えられるため。らしい。
(自分で新たに検証はしていないが、思い当たる経験はあった)

また、gitHubでレポジトリを作成するとき.gitignoreを作成できるがそのTemplateにUnityがある!!
(え?そんな便利なもんあったんや)
以下に示すものがUnity Templateの.gitignoreになる

# This .gitignore file should be placed at the root of your Unity project directory
#
# Get latest from https://github.com/github/gitignore/blob/master/Unity.gitignore
#
/[Ll]ibrary/
/[Tt]emp/
/[Oo]bj/
/[Bb]uild/
/[Bb]uilds/
/[Ll]ogs/
/[Mm]emoryCaptures/

# Asset meta data should only be ignored when the corresponding asset is also ignored
!/[Aa]ssets/**/*.meta

# Uncomment this line if you wish to ignore the asset store tools plugin
# /[Aa]ssets/AssetStoreTools*

# Autogenerated Jetbrains Rider plugin
[Aa]ssets/Plugins/Editor/JetBrains*

# Visual Studio cache directory
.vs/

# Gradle cache directory
.gradle/

# Autogenerated VS/MD/Consulo solution and project files
ExportedObj/
.consulo/
*.csproj
*.unityproj
*.sln
*.suo
*.tmp
*.user
*.userprefs
*.pidb
*.booproj
*.svd
*.pdb
*.mdb
*.opendb
*.VC.db

# Unity3D generated meta files
*.pidb.meta
*.pdb.meta
*.mdb.meta

# Unity3D generated file on crash reports
sysinfo.txt

# Builds
*.apk
*.unitypackage

# Crashlytics generated file
crashlytics-build.properties

こう見てみるとLibrary以外にも多くのものがpush非推奨になっていることがわかる。
(知らんかったぁ、今まで全部pushしてた)

コマンドラインでUnityProjectをgitHubにpushする手順

おまけ的なものでかなりgitの基本について記述するので知っている人は見なくて大丈夫なものになる。
というか、gitHubでレポジトリ作ったら出てくる内容だし...
gitのアカウントとレポジトリを作成しUnityでNew Projectした後、そのディレクトリ(~/UnityProject)で作業をするものとする。

~/UnityProject $ ls
Assets          Library         Logs            Packages        ProjectSettings UnityProject.sln   UserSettings
~/UnityProject $ git init
Initialized empty Git repository in ~/UnityProjects/.git/
~/UnityProject $ git remote add origin  git@github.com:UserName/UnityProject.git
~/UnityProject $ git pull origin main
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), 1.08 KiB | 553.00 KiB/s, done.
From github.com:UserName/UnityProject
 * branch            main       -> FETCH_HEAD
 * [new branch]      main       -> origin/main
~/UnityProject $ git add .
~/UnityProject $ git commit -m "push unity project"
[main 9b245ac] push unity project
 32 files changed, 3504 insertions(+)
....(出力省略)

~/UnityProject $ git push -u origin main
Enumerating objects: 42, done.
Counting objects: 100% (42/42), done.
.....(出力省略)
Branch 'main' set up to track remote branch 'main' from 'origin'.

UserNameやUnityProjectは各自で設定したものとなる。

余談

gitで詰まったというか調べて修正したポイントについて記述する。

warning: LF will be replaced by CRLF

これは改行コードを書き換えようとしていることを警告するもの。
作成したUnityProjectを~/UnityProject $ git add .しようとした時に表示された。
対処方法としては
$ git config --global core.autocrlf false
と設定する。

ただ、--globalで指定しているのにプロジェクトを作り直して$ git initした後に$ git add .とするとまた同じ警告文が表示されるため、毎度入力した。
次は--systemで指定してみようかな?

error: failed to push some refs to

これは$ git push origin mainをしたときに出力される。
エラーの内容を意訳すると

  • 最初に$ git fetchしてみたら?
  • それで$ git pushする前に$ git pullしてみたら?
  • それで無理なら$ git push --helpで詳細を確認してみて?

みたいなことが書かれている。
このエラーの文章に従って対処して解決できれば良いが、自分は$ git push-u オプションをつけると解決した。
$ git push -u origin mainで解決できた。
-u オプションについては$ git push --helpで確認できる。

最後に

今回はUnityゲーム開発者ギルドで質問し得た情報と個人的にgitで詰まったポイントについて簡単にまとめました。
主に未来の自分へのメモ的な意味合いで記事を書いているためわかりにくいところもあると思いますが、ここまで目を通して頂きありがとうございました。

参考文献

  1. Unityゲーム開発者ギルド
  2. git lfs
  3. GitHub
  4. 【解決】The file will have its original line endings in your working directory
7
4
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
7
4