Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gitignore - hide ignored files during checkout

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.

like image 605
freshquiz Avatar asked Dec 07 '12 10:12

freshquiz


People also ask

Does git checkout overwrite ignored files?

Silently overwrite ignored files when switching branches. This is the default behavior. Use --no-overwrite-ignore to abort the operation when the new branch contains ignored files. Using --recurse-submodules will update the content of all active submodules according to the commit recorded in the superproject.

How do I hide files in Gitignore?

gitignore . And anything that we put inside of this file is going to be ignored. So if we want to hide our secrets I can just type secrets. yml and then if I save the file close it and then now type git status you're going to see that the secret's file that used to be an untracked file is now gone.

Does Gitignore only work when committed?

Git can only ignore files that are untracked - files that haven't been committed to the repository, yet. That's why, when you create a new repository, you should also create a . gitignore file with all the file patterns you want to ignore.

How do I ignore a file without adding to Gitignore?

You just need to add the file to . git/info/exclude in the same way you would add it to . gitignore .


2 Answers

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

like image 90
Masked Man Avatar answered Sep 23 '22 06:09

Masked Man


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

like image 22
Chris Avatar answered Sep 23 '22 06:09

Chris