How to run git status without modifying .git/index - such as in a PROMPT_COMMAND

Viewed 1579

I've got a PROMPT_COMMAND that shows git branch information using git status, unfortunately when 'git status' is invoked, it appears to be modifying the .git/index file.

This is not good since if you are running with a user that has rights over the target directory (such as on a shared filesystem or if you're running as a privileged account) - it winds up changing the ownership of that file to your user since git appears to be recreating the .git/index file.

Is there any way to run 'git status' that will keep it from modifying the .git/index file?

2 Answers

If your Git version is at least 2.15, run git --no-optional-locks status. See commit 27344d6a6c8056664966e11acf674e5da6dd7ee3, which added this new option.

Otherwise, if you can make sure the index is up to date before running git status, git status won't update it again; but this is kind of tricky. Alternatively, you could temporarily make the index read-only—but this will interfere with other git commands running in parallel—or you could copy the index to a temporary and run git status with GIT_INDEX_FILE set to the path name of the temporary, then remove the temporary. This last is clumsy but will work with historical Git.

(Incidentally, I don't run git status from my own hand-rolled status-constructing bash prompt code, though it was not specifically for this reason. Instead, I run an explicit git update-index, which you could omit at the cost of performance, perhaps based on $PWD.)

Running git status does not modify the index file.

$ ls -l .git/index
-rw-r--r-- 1 adelsbergerm 1049089 445386 Jul 11 15:07 .git/index

$ git status
On branch REDACTED
Your branch is ahead of 'origin/REDACTED' by 211 commits.
  (use "git push" to publish your local commits)
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   REDACTED

no changes added to commit (use "git add" and/or "git commit -a")

$ ls -l .git/index
-rw-r--r-- 1 adelsbergerm 1049089 445386 Jul 11 15:07 .git/index

Your claim that modifying a file would change ownership is also suspect.

I suppose you could have some OS nobody's heard of where reading a file automatically writes to it and writing to a file automatically claims ownership of it, but barring that it would seem these are observation errors and you need to look for the problem elsewhere.

Related