During make, I create string fields which I embedded in the linked output. Very useful.
Other than a complex sed
/grep
parsing of the git status
command, how can I easily determine if files in the workspace have been modified according to git
?
To determine whether a file has changed, Git compares its current stats with those cached in the index. If they match, then Git can skip reading the file again.
git diff --name-only does the work, but it shows the tracked files only. In order to include the new files, you may (temporary) add all files with git add . and now the git diff --cached --name-only should list all changed files. After that, you may git restore --staged . to un-stage all files.
Add All Files using Git Add. The easiest way to add all files to your Git repository is to use the “git add” command followed by the “-A” option for “all”. In this case, the new (or untracked), deleted and modified files will be added to your Git staging area. We also say that they will be staged.
If you just want a plain “Are there any differences from HEAD?”:
git diff-index --quiet HEAD
If the exit code is 0, then there were no differences.
If you want “What files have changed from HEAD?”:
git diff-index --name-only HEAD
If you want “What files have changed from HEAD, and in what ways have they changed (added, deleted, changed)?”:
git diff-index --name-status HEAD
Add -M
(and -C
) if you want rename (and copy) detection.
These commands will check both the staged contents (what is in the index) and the files in the working tree. Alternatives like git ls-files -m
will only check the working tree against the index (i.e. they will disregard any staged (but uncommitted) content that is also in the working tree).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With