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.
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.
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.
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.
You just need to add the file to . git/info/exclude in the same way you would add it to . gitignore .
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.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
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With