by coldtea · 2y ago · 0 comments
- You can fix the bug and commit it into your feature branch and try to get them both deployed together.
- You can stash everything, create a new branch, switch to it, fix the bug, commit and push it, then switch back to what you were working on.
- You could locally clone the repository to a new directory and work on the other branch there, then push it back into the main one and then push from there back upstream.
--
What?
Why couldn't you, like, create another branch off of main, switch to that, and fix the bug there, then resume working on your feature branch?
Then when one of the two (bugfix or feature branch) is merged into main, you rebase main into the other.
Worktrees are a different way of managing multiple branches in flight. They're not hard to use, and using them doesn't imply any incompetency with git. They are likely being used to cope with another kind of problem.
Why would you ever have build artifacts, incremental or not, as part of your code repo?
Instead of stashing or anything like that we would commit with the text "WIP". Every time we had a stable point, we would make a "WIP" commit and then continue, giving us a good point to revert to if the next steps don't work.
If someone interrupts me I would make a WIP commit and then revert it and then make the change they're asking about.
Later on we would squash the WIP commits and make real commits out of the changes.
Another nice thing about this workflow is that everything goes into the reflog.
Just one concept instead of (1) commits (2) stash (3) oh these changes, eh, I don’t know what to do with them... let’s think about it after lunch.
Here's one example:
I'm working on a trunk-based project, and I am upgrading a core framework like Angular or Django. That's a fairly intrusive change that involves modifying some untracked on-disk artifacts, like node_modules/ and package lockfiles. Switching branches doesn't help me manage those artifacts, and might require a full teardown and rebuild of my dev environment each switch. Even if I maintain a meticulous commit history and never have uncommitted "WIP stuff" that I need to stash, switching from my branch to fix a bug in the main branch could be annoying and intrusive just because of the dev environment. This is a great case for a separate worktree.
This is kind-of the million dollar question, though, I think? What would such a reason be?
(I ask from a perspective of genuine curiosity, not of implying that you're wrong to have them)
Just for the purposes of comparison - my own "bright line between [good and bad]" is "does the commit message just contain 'WIP', or is it actually fully written-out?" - but I can see that "is the work committed or not?" also works. Thanks for explaining!
I’ll keep an open mind, but if you’re going to shake up a workflow as solid and well understood as git, you better have a damn good offering.
How "cloning the repo on a different folder on the same machine just to work on a bug fix" is different from the main way Git is supposed to be used (creating a new branch for the bug fix and working there, then checking out your feature branch again)?
In that it doesn't make sense.
They might as well go full 1980s style "codebase_1/ codebase_2/ codebase_3/ codebase_3.bak/" and so on copies, and skip code management altogether...
> You can stash everything, create a new branch, switch to it, fix the bug, commit and push it, then switch back to what you were working on.
OP said this (you and a few others reiterated or agreed with it)
> Why couldn't you, like, create another branch off of main, switch to that, and fix the bug there, then resume working on your feature branch?
I can't see the difference. Seeing them side by side they're virtually identical except the blog author specified stashing!
You're supposed to be working in a feature branch when your were given the bug to fix. Would you not have commited (or, worse, pushed) anything to it already? Most times you already have, so it's not like stashing your latest changes and getting to the latest state of the feature branch is neutral.
Plus, what big benefit would stashing have, since you'll still need to create a separate bugfix branch to create the bugfix PR off anyway? If you get to context switch while you were working on my-feature-branch, you still need to create my-bugfix-branch and push the fix from that, no?
Besides nobody guarantees you ahead of time that the bug will just be solved fast and they get to return to working on the feature pronto. So a bugfix branch makes more sense than a stash, which is more of a short term thing.
Just making a bugfix branch is neater.
(Again this is what git-reset(1) and friends are for)
So? That's what amend and/or squash is for. So that you don't need to be in some "perfect" condition to commit. Or you can do a soft git reset as you say.
But sure, even git stash would make more sense than worktrees for the TFA scenario I quoted.
Why not just commit it?
I mean, if it's not a problem to do that (and I don't find either option a problem personally) then fine, but it doesn't seem like it's a "better" option except in some small cases (like forgetting you stashed).
> You can stash everything, create a new branch, switch to it, fix the bug, commit and push it, then switch back to what you were working on.
And OP said this (snarkily)
> Why couldn't you, like, create another branch off of main, switch to that, and fix the bug there, then resume working on your feature branch?
They're the same except the blog author was more specific!
But is it overkill for some fast translation fixes? Yeah, probably:)
The question is, if that exists why are the virtual trees meaningfully different?
I mean, that's option 2 of what you're quoting, and is generally my preferred option. That said, there are cases where it breaks down and I end up wanting worktrees or independent clones, generally involving ≈`.gitignore`d local data:
• A newly created bugfixing branch may take awhile (minutes? hours?) for me to build. I can either go on a coffee break while waiting for it (since I can't very well test my ability to reproduce the bug, nor verify my bugfixes work, without having said branch in a compiling state), or I can have said build occur in a secondary worktree - and continue working on my original task in my primary worktree while waiting for it to build.
• If I've got a lot of `.gitignore`d config file tweaks related to my major feature work / profiling / debugging / ???, I might not want to bring those over to my bugfixing session. `git stash push --all` is an option, but that will get build artifacts - which will take awhile to stash - not just my local config files. A fun ancedote: I once wasted an hour failing to reproduce a game's audio crash because a local `user.ini` file had the entire audio system disabled via feature flag. (I now prefer setting the volume to 0 instead.)
• I do a bunch of porting and cross-platform abstraction stuff, which makes my build times even worse than usual by having to repeat builds N times. I might not want to invalidate by main build cache for all that, when my bugfixing branch might only reasonably need local testing on 1 platform, and gain next to no benefit from the previous cache as is (depending on how far diverged the branch I was working from and the branch where the bugfix should occur are.)
No, option 2 is to stash.
Their three weird options are:
(a) fix the bug on the feature branch and "try to get them both deployed together"
(b) git stash, fix the bug in a new branch, come back
(c) locally clone the repository
At least (b) is somewhat sane, but it's still weird that the even more code-managementy option of "commit your halfway feature work/create new branch for the bugfix/come back to the feature and amend/squash/soft reset" isn't even mentioned.
Your cases are more involved than the contrived scenario you give, where their weird options don't make sense.
I don’t want to fiddle with dropping the stash one too many times, forgetting what they were about and so on. Merge conflicts because I didn’t pop before resuming..
It’s just a mostly useless additional “thing”.
...this is just by-hand stashing with alternative commands and extra "WIP" commit messages.
Which, uh, is admittedly an option. One that won't easily preserve your index independently from your working tree - awkward if you're in the middle of painstakingly separating out changes to add to the next commit with `git add -p` - and one that risks "WIP" commits ending up in your tree... but it is an option.
The index + commits is flexible enough. The stash concept is not needed for me.