freshquiz
freshquiz

Reputation: 51

gitignore - hide ignored files during checkout

Here is my problem (via example steps):

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.

Upvotes: 5

Views: 1607

Answers (3)

Chris
Chris

Reputation: 471

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).

Upvotes: 1

Masked Man
Masked Man

Reputation: 11055

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

Upvotes: 4

aalbagarcia
aalbagarcia

Reputation: 1044

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

Upvotes: 0

Related Questions