Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's an easy way to detect modified files in a Git workspace?

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?

like image 832
Jamie Avatar asked Oct 07 '10 14:10

Jamie


People also ask

How does git detect changed files?

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.

Where is locally modified files git?

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.

How do I add modified files to github?

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.


1 Answers

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

like image 90
Chris Johnsen Avatar answered Oct 12 '22 04:10

Chris Johnsen