Alex Lowe
Alex Lowe

Reputation: 871

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

Explanation:

I created a 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, but once I am done with that course I am planning on learning C++ and then Php. But obviously, this is all besides the point, though.

So today(June 22, 2016) I noticed in my Git repository as I was pushing up some code from one of the tutorials that I was doing and just happened to run git log to see all of my commits and noticed something like this:

git commit tree

So from what I have read and the information that I have gathered from this finding is that I merged something wrong and because of that I have ended up with something that looks like the image above.

My next step was to see if I could use the git rebase -i --root command to delete all of the commits that are marked in the blue box (in the image above). Which of course was going fine until it hit commit 15 (about) where it then spit out an error message that looks something like this:

2nd error message

So at that point, I just skipped that commit by running git rebase --skip. So about two more commits went by when (right on queue):

1st error message

So, of course, I ran git rebase --skip and just let it finish up with the rest of the commits.

Once that got done I just ran git rebase --abort because it had skipped those two commits. My next step after all that happen was to search around on the internet for answers. One of the only good answers that I could find was here. I then followed the answer:

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.

The only problem was that I could not figure out how to remove parallel histories and/or remove merge commits because when I ran git rebase -i --root I get something like this:

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

The only problem being that I do not see the merge at all which is suppose to look like this Merge branch 'master' of github.com:Lowe-Man/Portfolio.

One last question I promise. How can I make the red line disappear, green one, and that little section of that blue line to make this a full linear history in the image below:

linear history

Any help would be greatly appreciated. Thanks, Alex

Upvotes: 0

Views: 393

Answers (1)

ElpieKay
ElpieKay

Reputation: 30966

A merge commit may be omitted. You could try git rebase --onto ab99b1c 5ea99f2^ origin/master. After it's done, the HEAD is what you want, but it may be in detached HEAD state.

Updated: to simplify the case,

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

Hope the preview looks okay (M is the merge commit of B and P). You want to remove O P along with M, and keep just A B D E.

git rebase --onto B M E

now it's like A-B-D'-E'.

Updated 2: to simplify the other case,

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

C is from A and merges B to M. If you want to move C into Line AD,

git rebase --onto B C^ D

Now you get A-B-C'-D'. M is a merge commit and has been removed.

Updated 3: use git reset and git cherry-pick to do the job in the 2nd update.

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

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

Upvotes: 3

Related Questions