【问题标题】:How does one split a branch from master如何从 master 中拆分分支
【发布时间】:2012-06-22 13:48:41
【问题描述】:

大家好,

我的 repo 有一个奇怪的问题。最初我只有 master 分支可以提交,但是在我完成应用程序的功能版本后,我决定创建一个 develop 分支以继续开发但保留工作版本master 中没有改动,直到我想合并它。

目前(使用gitk --all)我的 git 日志图如下所示:

标签 | *----*-----*-----*-----*------*------*------* 掌握和发展 \ \ --------*----* 开发(HEAD)

我不确定我做了什么导致这个,下面是我创建 develop 分支的步骤:

$ git checkout develop
$ vim example.txt
$ git add .
$ git commit -m "first file in develop"
$ git push origin develop 

在这之后,我开始计算开发分支,直到我查看了 gitk

我想要的(并认为是使用 git 分支的正确/健康方式)是:

标签 | * - - * - - -* - - -* - - -* - - -* - - -* 掌握 \ *-----*-----* 开发(HEAD)

我需要怎么做才能让我的代码库看起来类似于上图?

编辑#1:

git reflog 的输出用于 develop

54d8daf HEAD@{0}: reset: moving to HEAD@{15}
45dcaa7 HEAD@{1}: rebase finished: returning to refs/heads/develop
45dcaa7 HEAD@{2}: rebase: added another sanity check for cli parameters
f3b0f8b HEAD@{3}: rebase: I am working on getting the console improved, this including better color, and more cammand options
8c48bab HEAD@{4}: checkout: moving from develop to 8c48babb10ec2f8f28f364ce57b5095cb080c133^0
54d8daf HEAD@{5}: checkout: moving from master to develop
8c48bab HEAD@{6}: checkout: moving from develop to master
54d8daf HEAD@{7}: rebase: aborting
3bf3f64 HEAD@{8}: commit: rebase
6aa82d8 HEAD@{9}: rebase: added another sanity check for cli parameters
a9be651 HEAD@{10}: rebase: I am working on getting the console improved, this including better color, and more cammand options
8c48bab HEAD@{11}: checkout: moving from develop to 8c48babb10ec2f8f28f364ce57b5095cb080c133^0
54d8daf HEAD@{12}: rebase: aborting
7890b76 HEAD@{13}: rebase: added another sanity check for cli parameters
46847d7 HEAD@{14}: rebase: I am working on getting the console improved, this including better color, and more cammand options
8c48bab HEAD@{15}: checkout: moving from develop to 8c48babb10ec2f8f28f364ce57b5095cb080c133^0
54d8daf HEAD@{16}: commit: changed the how function, it seems I was right, wc counts all lines regardless of readable symbols or not.
13d35ed HEAD@{17}: commit: am working on the how function (should be trivial, but I have a bad feeling)
852fb68 HEAD@{18}: commit: I have added some more commands (thought not fully implemented), but more specifically I have changed the pattern matching
33dfc9f HEAD@{19}: commit (merge): change some output settings, asthetics and such. I am working on getting more command options for list previous lin
a248465 HEAD@{20}: commit (amend): added another sanity check for cli parameters
f0ca508 HEAD@{21}: commit (amend): added another sanity check for cli parameters
9e24e91 HEAD@{22}: commit: added another sanity check for cli parameters
0194cab HEAD@{23}: commit: I am working on getting the console improved, this including better color, and more cammand options
8c48bab HEAD@{24}: checkout: moving from master to testing
8c48bab HEAD@{25}: commit: I think I have wroked out all of the import bugs, the rest is really just esthetics, welcome version 1
b306d9f HEAD@{26}: commit: as it turns out it was easier then I thought, plus I fixed an incorrect append method
8445a25 HEAD@{27}: commit: seems I miss understood how bash case statments handles regex, or should I say pathname expansion matching
9fc5132 HEAD@{28}: commit: lol, totally forgot to use the function 'run' to get things going
ee2a2c7 HEAD@{29}: commit: seems I haven't yet figured out how to correctly parse cli args...
bb0fd28 HEAD@{30}: commit: fixed incorrect sanity check
eb3db1d HEAD@{31}: commit: have reached a stage I could refer to a working test system, which is what I am going to do.
4bfd53b HEAD@{32}: commit: added a lot more colours (am using a list from arch-wikis), incorporated much better error handling
2d9b598 HEAD@{33}: commit: getting there part 1
0edfcae HEAD@{34}: commit: output functions so far
06130a7 HEAD@{35}: commit: modularized two printing function into a seperate script
e0033b8 HEAD@{36}: commit (initial): used some functions from previous bash script, maybe I should create a generic script for text output...?

错误地,我试图做一个变基,但每次合并尝试都失败了。在文件的不同签出上使用git diff--theirs--ours)表明远程和本地之间没有区别。所以我重置了头部以提交HEAD@{15}

编辑#2: 对于可能造成的混乱,我深表歉意,但是在图 1 中,“master & develop”表明两个分支(masterdevelop)都合并为一个 '溪流'。我希望这更有意义。

编辑#3:

来自git log master --pretty=oneline的输出

8c48babb10ec2f8f28f364ce57b5095cb080c133 I think I have wroked out all of the import bugs, the rest is really just esthetics, welcome version 1
b306d9f58c3ebab03364c4381754bc87db9e7f2d as it turns out it was easier then I thought, plus I fixed an incorrect append method
8445a258fc388eba5b920de57b1f390623da87a0 seems I miss understood how bash case statments handles regex, or should I say pathname expansion matching
9fc5132c30bdf2ab143ad046493326ba63e4300d lol, totally forgot to use the function 'run' to get things going
ee2a2c7ff69498a80e9fc5f36cc340f6e1b95bcb seems I haven't yet figured out how to correctly parse cli args...
bb0fd28c89a0c16c379774b7fc5768a4160d6852 fixed incorrect sanity check
eb3db1d055b45793fb9a3fd5f884926687f7b8dd have reached a stage I could refer to a working test system, which is what I am going to do.
4bfd53b3af85d82aea11871a0ddb7f9d583cfb73 added a lot more colours (am using a list from arch-wikis), incorporated much better error handling
2d9b598d77e2b62611dfd2e42b183d0262880014 getting there part 1
0edfcae88e6eae66de4fae1788433d83b90a1c37 output functions so far
06130a7288ce26b4a2235864f7cd5b48a35dcea6 modularized two printing function into a seperate script
e0033b8f440f2ffb7b9a120f5a9be9ea167e1e30 used some functions from previous bash script, maybe I should create a generic script for text output...?

git log develop --pretty=oneline 的输出

54d8daf475a5e1108cd6345850245db2d0e6fd80 changed the how function, it seems I was right, wc counts all lines regardless of readable symbols or not.
13d35ed979de3518eca26961f3be4bb0b7d8de03 am working on the how function (should be trivial, but I have a bad feeling)
852fb68b8ca929e1156e38e2a58a12f07bd9aaa5 I have added some more commands (thought not fully implemented), but more specifically I have changed the pat
33dfc9f187a54dc7234c9ca31bee91eef7c89a98 change some output settings, asthetics and such. I am working on getting more command options for list previo
a2484654eff741f4c625f8cb097fbb92f6ac8b9b added another sanity check for cli parameters
9e24e9157157f128f75b3d7859875d06d33137bd added another sanity check for cli parameters
0194cab8af66656bb9fe83c919409354f6d12edc I am working on getting the console improved, this including better color, and more cammand options
8c48babb10ec2f8f28f364ce57b5095cb080c133 I think I have wroked out all of the import bugs, the rest is really just esthetics, welcome version 1
b306d9f58c3ebab03364c4381754bc87db9e7f2d as it turns out it was easier then I thought, plus I fixed an incorrect append method
8445a258fc388eba5b920de57b1f390623da87a0 seems I miss understood how bash case statments handles regex, or should I say pathname expansion matching
9fc5132c30bdf2ab143ad046493326ba63e4300d lol, totally forgot to use the function 'run' to get things going
ee2a2c7ff69498a80e9fc5f36cc340f6e1b95bcb seems I haven't yet figured out how to correctly parse cli args...
bb0fd28c89a0c16c379774b7fc5768a4160d6852 fixed incorrect sanity check
eb3db1d055b45793fb9a3fd5f884926687f7b8dd have reached a stage I could refer to a working test system, which is what I am going to do.
4bfd53b3af85d82aea11871a0ddb7f9d583cfb73 added a lot more colours (am using a list from arch-wikis), incorporated much better error handling
2d9b598d77e2b62611dfd2e42b183d0262880014 getting there part 1
0edfcae88e6eae66de4fae1788433d83b90a1c37 output functions so far
06130a7288ce26b4a2235864f7cd5b48a35dcea6 modularized two printing function into a seperate script
e0033b8f440f2ffb7b9a120f5a9be9ea167e1e30 used some functions from previous bash script, maybe I should create a generic script for text output...?

git log master...develop 的输出

提交 54d8daf475a5e1108cd6345850245db2d0e6fd80 作者:汉斯 日期:2012 年 6 月 21 日星期四 21:27:14 +0200 改变了how函数,看来我是对的,wc计算所有行,不管可读符号与否。 提交 13d35ed979de3518eca26961f3be4bb0b7d8de03 作者:汉斯 日期:2012 年 6 月 21 日星期四 21:09:44 +0200 正在研究如何功能(应该是微不足道的,但我有一种不好的感觉) 提交 852fb68b8ca929e1156e38e2a58a12f07bd9aaa5 作者:汉斯 日期:2012 年 6 月 21 日星期四 20:58:16 +0200 我添加了更多命令(认为没有完全实现),但更具体地说,我稍微改变了模式匹配,现在是 r 提交 33dfc9f187a54dc7234c9ca31bee91eef7c89a98 合并:a248465 9e24e91 作者:汉斯 日期:2012 年 6 月 21 日星期四 20:42:56 +0200 更改一些输出设置、美学等。我正在努力为列出前几行等获取更多命令选项 提交 a2484654eff741f4c625f8cb097fbb92f6ac8b9b 作者:汉斯 日期:2012 年 6 月 21 日星期四 20:23:52 +0200 为 cli 参数添加了另一个健全性检查 提交 9e24e9157157f128f75b3d7859875d06d33137bd 作者:汉斯 日期:2012 年 6 月 21 日星期四 20:23:52 +0200 为 cli 参数添加了另一个健全性检查 提交 0194cab8af66656bb9fe83c919409354f6d12edc 作者:汉斯 日期:2012 年 6 月 21 日星期四 20:06:02 +0200 我正在努力改进控制台,包括更好的颜色和更多的 cammand 选项

【问题讨论】:

  • 在顶部分支上显示“master & develop”是什么意思?
  • @vergenzt:我的意思是说分支 masterdevelop 是一个分支,直到标签之后。当我第一次创建 repo 并且只有 master 时,情况并非如此。
  • 正在使用什么版本的 git?
  • @wadesworld:本地git版本1.7.10.msysgit.1,服务器使用的是1.7.10.3

标签: git version-control


【解决方案1】:

您可以对任何分支使用 reflog:git reflog 来查看该 ref 的历史记录,然后只需使用 git reset --hard HEAD@{INDEX_HERE} 即可恢复该分支的更改。

第一张图显示了当您在 master 分支中提交,然后在 develop 分支中创建 git pull 时的情况。 git pull 会将跟踪分支(master)合并到当前分支(develop)中。

如果您可以为分支 master 和 development 发布您的 reflog,那么我们可以检查它。

【讨论】:

  • 是的,正如预期的那样有一个合并:33dfc9f HEAD@{20}: commit (merge): change some output settings,... 是来自master 分支吗?
  • Reflog 是所有分支的一个 :) 你不应该为每个分支发布两次)
  • 谢谢你,删除它;)
  • 请添加以下命令的输出:git log master --pretty=onelinegit log develop --pretty=onelinegit log master...develop
  • 好的,根据git日志,你的图应该和图2一样。但是你是怎么合并来自同一个分支的两个提交的??? Merge: a248465 9e24e91不正常。。建议你为develop分支做一个交互式rebase,修改历史中的merge commit
【解决方案2】:

我需要做什么才能让我的仓库看起来与上图相似?

要从'master'创建分支'develop',我会按照git manual中给出的说明进行操作:“要创建一个分支并同时切换到它,你可以运行git checkout命令-b 开关”。

在你的情况下,汉斯,你可以执行以下操作(假设“主”在远程“原点”中):

# checkout and pull latest from master
git checkout master
git fetch origin
git pull origin master

# create the develop branch
git checkout -b develop

现在“master”和“develop”分支是不同的,未来的提交应该让你的提交历史看起来像你想要实现的正确/健康的图表。

【讨论】:

  • 你是否建议我用我当前的文件创建新的 repo,但是这次采取了正确的步骤来创建 develop 分支?
  • 我认为了解情况并修复它会更好,但是您可以轻松删除 develop 分支并再次从 master 分支重新创建它 - 没关系,因为您没有'不要将你的开发分支发布到远程。
【解决方案3】:

事实证明,我误解了分支在 git 中的工作方式。这是的根本问题。 我的回购设置完全没有问题

然而,正如 Alexander 向我指出的那样,a248465 9e24e91 存在异常合并,因此创建了如图 1 所示的“双重提交”。这些提交实际上是相同的提交,但具有不同的 SHA1 哈希值。我认为这是因为我错误地应用了git commit --amend

删除它们的解决方案是重新设置 develop 分支并从历史记录中删除提交 9e24e91

感谢大家的帮助!

【讨论】:

    猜你喜欢
    • 2019-06-06
    • 2020-10-11
    • 2011-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-22
    • 2014-12-17
    相关资源
    最近更新 更多