Proper Git way to work with new files introduced in new branch

Started by Chief of Nothing, August 10, 2021, 06:51:25 AM

Previous topic - Next topic

Chief of Nothing

Not sure where to put this one, but it is SMF related...

Hoping some of the learned Git users can shed some light on the proper way to do this. From everything I've read it seems that there is no way to be able to tell Git that a certain file belongs only to a certain branch and not to any others. So using ShawnB61's excellent tutorial as the  basis of what I write below, I have, for example, a default branch and say I've created a feature branch or two to fix a couple of things. While waiting for them to be accepted and merged or denied and thrown away or changes requested I create another feature branch to work on something else which requires a new file or two. It would look something like this:


Default branch (4 files)
|
|-- Feature branch fix bug A (changes 1 file)
|
|-- Feature branch fix bug B (changes 1 file)
|
|-- Feature branch new feature (changes 2 files, adds 2 new files)


The new files obviously get picked up as changes by branches A and B as the new files are sitting in the working directory but they don't belong with A and B so shouldn't be a part of any of their commits if I need to do return to them to change something.

So what is the correct way to deal with this scenario? Everything I've read on the subject, including a wall of text on a StackOverflow post that explains how Git works (which was very indepth) doesn't actually give an answer to the question; how to prevent the new files that belong with the changes in the new feature branch from being included with new commits from A and B, or a new bug fix branch created after the new feature branch.

The only thing I can think of is just create a new default branch in another directory and use that to develop the new feature but that doesn't seem the right way to do things so there is something I'm missing that I can't find an answer for and I can't work out. Can anyone enlighten me?

shawnb61

Just commit the changes to the proper branch.  When you checkout another branch, they won't be there.

From slide 13: "Every time you checkout another branch, the contents of the working
directory are changed to reflect that branch."

When doing your commits, it helps to do a git status.  That will show any new files you have added, and it helpfully suggests you do a "git add" to add the files to your next commit.  Do the git add, then commit.  When you checkout another branch, you won't see the other files.
A question worth asking is born in experience & driven by necessity. - Fripp

Chief of Nothing

Thankyou shawnB61. Why anyone else on the internet couldn't say something as simple as that to everyone else who asked the same question  I'll never know. I have it now, once you commit new files then Git knows where they belong, not beforehand.

Oh, I'm sure you know that in the next few days that GitHub will require either SSH or Personal Access Tokens to be able to participate in SMF development; not sure whether you might want to add an extra slide to your tutorial for newbies.

Advertisement: