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

Re: What's the use case for committing both the freshly created file and it's exclusion in .gitignore?

From
Jeff King <peff@peff.net>
Date
Aug 9, 2018, 19:58 UTC
Message-ID
<20180809195845.GE32376@sigill.intra.peff.net>
In-Reply-To
<CAAdU=LuNn7qdXf81C3-3=0uh7NOSihm3U3dfDcXPiKxfaZMaFQ@mail.gmail.com>
On Thu, Aug 09, 2018 at 01:04:43PM +0200, Bartosz Konikiewicz wrote:
Show 18 quoted lines
> Hi there!
> 
> I hope that the subject of my message (i.e. the question) is
> exhaustive enough, so I'll just stick to reproducing my issue.
> 
> Steps to reproduce:
> 
> 1. Create a new file.
> 2. Stage the file.
> 3. Add the file to .gitignore.
> 4. Stage the .gitignore.
> 5. Commit changes.
> 
> I imagined that the file would now be removed from the stage (because
> it's ignored now and not yet committed) but it isn't. Where this
> behavior would be desirable? I know that a 'git add' command can be
> invoked with an '-f' flag, which would yield the same result, but I
> can't come up with an explanation where can it be applied.

As far as I know, that is not an intentionally supported workflow. It is merely the result that .gitignore is only considered when adding new files to the index, not when committing nor when updating the entry for an existing file.

If you are asking as a more general case: why do we not complain about .gitignore for files the index already knows about, then I think that is useful. It lets you override the .gitignore _once_ when adding the file initially, and then you don't have to deal with it again (and keep in mind that the pattern excluding it may be broad, like "*.o", or even just "*", so simply deleting it from the .gitignore is not an option).

You could probably accomplish this these days by using a negative pattern in your .gitignore file. But I think the behavior in question may predate negative patterns (but I didn't dig). It's also a bit simpler to use in practice, IMHO.

-Peff
Previous: Bartosz KonikiewiczNext: Jonathan Nieder
Message 2 of 5 in “What's the use case for committing both the freshly created file and it's exclusion in .gitignore?”
  1. Bartosz KonikiewiczAug 9, 2018
  2. Jeff KingAug 9, 2018
  3. Jonathan NiederAug 10, 2018
  4. Bartosz KonikiewiczAug 10, 2018
  5. brian m. carlsonAug 10, 2018

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.