一个 git 存储库中的两个并行历史 - 导致 git 变基问题

Two parallel histories in one git repository - Causing git rebase problems

解释:

我创建了一个 Git repository back on October 15, 2015 so that I would have a sort of code Portfolio for when I go to college in about 2 years. At the moment I am learning how to program with python with the help of Udemy,但是一旦完成该课程,我计划学习 C++ 然后 Php。但显然,这不是重点。

所以今天(June 22, 2016)我在我的 Git 存储库中注意到我正在推送我正在做的一个教程中的一些代码,而恰好 [=7​​3=] git log 查看我所有的提交并注意到类似这样的事情:

所以根据我所掌握的 和我从这个发现中收集到的信息,我合并了一些错误的东西,因此我最终得到了看起来像上图的东西。

我的下一步是看看我是否可以使用 git rebase -i --root 命令删除所有标记在蓝色框中(在上图中)的提交。在提交 15(大约)之前,这当然一切顺利,然后吐出一条看起来像这样的错误消息:

所以在那个时候,我刚刚跳过 运行ning git rebase --skip 的那个提交。因此,当(在队列中):

时又进行了两次提交

所以,当然,我 运行 git rebase --skip 让它完成其余的提交。

一旦完成,我只是 运行 git rebase --abort 因为它跳过了这两个提交。在这一切发生之后,我的下一步是在互联网上搜索答案。我能找到的唯一好的答案之一是 。然后我按照答案:

I would suggest that you attempt to first flatten your history to get rid of the parallel histories, and to remove the merge commits. Then, once you have a strictly linear history, you can set about rewriting the history to remove all the debugging churn.

唯一的问题是我不知道如何删除平行历史 and/or 删除合并提交,因为当我 运行 git rebase -i --root 我得到这样的东西:

pick 9140277 Initial commit
pick 95b2f3b Made some minor changes to the code to increase usability
pick d83b165 Converts the folder to a .tar.bz2 and then deletes the folder
pick 3b755b3 Removed some blank lines at the bottom of the file
pick e2f2e3e Added the feature to remove files/folders that are older than a certain age
pick 86b1115 Finished the backup final ftp script for now
pick bfbbcd4 Removed text from a variable
pick 2a7cacd Fixed some minor bugs in the code
pick a943277 Added some new fetures to the script
pick b31bf66 Removed some unnecessary lines of code
pick cc2c9f7 Added Countdown Timers and other help to the portfolio
pick f443919 Fixed a few if statements
pick d6588a0 Everything is in working order
pick f8cc756 Renamed some of the files
pick 6a859f6 Rename backup final ftp.py to backup ftp v.4 (final).py
pick 2dcdae3 Deleted an unneeded folder
pick 0c10370 Added 'Lecture 14 Numbers'
pick 5358b32 Added 'Remove Numbers v.1.py'
pick 8b95691 Reorganized the repository
pick 250f94b Added 'Lecture 16 Strings'
pick 6ebfd31 Changed the numbering of the files
pick 1f275e4 Added 'Lecture 19 Print Formatting'
pick b7d6608 Initial commit
pick 6718968 Made some minor changes to the code to increase usability
pick c2c58c3 Converts the folder to a .tar.bz2 and then deletes the folder
pick 19a495f Removed some blank lines at the bottom of the file
pick c5687a3 Added the feature to remove files/folders that are older than a certain age
pick 93e9742 Finished the backup final ftp script for now
pick 589251c Removed text from a variable
pick 4a7f2d1 Fixed some minor bugs in the code
pick 4172115 Added some new fetures to the script
pick 2377980 Removed some unnecessary lines of code
pick e183900 Added Countdown Timers and other help to the portfolio
pick dfc9747 Fixed a few if statements
pick 0fa9983 Everything is in working order
pick 09abfd7 Renamed some of the files
pick 051dae3 Rename backup final ftp.py to backup ftp v.4 (final).py
pick bac2105 Deleted an unneeded folder
pick e35b6ba Added 'Lecture 14 Numbers'
pick 60b17e4 Added 'Remove Numbers v.1.py'
pick c654494 Reorganized the repository
pick 16e17be Added 'Lecture 16 Strings'
pick 3f37ca3 Changed the numbering of the files
pick ab99b1c Added 'Lecture 19 Print Formatting'
pick 5ea99f2 Added 'Lecture 21 Lists'
pick b8dba98 Reorganized/Fixed all of the code/comments on all of the lectures that I have done up to this point
pick ae50e5b Added 'Lecture 23 Dictionaries'
pick 3306d23 Added 'Lecture 25 Tuples'
pick 36e22b5 Added 'Lecture 26 Files'
pick 2fd3983 Removed a gitignore specifier from the .gitignore file

唯一的问题是我根本看不到合并,它看起来像这样 Merge branch 'master' of github.com:Lowe-Man/Portfolio

最后一个问题我保证。我怎样才能让红线、绿线和蓝线的一小部分消失,使其成为下图中的完整线性历史:

如有任何帮助,我们将不胜感激。 谢谢, 亚历克斯

可以省略合并提交。你可以试试git rebase --onto ab99b1c 5ea99f2^ origin/master。搞定之后,HEAD就是你想要的,只是可能处于detached HEAD状态。

更新:为了简化案例,

A--B--M--D--E
           /
O----P

希望预览看起来没问题(M 是 B 和 P 的合并提交)。您想连同 M 一起删除 O P,只保留 A B D E。

git rebase --onto B M E

现在就像 A-B-D'-E'

更新 2:为了简化另一种情况,

A-B----M-D
    \        /
     C---

C来自A并合并B到M。如果你想把C移动到AD行,

git rebase --onto B C^ D

现在你得到 A-B-C'-D'。 M 是合并提交,已被删除。

更新 3:使用 git resetgit cherry-pick 完成第二次更新中的工作。

git reset B --hard
#now we get A-B

git cherry pick C D
#now we get A-B-C'-D'