git commitがinsufficient permission for ~で失敗する
SSHを使用している環境でsudo git clone
を無意識に使用してしまい、リポジトリのアクセス権限が適切に設定されていない場合、git操作でエラー発生することがあります。
エラー例
[1] git commitでエラー。
.gitフォルダに書き込み権限なし。
git commit実行
masami@masami-L ~/ecommerce (main)> git commit -m "Initial data was created using the seed file"
error: insufficient permission for adding an object to repository database .git/objects
error: Error building trees
[2] 何も考えずにsudo
を付加して実行。
root ユーザーには Git の user.email と user.name が設定されていない。
masami@masami-L ~/ecommerce (main)> sudo git commit -m "Initial data was created using the
seed file"
[sudo] masami のパスワード:
Author 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: unable to auto-detect email address (got 'root@masami-L.(none)')
[3] もう1回、sudo
を付けずに実行。前回のコマンドで一般ユーザーにroot権限が付き、 .gitフォルダへの書き込みが可能になる。
masami@masami-L ~/ecommerce (main)> git push
Enumerating objects: 19, done.
Counting objects: 100% (19/19), done.
Delta compression using up to 4 threads
Compressing objects: 100% (13/13), done.
Writing objects: 100% (13/13), 7.22 MiB | 646.00 KiB/s, done.
Total 13 (delta 3), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (3/3), completed with 3 local objects.
To https://github.com/pea-sys/ecommerce.git
5ff5c13..8595f38 main -> main
解決方法
sudo を使用して Git 操作を行うのは一般的に推奨されません。
下記操作により、エラーが解決します。
- git ディレクトリの所有権を正しいユーザーに戻す
sudo chown -R yourusername:yourusername .git
- 適切な権限を設定する
chmod -R u+rwX,go-w .git