threads / discuss / 21984

How to exclude files from "git diff"

Subject: How to exclude files from "git diff"

## tl;dr

7 messages between Dec 18, 2009 and Jan 5, 2010.

replies: 6people: 5as markdown or json

H.J. Lu· Dec 18, 2009, 17:09 UTC · lore
Hi,

I have some bookkeeping files in my git repository. How do I exclude them from "git diff"? Does "git diff" support

# git diff --exclude="foo.*.bar*"
Please CC me since I am not on the git mailing list.
Thanks.
-- 
H.J.
Jeff King· Jan 5, 2010, 06:45 UTC · re: H.J. Lu · lore

Re: How to exclude files from "git diff"

On Fri, Dec 18, 2009 at 09:09:15AM -0800, H.J. Lu wrote:
> I have some bookkeeping files in my git repository.  How do I
> exclude them from "git diff"? Does "git diff" support
> 
> # git diff --exclude="foo.*.bar*"

No, I don't believe there is a way to do that. You would have to do something like:

  git diff $(git ls-files | grep -v whatever)

The usual concept of "exclusion" for git is not to track files at all via the .gitignore mechanism. Are these files that have content you really _want_ in the repository, but you just don't want to see them when doing some diffs? Or are they files that could not be in the repository at all?

-Peff
H.J. Lu· Jan 5, 2010, 14:20 UTC · re: Jeff King · lore

Re: How to exclude files from "git diff"

On Mon, Jan 4, 2010 at 10:45 PM, Jeff King <peff@peff.net> wrote:
Show 17 quoted lines
> On Fri, Dec 18, 2009 at 09:09:15AM -0800, H.J. Lu wrote:
>
>> I have some bookkeeping files in my git repository.  How do I
>> exclude them from "git diff"? Does "git diff" support
>>
>> # git diff --exclude="foo.*.bar*"
>
> No, I don't believe there is a way to do that. You would have to do
> something like:
>
>  git diff $(git ls-files | grep -v whatever)
>
> The usual concept of "exclusion" for git is not to track files at all
> via the .gitignore mechanism.  Are these files that have content you
> really _want_ in the repository, but you just don't want to see them
> when doing some diffs? Or are they files that could not be in the
> repository at all?
Yes, I want those files in repository. They are for my personal use only.
Thanks.
-- 
H.J.
Dmitry Potapov· Jan 5, 2010, 17:54 UTC · re: H.J. Lu · lore

Re: How to exclude files from "git diff"

On Tue, Jan 5, 2010 at 5:20 PM, H.J. Lu <hjl.tools@gmail.com> wrote:
>
> Yes, I want those files in repository. They are for my personal use only.

If you modified some file locally and do not want to see and commit those modifications, you may want to use:

git update-index --assume-unchanged foo

After that, git will not notice any change to `foo'. So, 'git diff' and any other command (such git commit -a) will ignore those changes.

Warning: using  --assume-unchanged may result in losing your changes
if you switch between branches containing different versions of 'foo'.

Normally, when you try to do that, git will not allow switch to another branch saying that `foo' is modified. But using --assume-unchanged disables this check. But if you have the same `foo' on all branches then switching between branches will preserve your modifications.

Dmitry
H.J. Lu· Jan 5, 2010, 18:15 UTC · re: Dmitry Potapov · lore

Re: How to exclude files from "git diff"

On Tue, Jan 5, 2010 at 9:54 AM, Dmitry Potapov <dpotapov@gmail.com> wrote:
Show 9 quoted lines
> On Tue, Jan 5, 2010 at 5:20 PM, H.J. Lu <hjl.tools@gmail.com> wrote:
>>
>> Yes, I want those files in repository. They are for my personal use only.
>
> If you modified some file locally and do not want to see and commit
> those modifications, you may want to use:
>
> git update-index --assume-unchanged foo
>

I added those files into my repository for bookkeeping purpose. It shouldn't go out at all.

-- 
H.J.
Nguyen Thai Ngoc Duy· Jan 5, 2010, 18:31 UTC · re: H.J. Lu · lore

Re: How to exclude files from "git diff"

On 1/6/10, H.J. Lu <hjl.tools@gmail.com> wrote:
Show 14 quoted lines
> On Tue, Jan 5, 2010 at 9:54 AM, Dmitry Potapov <dpotapov@gmail.com> wrote:
>  > On Tue, Jan 5, 2010 at 5:20 PM, H.J. Lu <hjl.tools@gmail.com> wrote:
>  >>
>  >> Yes, I want those files in repository. They are for my personal use only.
>  >
>  > If you modified some file locally and do not want to see and commit
>  > those modifications, you may want to use:
>  >
>  > git update-index --assume-unchanged foo
>  >
>
>
> I added those files into my repository for bookkeeping purpose. It
>  shouldn't go out at all.
That could be a way to simulate git diff --exclude="foo*.bar*":

git ls-files -- 'foo*bar*'|xargs git update-index --assume-unchanged git diff git ls-files -- 'foo*bar*'|xargs git update-index --no-assume-unchanged

assume that you don't use assume-unchanged for any other purposes
-- 
Duy
Junio C Hamano· Jan 5, 2010, 18:41 UTC · re: H.J. Lu · lore

Re: How to exclude files from "git diff"

"H.J. Lu" <hjl.tools@gmail.com> writes:
Show 13 quoted lines
> On Tue, Jan 5, 2010 at 9:54 AM, Dmitry Potapov <dpotapov@gmail.com> wrote:
>> On Tue, Jan 5, 2010 at 5:20 PM, H.J. Lu <hjl.tools@gmail.com> wrote:
>>>
>>> Yes, I want those files in repository. They are for my personal use only.
>>
>> If you modified some file locally and do not want to see and commit
>> those modifications, you may want to use:
>>
>> git update-index --assume-unchanged foo
>>
>
> I added those files into my repository for bookkeeping purpose. It
> shouldn't go out at all.
Sounds like time to run "rm --cached" to me.  IOW, keep it untracked.

← back to recent threads