Home > other >  How to deal with files ignored in a feature branch but showed up when switched to the master branch?
How to deal with files ignored in a feature branch but showed up when switched to the master branch?

Time:03-02

Context

In the feature branch, I updated the .gitignore to ignore some generated build files. When I wanted to switch over to the master branch and start work on another feature, the build files showed up because the master branch's .gitignore has not been updated yet.

Question

How do I create a new branch from master and work on a new feature, without the build files showing up as unstaged changes? The branch that contains the build files cannot be merged yet.

CodePudding user response:

You have several choices:

  • Just ignore then "with your eyes"
  • Base your new branch on the unmerged feature branch instead of on master
  • Arrange to have a change committed that only updates the .gitignore file
  • Edit .git/info/exclude, which is just like .gitignore except it's local to your repository (this is documented in the gitignore man page)

CodePudding user response:

In addition to the options provided in larsks's answer, sometimes I opt to just repeat the same changes to the .gitignore file on another branch, but leave it as a pending change and don't commit it. That way it will follow you through branch switches. Once you commit it, then the change will disappear when you switch branches. I find it's much easier to stare at just 1 extra pending file, compared to many. One downside to this though is you need to remember to not accidentally commit the .gitignore file if you are in the habit of staging and committing all pending files in one shot.

Note this works as long as no one else edits .gitignore and commits it to master. As soon as that happens you won't be able to switch branches anymore because you'll have pending changes that would be overwritten, so you would need to stash or commit the file to continue.

Also, .gitignore files can exist in any directory. If the files you wish to ignore are specific to the new project for your feature, and if the project has it's own directory, you could put a new .gitignore file in that directory and leave it pending, and then it's extremely unlikely that someone else would create that file and commit it to master before you do, so you won't have the problem of not being able to switch branches. This is also beneficial if you issue a hard reset for some reason on another branch, a new untracked .gitignore file will survive.

  • Related