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 add で "fatal: Unable to create '.git/index.lock': Permission denied" になるときの対処法

Last updated at Posted at 2025-10-21

Git などのバージョン

.bash
git --version
git version 2.51.0

nginx -v
nginx version: nginx/1.28.0

起きたこと

Nginx のドキュメントルートにある WordPress のプロジェクトに対して git add . を実行したら fatal: Unable to create '/usr/share/nginx/html/wordpress/.git/index.lock': Permission denied と表示される。

.bash
cd /usr/share/nginx/html/wordpress

pwd
/usr/share/nginx/html/wordpress

# init は実行できる
git init
hint: Using 'master' as the name for the initial branch. This default branch name
hint: is subject to change. To configure the initial branch name to use in all
hint: of your new repositories, which will suppress this warning, call:
hint:
hint:   git config --global init.defaultBranch <name>
hint:
hint: Names commonly chosen instead of 'master' are 'main', 'trunk' and
hint: 'development'. The just-created branch can be renamed via this command:
hint:
hint:   git branch -m <name>
hint:
hint: Disable this message with "git config set advice.defaultBranchName false"
Initialized empty Git repository in /usr/share/nginx/html/wordpress/.git/

# git branch が実行できない
git branch
fatal: detected dubious ownership in repository at '/usr/share/nginx/html/wordpress'
To add an exception for this directory, call:

        git config --global --add safe.directory /usr/share/nginx/html/wordpress

# 所有者を確認
ls -ld .
drwxr-xr-x 6 nginx nginx 4096 Oct 20 14:58 .

# fatal の警告メッセージにあるプロンプト通り実行 ( 別のユーザーによる git 実行を許可 )
git config --global --add safe.directory /usr/share/nginx/html/wordpress

# git config --global を実行したあとは、警告が消える
git branch

# git add が実行できない ( Permission denied )
git add .
fatal: Unable to create '/usr/share/nginx/html/wordpress/.git/index.lock': Permission denied

原因 :

wordpress ディレクトリ (対象のディレクトリ ) に 所有者以外のユーザーが .git を追加しようとすると、セキュリティ上危ないので拒否される。

捕足

  • git add . について :

updates the index with current working tree content, preparing it for the next commit

次回のコミットのために、あらかじめインデックスを現在作業中のツリーの内容に更新しておくものです

  • git config --global --add safe.directory について:

git は実行時にディレクトリの所有者と、実行アカウントが違うと、fatal: detected dubious ownership になって実行できない。git config --global --add safe.directory
を一時的に実行し、この仕様を回避することが可能。

うまくいった方法

他の人も言っているように、ディレクトリの所有者とグループをカレントユーザー ( 現在シェルにログインしているユーザー) のものに変更すると、無事 git add . を正常に実行できるようになりました。

.bash
# カレントユーザーを表示
whoami
hirame-king

# git を実行したいディレクトリの所有者とグループをカレントユーザーに変更
sudo chown -R hirame-king:hirame-king /usr/share/nginx/html/wordpress

# 変更した所有者及びグループの権限を確認
ls -ld /usr/share/nginx/html/wordpress
drwxr-xr-x 6 hirame-king hirame-king 4096 Oct 21 09:49 /usr/share/nginx/html/wordpress

# .git ファイルの所有者とグループも。 (chown -R)
ls -ld /usr/share/nginx/html/wordpress/.git
drwxr-xr-x 6 hirame-king hirame-king 4096 Oct 21 13:32 /usr/share/nginx/html/wordpress/.git

git add .

他にためしたこと

git add . を正常に実行したくてやったこと。

.bash
# 1. 排他制御で被っているindex(.lock) ファイルが親ディレクトリ (/usr/share/nginx/html/) 配下にあるかどうか調べる

sudo find /usr/share/nginx/html/ -path *index*
    # index(.lock) ファイルはすでにない
    # あるいは他の階層にない

# 2. '.git' を強制削除

sudo rm -rf .git

git config --global --add safe.directory /usr/share/nginx/html/wordpress
    # 'detected dubious ownership' のエラーを ( 一時的に) 回避

git init && git add .    # もう一回トライ
/usr/share/nginx/html/wordpress/.git: Permission denied
    # 'git add' が実行できない ( Permission denied )

# 3. 所有者を変更
sudo chown hirame-king:nginx /usr/share/nginx/html/wordpress
[sudo] password for hirame-king:
    # ディレクトリの所有者 ( 'hirame-king' ) を現在のログインユーザーに設定

hirame-king@xxx:/usr/share/nginx/html/wordpress$ git add .
    # もう一回トライ
fatal: Unable to create '/usr/share/nginx/html/wordpress/.git/index.lock': Permission denied
    # 'git add' が実行できない ( Permission denied )

補足情報:

index.lock について

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?