Search code examples
gitbashcommand-promptfile-permissions

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


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?


Solution

  • 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.)