0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

git commitがinsufficient permission for ~で失敗する

Posted at

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
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?