English 中文(简体)
意外地致力于一个错误的分支
原标题:accidentally working on a wrongly-based branch

我知道,我的工作流程很常见:

  1. create feature branch
  2. make changes
  3. add changed files
  4. commit
  5. push to server

But the mistake I m always making — all the dang time! — is to forget the preliminary step:

  1. 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.

因此,我有两个问题:

  1. 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?
  2. 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.

问题回答

Your situation is a common issue in git workflows, and it s good to address both how to recover from it and how to prevent it in the future.

www.un.org/Depts/DGACM/index_spanish.htm Rescuing the Mistake

  1. Create a New Branch from the Correct Base:

第一,从原定的基务部门(例如总经理)设立一个新的分支机构。

git checkout master
git pull
git checkout -b new-feature-branch
  1. Cherry-Pick Changes:

其次,使用<代码>cherry-pick<>/code>,将您错误创建的分支的承诺适用于新部门。 查明你在错误的分支上所做的承诺(可为此使用<条码>gitlog),并将其应用于新部门。

git cherry-pick <commit-sha>

各位对此表示要转移。 这种方法比重新建立基础更为简单,避免出现未经处理的档案问题。

  1. Handle Uncommitted Changes:

对于您的工作名录的任何未经许可的改动,在开关前将其搁置起来,并将其应用于新部门。

git stash
git checkout new-feature-branch
git stash pop

www.un.org/Depts/DGACM/index_spanish.htm 防止滥用

  1. Git Hooks:

您可使用<条码>前或<条码>。 h Git 在Gite,警告你,如果你不回到原定的基地。 这就需要说明。

这里的基本例子说明这一文字可能认为:

#!/bin/sh
intended_base="master"

current_branch=$(git rev-parse --abbrev-ref HEAD)
if [ "$current_branch" != "$intended_base" ]; then
    echo "Warning: You are not on the $intended_base branch."
    exit 1
fi

将这一文字列入<代码>.git/hooks/pre-commit或.git/hooks/pre-push在您的存放处存档,并予以执行(chmod +x .git/hooks/pre-commit)。 如果你不再担任总务,这将削弱承诺或推动。

  1. Regular Checks:

在开始新工作之前,总是检查目前的分支部门(git Branch git status)。 虽然这取决于人工纪律,但它是一种良好做法。

commit-ish上设立了分支机构。 目前,除非你使用一项指令,将承付的款项记入。 见DESCRIPTION,git Branch

The command’s second form creates a new branch head named which points to the current HEAD, or if given. As a special case, for , you may use "A...B" as a shortcut for the merge base of A and B if there is exactly one merge base. You can leave out at most one of A and B, in which case it defaults to HEAD.

第2类

git branch [--track[=(direct|inherit)] | --no-track] [-f]
    [--recurse-submodules] <branchname> [<start-point>]

或短期

git branch <branchname> [<start-point>]

因此,git Branch dev equals to git Branch devAD。 如果现任负责人不是预期的起始点,该处就会走错的道路。

我们还可以使用<代码>giteckout或gitchange创建(和核对)一个新的分支。 如果忽略起始点,它们也有相同的规则,即它没有加入< HEAD。 在很多其他强项指挥中,如果承诺是任择性的,它就没有遵守

你所做的承诺在it数据库中得到了维护,因此不会感到担忧。 除非你删除<代码>.git,否则这些代码是安全的,并且没有做任何支持。 在你设立/编辑右起始点事务组之后,您可使用<条码>git cherry-pick、<条码>git rebase或<条码>git格式-----sign+<条码>git am将这些承诺适用于右部门。 <代码>git cherry-pick简单易懂。

# find commits on the wrong branch
git log <branch>

# create a back-up branch
git branch backup <branch>

# reset the branch to the right start-point
# note that hard reset discards uncommitted changes,
# so use "git stash" to save the uncommitted changes
# or use "git worktree" to create a clean worktree for the task
git checkout <branch>
git reset <start-point> --hard

# apply the commits on the backup branch from the oldest to the youngest
git cherry-pick <commit1>
git cherry-pick <commit2>

# ...
# if there is any conflict, resolve it
# and use "git add" + "git cherry-pick --continue" to go on till the end 

是否有办法首先防止这一错误?

每当你设立分支机构时,就请检查并注明起点。





相关问题
git confusion - cloning a repo is returning a past version

Im having some confusion with my git usage. I cloned a repo from one comp to the other, and the new clone is the state of the original that was active some time ago. So its cloning a past version. ...

Appropriate strategy for tagging and hotfixing with git

I was wondering if the strategy I m using for tagging and hotfixing tags (which then I use for deploying rails applications) with git is appropriate. For tagging I just tag a commit of the master ...

Tips on upgrading CVS to git/hg?

We still use CVS, I use git and hg for my personal use though I m still a novice at both, but I realize they re much more modern and better, faster, distributed, etc. It s just everyone is so ...

Using Git in a TFS shop

Using Git at home has spoiled me - I now find using TFS at work to be a bit of a drag and want to explore the possibility of using Git locally and syncing somehow with TFS. I figure there are a few ...

热门标签