Git. Find an error in code, created during rewriting of history

65 Views Asked by At

Suppose I made number of changes in the code and see that code-result unexpectedly has been corrupted. I want to find which change corrupted the result.

It is clear how to find the bugged change if it is contained in some commit.

But how to find the bugged change if it was not in a separate commit, but it was made during history rewriting?

So few days ago I had git history like:

commit00 ( contains no result.txt )
commit01 ( contains no result.txt )
...
commit10 ( contains version1 of result.txt )
commit11 ( contains version1 of result.txt )
...
commit20 ( contains version2 of result.txt )

all versions of results.txt was fine. Then I made few times history rewriting like:

<make changes>
git commit -m "changes to attach to commit0x"
git rebase -i commit0x
<use squash to attach changes to commit0x>

I don't remember which changes I made this way. But now I see that when I go to commit10 or commit20 my program gives different results (not version1 and version2 results).

1

There are 1 best solutions below

4
On

You should be able to use

git reflog

to find the commit(s) before the rebase and go back to that, supposing the rebase was done recently or the log wasn't pruned.

As an example if your git reflog would look like this:

0fe7a44 HEAD@{0}: checkout: moving from b5e056e81aaaf9191dc88f30d54997318f585f5a to master
b5e056e HEAD@{1}: checkout: moving from master to b5e056e81
0fe7a44 HEAD@{2}: rebase -i (finish): returning to refs/heads/master
0fe7a44 HEAD@{3}: rebase -i (continue): add test file
b5e056e HEAD@{4}: rebase -i (start): checkout b5e056e^
69eb9b9 HEAD@{5}: commit: remove test file
b5e056e HEAD@{6}: commit: add test file
f1a96f7 HEAD@{7}: commit (initial): Initial commit.

You could simply check the HEAD you think might contain your changes with

git log HEAD@{6}

or checkout/reset to that as you would with a normal branch.

... hope that'll help.

Sources: git-reflog docs