Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recover from incorrect rebase or merge

Tags:

git

We have 2 branches: master and free. Most of the code is written in master and merged into free (not the other way). Now, at some point something went wrong: code that should only be in branch free now appears in master. It seems like somebody merged free into master (or re-based free on top of master) and pushes this. This is silly, but we can't identify when this happened and which commit (commits) introduces this bug.

I would appreciate any advice on how to recover from this situation.

like image 664
Dziamid Avatar asked Jan 23 '26 10:01

Dziamid


1 Answers

You can use the -S<search-string> option to git log to find which commits added or removed a particular string from a file.¹ So, if you know that the ToasterFactory class was only introduced on the free branch, you could do the following to see which merge commits brought that into master:

git log -SToasterFactory --merges master

... or if it might have been introduced by a rebase or cherry-pick, you can just do:

git log -SToasterFactory master

If it was a merge commit that brought in all the changes from master, you could try reverting that merge commit, e.g. with:

git revert <SHA1-OF-MERGE-COMMIT>

The one note of caution about that is that if you later want to remerge, you may need to revert the revert first.


¹ Strictly speaking, this option only returns commits where the number of occurences of that string in a particular file. With git version 1.7.4 or later you can use the -G option instead.

like image 175
Mark Longair Avatar answered Jan 25 '26 07:01

Mark Longair



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!