【问题标题】:How to make one branch based on another?如何基于另一个分支创建一个分支?
【发布时间】:2015-05-13 09:19:54
【问题描述】:

我对我的仓库执行了某些操作。 (这实际上是尝试将 svn 分支与 git 同步,但为了这个问题,我们假设它们是未知的——正如 SO 指南所建议的那样,尽量保持最小化)。在那之后,我得到了一个不基于任何东西的分支broken_branch。只是一个没有任何父母的提交。

$ git lg
* 5757abc - (HEAD, origin/branches/broken_branch, broken_branch) ~ Some broken branch commit message (58 minutes ago) <AuthorName>

但是,我知道这个提交应该基于其他带有哈希的提交,比如来自master 分支的abc1234。所以,我尝试做一个合乎逻辑的事情,rebase:

$ git rebase abc1234
First, rewinding head to replay your work on top of it...
Applying: ~ Some broken branch commit message
Using index info to reconstruct a base tree...
<stdin>:829: trailing whitespace.
  userData:
<stdin>:839: trailing whitespace.
  userData:
<stdin>:849: trailing whitespace.
  userData:
<stdin>:859: trailing whitespace.
  userData:
<stdin>:869: trailing whitespace.
  userData:
warning: squelched 204361 whitespace errors
warning: 204366 lines add whitespace errors.
Falling back to patching base and 3-way merge...
Auto-merging Path/To/File/Class.cs
CONFLICT (add/add): Merge conflict in Path/To/File/Class.cs
Failed to merge in the changes.
Patch failed at 0001 ~ Some broken branch commit message
The copy of the patch that failed is found in:
   /Path/To/Project/.git/rebase-apply/patch

When you have resolved this problem, run "git rebase --continue".
If you prefer to skip this patch, run "git rebase --skip" instead.
To check out the original branch and stop rebasing, run "git rebase --abort".

其中Path/To/File/Class.cs 是在broken_branch 的单次提交中修改的文件。实际上,大约有 20 个文件被修改,每一个文件我都收到相同的消息。

(我已替换此问题中的所有字符串,以免意外泄漏任何内容,但我努力使所有内容保持一致)。

所以,我有这些问题:

  1. 我使用git rebase 解决此问题的决定是否正确?还是我应该使用其他东西?
  2. 为什么会出现这些错误?我很肯定 broken_branch 最初是在这个特定的提交上创建的(在 svn 中,但让我们将 svn 排除在问题的范围之外)。
  3. 我怎样才能解决这个变基来给我想要的结果?

【问题讨论】:

  • 潜在编辑注意事项:我没有用svn 标记它,因为我试图尽量减少问题的上下文。我只提到svn 来解释我是如何达到这种状态的(因为否则有人会问我),但为了这个问题应该是无关紧要的。
  • 另外,我认为这个问题现在的标题很糟糕,但我想不出更好的标题。随意编辑它。
  • 我会在从“已知”父亲创建的新分支上挑选破碎的银行提交。然后删除损坏的分支。
  • @BigMike 试过了;得到 error: could not apply 5757abc... ~ Some broken branch commit message 与相同的合并冲突

标签: git git-branch git-rebase


【解决方案1】:

据我了解,您希望您的 git 存储库看起来像这样:

    abc1234
    |
    5757abc'

5757abc' 的内容与 5757abc 完全匹配并且是 abc1234 的子项?如果是这样,那么您可以这样做:

# Create test_branch pointing to commit 5757abc
git checkout -b test_branch 5757abc

# Reset test_branch to point to abc1234, but do not change the index 
# or working copy (these still contain everything in 5757abc)
git reset --soft abc1234

# Commit your changes
git commit -m "My New Commit"

参考:https://git-scm.com/blog/2011/07/11/reset.html

这将根据需要修改 git 历史记录。

您还可以修改&lt;repo&gt;/.git/refs/remotes/origin/branches/broken_branch,使origin/branches/broken_branch 指向这个新提交。

但是,我不知道 git-svn 在此之后是否会正常工作。请务必在尝试此操作之前进行备份!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-15
    • 2011-05-27
    • 2021-08-09
    • 1970-01-01
    • 2011-07-15
    • 2022-10-25
    • 1970-01-01
    相关资源
    最近更新 更多