Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can anyone explain, why "git status" touches the .git directory?

I currently maintain a project for a git-prompt for bash (https://github.com/magicmonty/bash-git-prompt) and I just got a bug report (https://github.com/magicmonty/bash-git-prompt/issues/97) from someone who works with Docker, who tells me, that everytime he uses the prompt, the cache is invalidated, because the .git directory is constantly touched.

I have looked into this, and found out, that it is the command git status, which touches the .git directory. It seems, that only the directory entry itself and no contents are touched. Can anyone explain, why this is needed, or is this maybe a bug in Git.

Is there a way to show all status info, without touching the .git directory?

Thanks for the help

Update:

Since the whole reason to use the git status command was, to determine the number of untracked files, I replaced it with git ls-files --others --exclude-standard | wc -l, which doesn't need a lock.

like image 693
magicmonty Avatar asked Nov 14 '14 19:11

magicmonty


People also ask

What does git status tell you?

The git status command displays the state of the working directory and the staging area. It lets you see which changes have been staged, which haven't, and which files aren't being tracked by Git. Status output does not show you any information regarding the committed project history.

What is the most likely cause if git status results in the output not a git repository?

Conclusion. The “not a git repository” error is common. The cause is running a Git command in the wrong folder or running a Git command before initializing a Git repository.

How do I remove unchanged files in git?

Remove every file from Git's index. git rm --cached -r .

Does git check remote status?

First use git remote update , to bring your remote refs up to date. Then you can do one of several things, such as: git status -uno will tell you whether the branch you are tracking is ahead, behind or has diverged. If it says nothing, the local and remote are the same.


1 Answers

strace git status shows that this action uses the lock file .git/index.lock, that's why the .git's mtime is updated.

git being cool, it uses the environment variable GIT_INDEX_FILE to decide which lock file to use. If unset, git uses .git/index (this is the default), but if set, git uses its value. From man git:

GIT_INDEX_FILE

This environment allows the specification of an alternate index file. If not specified, the default of $GIT_DIR/index is used.

So:

GIT_INDEX_FILE=banana git status

will not update your .git's mtime.

So you now have to make a decision as whether you want to go along this path or not (which certainly has many caveats).

Good luck!

like image 176
gniourf_gniourf Avatar answered Sep 30 '22 03:09

gniourf_gniourf