5

Here is my problem (via example steps):

  • I have a Git repo with two different branches (branchA and branchB), which are currently identical.
  • Checkout to branchB and create a new file "foo.txt".
  • Add that file to ".gitignore" of that branch, so that it is successfully ignored in branchB.
  • Create another file "hello.txt", stage it and commit it.
  • Checkout to branchA.
  • foo.txt appears in branchA, but hello.txt does not.

Why does foo.txt appear in branchA and not get "hidden" like hello.txt does?

From my perspective this is a great annoyance and seems like a bug/feature request, but has Git been designed to act this way? I was surprised to find no other people complaining about this.

freshquiz
  • 51
  • 3

3 Answers3

4

It is not a bug. To understand how it works, you need to know a few things.

  • .gitignore is not "automatically" applied globally. You need to commit it.
  • Any uncommitted files will be visible in every branch. When switching branches, you should either commit the file, or you could stash it.

Also, this question has been asked before, though I don't blame you for not finding it in the search. Working on two unrelated files in separate git branches

Community
  • 1
  • 1
Masked Man
  • 1
  • 7
  • 40
  • 80
  • I had a feeling it wasn't a bug and I'm sorry I didn't find it before. Can you see how it might be annoying though? I have files I don't want to be committed (because they are OS specific), but I don't want them in all my branches. I have never used stashing before, but from what a friend told me it doesn't seem like the ideal solution, seems a little hacky, but I appreciate your advice though (I will give it a go anyway). – freshquiz Dec 07 '12 at 11:43
  • After reading the answer from the post you linked, you can see that Christopher seems to be thinking along the same lines as I am about stashing. I reckon this deserves the title of a feature request for Git. I'm trying to think of reasons why you wouldn't want the ignored files to disappear/reappear with checkouts and I can't really think of any. – freshquiz Dec 07 '12 at 11:51
  • @freshquiz I know it can be a bit annoying. You do not _have_ to `stash`, you could always use `commit`. Be aware that your `commit` only updates your local clone and you can always undo your commit when you come back to the branch later. Also, don't worry about not finding the previous question. Searching doesn't always work perfectly. :) – Masked Man Dec 07 '12 at 11:51
  • True, but I don't like the idea of committing files that are binary/OS specific and hence should be ignored. Undoing commits gives me headaches and I can't afford to lose my work so cheaply. Don't you think it would be a good feature request? – freshquiz Dec 07 '12 at 11:53
  • I certainly wouldn't mind having that feature, and I also agree that binary files, etc. shouldn't be committed (even locally). You could add those to the `.gitignore` and see if that helps. – Masked Man Dec 07 '12 at 11:56
  • I might just submit it as a request then. I have added them to my .gitignore file(s), but doesn't solve my problem. When I added them to my .gitignore, I genuinely expected my problem not to exist (i.e. the feature to be implemented), that's how logical it seems to me. – freshquiz Dec 07 '12 at 11:58
  • Yes, please do submit the request. – Masked Man Dec 07 '12 at 11:58
  • Haven't researched where to do that yet, but to save me the time, would you have any idea where I would go to do that? – freshquiz Dec 07 '12 at 12:01
1

foo.txt is not appearing in branchA, it's just no longer being ignored because your changes to .gitignore (I assume you committed these to branchB) are lost when you checked out branchA. foo.txt is just an untracked file. Having foo.txt "disappear" when you switch to branchA is saying you want the file deleted from your working directory. This very different than ignoring a file.

You can specify ignore patterns that affect the whole repository regardless of what branch you have checked out using the $GIT_DIR/info/exclude file. This is local to your repository though, so ignore patterns specified in this file will not be propagated to other repositories. See gitignore(5).

Chris
  • 451
  • 2
  • 5
0

The quick answer:

  • hello.txt is not "hidden", it has been committed to branchB and therefore you will not see in branchA untill you merge or rebase branchB into branchA
  • the same happens to .gitignore. If you committed .gitignore to branchB (which I guess you did) and then checkout branchA, well, branchA has no .gitignore file and that´s why you see foo.txt. Again, you need to merge or rebase branchB into branchA
aalbagarcia
  • 1,019
  • 7
  • 20
  • I already knew that, but thanks anyway. Regardless of whether branchA has the same or a different .gitignore file, my point is: I believe that ignore files should disappear/reappear during checkout like staged files. – freshquiz Dec 07 '12 at 11:47