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

Re: [PATCH] stash: Add --clean option to stash and remove all untracked files

From
David Caldwell <david@porkrind.org>
Date
Jun 21, 2011, 01:36 UTC
Message-ID
<4DFFF5AA.4030401@porkrind.org>
In-Reply-To
<20110621003852.GB2050@sigill.intra.peff.net>
On 6/20/11 5:38 PM, Jeff King wrote:
Show 12 quoted lines
> On Mon, Jun 20, 2011 at 04:36:26PM -0700, David Caldwell wrote:
> 
>> The --clean option acts like the normal "git stash save" but also adds all
>> untracked files in the working directory to the stash and then calls "git
>> clean --force --quiet" to restore the working directory to a pristine
>> state.
> 
> Hmm. I think I would call this something like "--untracked", as to me
> the main function is saving those files, not cleaning them afterwards
> (the fact that they are cleaned is really just making the untracked-file
> handling in line with what we do for tracked files; we put the changes
> in the stash and remove them from the working tree).

I see your point but I thought "--clean" was pretty descriptive of how the working dir ended up afterward. Maybe "git stash --everything" (or "--all")?

Show 15 quoted lines
>> "git stash" alone is not enough in this case--it leaves untracked files
>> lying around (configure and automake droppings, for instance) that might
>> mess up a release process that expects everything to be very clean.
> 
> For that workflow, do you actually want the files saved and restored
> via "stash pop"? That is, aren't those untracked files just useless
> cruft that could be regenerated, and you would be just as happy to do:
> 
>   $ git tag release-1.0
>   $ git stash
>   $ git clean
>   $ make release
>   $ git stash pop
>
> and have a pristine state after your pop?

Yes, in that case you are right. My example was poor. I'm more worried about junk that might actually affect the release but isn't auto-generated. I'm usually too wary to run "git clean" because of the random files I have sitting around--todo lists, random patches, new files I haven't added but are sitting around possibly affecting tests, things like that. I want them back at some point, but I want the directory very clean when doing the release just to make sure I have everything properly committed (so I could, for example, detect the new source file that I forgot to add and commit).

> Also, wouldn't you want to "git clean" after your "make release" but
> before your "git stash pop" in case the build creates cruft that is not
> overwritten by your stash pop?

Yes, you'd definitely want the git clean before the git stash pop. I forgot that step.

> Please put your comments on the patch (i.e., anything not destined to go
> into the commit message) below the "---" marker; that helps "git am"
> know which part is which.

Whoops. That was a copy and paste error. Took multiple tries to get git send-email to work ;-).

Show 6 quoted lines
>>   * I used 'find . -name ".git" -prune -o -print' to get a list of all the
>>     files in the working directory. That assumes ".git" is the name
>>     of the repo--is that assumption valid?
> 
> Generally yes, but somebody could do something tricky with GIT_DIR. You
> should be using "git ls-files -o" instead.

Ah, thanks--I didn't know about that command! I was considering using "git status --porcelain", but ls-files looks exactly like what I want.

Show 14 quoted lines
> For that matter, what should this do with gitignored files, like
> generated object files?
> 
>>   * Also, that find command does not respect .gitignore. Should it?
> 
> I'm not sure of the answer to this. I think it would depend on your
> workflow and your project (i.e., is your build system fragile enough
> that you need to get rid of ignored build products between builds, or is
> it OK to leave them, which is more efficient). I would think respecting
> ignore would be a sane default, but I don't know if it should be
> configurable, or have an extra command line option to stash everything.
> 
> If you do want to respect .gitignore, then you can add
> "--exclude-standard" to the "ls-files" command I mentioned above.

The more I think about it the more I think you're right that it should respect .gitignore on the default case and have another option to be really thorough (even if my build system isn't always fragile, it's sometimes nice to be overly cautious just for my own paranoid peace of mind).

Perhaps --clean (or --untracked) respects .gitignore and --all just flat out does everything?

Show 13 quoted lines
>> @@ -86,7 +87,7 @@ create_stash () {
>>  			git read-tree --index-output="$TMPindex" -m $i_tree &&
>>  			GIT_INDEX_FILE="$TMPindex" &&
>>  			export GIT_INDEX_FILE &&
>> -			git diff --name-only -z HEAD | git update-index -z --add --remove --stdin &&
>> +			(git diff --name-only -z HEAD; test -n "$clean" && find . -name ".git" -prune -o -print0) | git update-index -z --add --remove --stdin &&
>>  			git write-tree &&
>>  			rm -f "$TMPindex"
>>  		) ) ||
> 
> When you apply this stash, what does the resulting index look like? Do
> the untracked files remain properly untracked? That might be a good
> thing to double check in the test script.

They do in indeed, but you're right, that would be a good automated test to add.

Thanks, I greatly appreciate the help.
-David
Previous: Jeff KingNext: Andrew Wong
Message 3 of 15 in “stash: Add --clean option to stash and remove all untracked files”
  1. stash: Add --clean option to stash and remove all untracked filesDavid Caldwell, Jun 20, 2011
  2. Jeff KingJun 21, 2011
  3. David CaldwellJun 21, 2011
  4. Andrew WongJun 21, 2011
  5. Johannes SixtJun 21, 2011
  6. Jeff KingJun 21, 2011
  7. Paul EbermannJun 21, 2011
  8. Marc BranchaudJun 21, 2011
  9. Miles BaderJun 22, 2011
  10. Jeff KingJun 22, 2011
  11. Junio C HamanoJun 21, 2011
  12. stash: Add --include-untracked option to stash and remove all untracked filesDavid Caldwell, Jun 24, 2011
  13. Matthieu MoyJun 24, 2011
  14. stash: Add --include-untracked option to stash and remove all untracked filesDavid Caldwell, Jun 25, 2011
  15. Junio C HamanoJun 26, 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.