我知道,我的工作流程很常见:
- create feature branch
- make changes
- add changed files
- commit
- push to server
But the mistake I m always making — all the dang time! — is to forget the preliminary step:
- be on the master or other base branch
So I end up creating a branch off of whatever random branch I happened to be on when I did step 1, which is wrong, and doesn t set up the merge I want my co-workers to review after I do step 5, and certainly doesn t set up the merge I want to eventually do back to master. Today I didn t discover the mistake until after step 5, which was a huge nuisance to undo.
因此,我有两个问题:
- What s the right way to rescue myself from this mistake — that is, to take all the work I did in step 2, and cleanly move it to a new branch based where it was supposed to be?
- Is there a way to prevent this mistake in the first place?
For question 1, I suspect the canonical answer is git rebase
. But I don t even try to use it any more, partly because
it s too hard to think about, and partly because it rarely works
for me anyway. (I think the failure mode is that it complains
about any other uncommitted files in the vicinity — and I always
have gobs of those.) So I tend to fall back on git show
or git diff
followed by git apply
, which always feels kind of low-level and
pedestrian, but is at least easy to think about, and usually works.
But is there something I m missing? Is there some other easy-to-think-about, guaranteed-to-work way of picking up a feature branch and plopping it down with a different ancestor? (I know about git filter-branch, but it s even harder to think about.)
But then my other question is, is there some nice way of just avoiding the problem in the first place? I m imagining a warning that could pop up any time I try to create a new branch branched off of something other than master (or some per-worktree configurable "default" branch). If that feature doesn t exist, I think I may try adding it, because it would save me lots of wasted time.