1

Assume I have a structure in master.

d - src 
      - main ...
      - resources ...
  - target
      - xyz

Files xyz should not be tracked, so I added into .gitignore target/* and commit the structure

  • git commit -m 'initial structure'

I create 2 branches

  • git branch t1
  • git branch t2

and switch to t1

  • git checkout t1

I start doing some work, compile stuff, such that target is populated. I commit my changes to t1.

  • git commit -a 't1 specific changes'

then I switch to t2

  • git checkout t2

When I look into target, it is still populated with the files created when branch t1 was active, while I would like to have these 'cleared out'

I found a similar discussion stating that git should behave differently than I see. Git is deleting an ignored file when i switch branches

What am I missing ?

Francis

Zoe
  • 27,060
  • 21
  • 118
  • 148
Francis Martens
  • 3,458
  • 4
  • 24
  • 26
  • I don't think you read that other question carefully enough. The ignored file wasn't actually ignored. It was tracked in one branch but not another, then added to the gitignore but never removed from the repository, so switching branches removed it as it would any tracked content present in only one branch. – Cascabel Jan 20 '11 at 22:02

3 Answers3

2

This is by design. Git will not touch files that it is not tracking in the working folder. If you want to clean the untracked files you can use

git clean -xdf

More commonly, I want to throw away changes that are not tracked but not ignored. This may be because I was experimenting with some code and added a new class file. In this case I would use:

git clean -df

This would leave my output directories as is. I would rely on my development tools to clean these folders instead.

You could use the hook, but that's not a good solution as it is harder to share across a team.

Adam Dymitruk
  • 124,556
  • 26
  • 146
  • 141
  • 1
    I think this is pretty definitely behavior you don't want to automate (e.g. with a hook or alias). Someday you're going to write some code, neglect to check it in, and switch branches... and discover that your "helpful" automatic cleaning just threw it away for you. Nothing to do with whether it's shareable - you just don't want to do it by default. – Cascabel Jan 20 '11 at 22:03
  • Agreed. I'm only half-helping :) – Adam Dymitruk Jan 20 '11 at 23:35
0

You can add a post-checkout hook which cleans ignored files:

git clean -f -X
max
  • 33,369
  • 7
  • 73
  • 84
0

The question you linked is talking about a file that's tracked in one branch and ignored in another. You're talking about generated files that aren't tracked in any branches. Since git isn't tracking them, it won't do anything to them.

jamessan
  • 41,569
  • 8
  • 85
  • 85