'If I add and ignore a file in one branch, do I have to manually ignore the file in other branches?

I have a repository with main, dev and feature branches. I created a few files in a feature branch and included them in .gitignore. I don't believe the files were ever added/committed to that branch. If I go back to one of the other branches they show up as new/addable files in that branch (not ignored). Do I have to manually update .gitignore in every branch or is there a way to make that "just happen". The files are unique to this repo so the global .gitignore isn't really the answer.

It's mainly a matter of being skittish when I see a credentials file or other files that I don't want committed show up as addable in other branches. I'm not quite ready to merge the feature back to the other branches either.



Solution 1:[1]

There is no one-size-fits-all answer to this particular problem. (There are several one-size-fits-none answers, but don't use those, they don't fit. ?) As larsks mentioned in a comment, .gitignore is an ordinary file, stored like any other file in ordinary commits, so if you need it updated, you'd need to update it in each branch-tip.

What may work is to list the files—the ones you'd like Git to shut up about—in .git/info/exclude. This file is not a working-tree file under version control (by definition: it's inside the .git repository directory itself), so it is not removed and replaced by git switch or git checkout. A file name or pattern listed in .gitignore, or one listed in .git/info/exclude, or one listed in your global .gitignore, is treated the same way, regardless of which of these three files lists the name-or-pattern (with the exception of precedence, but the info/excludes file overrides the .gitignore file so you're good there).

This file is not copied by cloning, so it's not a general solution.

To expand though, if you change .gitignore in the other branches without merging, does that make the branch ahead of the feature branch?

I'm not sure which branch you mean by the branch (first you have the plural, "other branches", then the singular, "the branch"), but if you add a new commit to some branch, that branch has advanced by one commit and is now "ahead". So I think the answer you're looking for here is "yes".

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 torek