LoginSignup
16
10

More than 5 years have passed since last update.

【Unity】git addしたときのopen("Temp/UnityLockfile"): Permission deniedの原因と対処方法

Posted at

動作確認環境

  • Windows 10
  • Unity 2018.3.1f1 Personal

背景

先日、新規作成したUnityプロジェクトをGitHubにpushしようと思い、git add .しました。
そしたら、下記のようなエラーが出ました。

$ git add .
error: open("Temp/UnityLockfile"): Permission denied
error: unable to index file Temp/UnityLockfile
fatal: adding files failed

どうやら、Unityでプロジェクトを開いているときにだけ存在するTempフォルダが原因のようです。

解決方法

プロジェクトを閉じて、git add.すればいいです。
もしくは、.gitignoreに追記して、Version管理対象外にすれば問題ないです。

なぜこうなってしまったか

自分の場合は、githubのUnity用テンプレートの.gitgnoreを使用していました。
.gitgnoreに書いているファイルおよびフォルダ、ディレクトリは、gitのバージョン管理対象外にすることができます。

.gitignore
[Ll]ibrary/
[Tt]emp/
[Oo]bj/
[Bb]uild/
[Bb]uilds/
Assets/AssetStoreTools*

# Visual Studio cache directory
.vs/

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

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

# Unity3D Generated File On Crash Reports
sysinfo.txt

# Builds
*.apk
*.unitypackage

にもかかわらず、なぜgit add .の対象になってしまったか。
それは、単純に手順が悪かったです。
どういうことかというと、

  1. GitHubで、画面ポチポチして新規リポジトリを作成する。←ここで.gitignoreを作成。
  2. ローカルでUnityプロジェクトを新規作成。
  3. 新規作成したUnityプロジェクト配下でgit init
  4. git add .
  5. git commit -m "first commit"
  6. ここでGitHubのリポジトリと紐付いていないことに気づく。
    git remote add origin https://github.com/<username>/<ripository>.git
  7. git pull origin master してリモートリポジトリと同期する。← ここでローカルに.gitignoreを作成

git add .している時点では、ローカルリポジトリに.gitignoreがありません。
そのため、.gitignoreで管理外に設定しているTempもバージョン管理の対象にしてしまいます。
その結果、Tempも含めて全てaddしてしまったということでした。

終わりに

正直、このエラーがでるまで、自分の手順が間違っていることに気づきませんでした。
個人の勉強用リポジトリなので、管理が雑でした。
ミスって不要なファイルまでpushしていることも気づかず、お恥ずかしい…

16
10
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
16
10