How can I re-merge commits that were reverted in github?
Asked Answered
F

2

8

I will try my best to concisely describe my current situation, and would be most grateful for some advice.

I merged in a big feature branch of code this morning that turned out to have a critical error. In order to undo it, my coworker reverted the merge (which had several commits) in GitHub and after pulling, all appeared to be well.

After making some tweaks to the feature branch, I wanted to make sure it could go in again, so I used 'git merge master' on my feature branch (like I always do) to make sure everything is up to date.

Surprisingly, the result of that was to delete all of the new code that I am needing to merge back in to the master repo!

Is this due to the revert that occurred on the branch? Looking through the git log, I can see that all of the commits are there still. And even more strange, the pull request in github doesn't show any of the original commits in the diff, only what I have changed since the revert.

Can someone help me make sense of this?

I know some people have suggested simply reverting the revert, but I need to go back in clean, as the changes I made deal with the structure of much of the code.

Fagot answered 20/5, 2015 at 3:47 Comment(4)
hi there. try git reflog in your console. it will show you the full history of what has been done to your repo (not branch but full repo).checkout the desired point in time and continue from there.Diophantus
Thanks for the comment! In my case, the commits are interspersed throughout. The branch was made over several months, and there isn't a clean spot for me to reset to. I think that rebase is what I need, just trying to figure out how to completely remove the commits I am adding in again from the feature branchFagot
Ok, ill post it as answer - the rebase you needDiophantus
I suggest against rebasing here. Changing history of a published branch will make you very unpopular.Assailant
A
11

I ran into this very same issue once. My solution that worked was to "revert the revert" before merging master back into my branch.

What that does is leave you with all your changes, including the new fixes that you've got.

git checkout master
git checkout -b master-with-revert-revered
git revert <revert commit id>
git checkout fixed-branch
git merge master-with-revert-revered

After that, your fixed-branch should be directly mergable back into master with no problem.

Assailant answered 20/5, 2015 at 6:23 Comment(1)
I appreciate the insight, Daniel. I have tried a number of different options up until now, and due to the complexity of these commits and the sheer number of them, I think that reverting the revert is going to be my safest bet.Fagot
D
1

I think that rebase is what I need, just trying to figure out how to completely remove the commits I am adding in again from the feature branch

if you need to "remoe/merge" commit you should use squash.
Squash is one of the rebase options.

In order to use it you need to do interactive rebase.
git rebase -i HEAD~10 will open a vi (or any other editor that you are using) and will allow you to "reshuffle" your commits. You can remove commit, merge them in to a single one, re-order them and mush more.

You will find a list of options inside the vi along with the list of the commits that you choose to reabse (In the sample above it will take the last 10 HEAD~10)

Diophantus answered 20/5, 2015 at 6:16 Comment(3)
Thanks for your answer! So in my case, because revert didn't really remove the commits, do I want to delete all of them from the branch? Looks like the command for that is dd. And it also looks like the first commit I need to remove is over 100 commits ago, so HEAD~100?Fagot
Exactly. you got it :-)Diophantus
Or you can revert to the desired point in time, create new branch from that point and then interactively rebase form this pointDiophantus

© 2022 - 2024 — McMap. All rights reserved.