git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: Best way to check for a "dirty" working tree?

From
DSDirk Süsserott <newsletter@dirk.my1.cc>
Date
Jun 14, 2011, 13:28 UTC
Message-ID
<4DF761E7.8040707@dirk.my1.cc>
In-Reply-To
<20110613222225.GA14446@elie>
Hi Jonathan,
Am 14.06.2011 00:22 schrieb Jonathan Nieder:
Show 17 quoted lines
> Hi Dirk,
> 
> Dirk Süsserott wrote:
> 
>> I have a script which moves data from somewhere to my local repo and
>> then checks it in, like so:
>>
>> -----------
>> mv /tmp/foo.bar .
>> git commit -am "Updated foo.bar at $timestamp"
>> -----------
>>
>> However, before overwriting "foo.bar" in my working directory, I'd like
>> to check whether my working tree is dirty (at least "foo.bar").
> 
> Interesting example.  Sensible, as long as you limit the commit to
> foo.bar (i.e., "git commit -m ... --only foo.bar")!

Uhh, nice hint. I didn't know that git-commit accepts a path, too. That's safer. However, in my particular case the working tree is either clean or exactly the file in question has changed. If sth. else changes (e.g. my commit-script) I do that in a separate "transaction".

Show 15 quoted lines
> Now, a word of warning.  One aspect of this "do not second-guess the
> caller" behavior is that low-level commands like "git diff-index"
> blindly trust stat() information in the index, rather than going to
> re-read a seemingly modified file and updating the index if the
> content is not changed.  You can see this by running "touch foo.bar";
> "git diff-index" will report the file as changed, until you use "git
> update-index" to refresh the stat information:
> 
> 	git update-index --refresh --unmerged -q >/dev/null || :
> 	if ! git diff-index --quiet HEAD -- foo.bar; then
> 		dirty=1
> 	fi
> 
> Alas, this doesn't seem to be documented anywhere (except for the
> gitcore-tutorial(7))!  It ought to be.

Hmm, it MUST be documented somewhere, because I have several scripts that use "update-index --refresh" to get rid of what I call "phantom changes": sometimes I transfer (scp) files from a remote machine to the local tree. The set of files is already known to Git, so my first guess was that Gitk would only show the "real" diff, but it actually showed *all* transferred files as changed. After running "git status" Gitk does it right and shows only content's diff. Surprisingly, "git status" seems to be a read/write operation and does "update-index --refresh" in the background. After some research I learned about "update-index --refresh" and use it frequently for scp'ed files.

Unfortunately, I cannot remember *where* I learned about it.
> Hope that helps,
> Jonathan

That helped a lot. Thank you, Dirk

Previous: Jonathan Nieder
Message 4 of 4 in “Best way to check for a "dirty" working tree?”
  1. Dirk SüsserottJun 11, 2011
  2. Ramkumar RamachandraJun 12, 2011
  3. Jonathan NiederJun 13, 2011
  4. Dirk SüsserottJun 14, 2011

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.