Undoing a git bisect mistake
Asked Answered
E

3

226

I'm doing a non-automated git bisect via command line. All is going well until I accidentally hit return on the wrong line in my command history, and rather than running the test, I run 'git bisect good' (or bad). Oops - I don't yet know if this commit should be marked good or bad, yet that's what I've done.

Can I undo the 'git bisect good' command, or make git forget the result of it, and go back and run the test for that commit?

Etty answered 21/12, 2011 at 18:45 Comment(0)
C
283

From the git-bisect documentation:

Bisect log and bisect replay

After having marked revisions as good or bad, issue the following command to show what has been done so far:

$ git bisect log

If you discover that you made a mistake in specifying the status of a revision, you can save the output of this command to a file, edit it to remove the incorrect entries, and then issue the following commands to return to a corrected state:

$ git bisect reset
$ git bisect replay that-file
Canicular answered 21/12, 2011 at 18:48 Comment(5)
Too complicated, too many it-dependses. What command or sequence of commands, taking NO parameters depending on the current repository state (i.e. "that-file"), will GUARANTEEDLY reset the repository to the state just before the erroneous command?Scimitar
@SzczepanHołyszewski This literally took 25 seconds from I read it to me being exactly where I was before I mistakenly issued a git bisect good too many. There are no "it depends" bits here. Just literally do it. It is not complicated. git bisect log > f; vim f; git bisect reset; git bisect replay f;Craiova
And them rm f (or rm that-file) :) I know it's obvious, but it triggers me when I see instructions that leave unused files on your system (I know that in this case, being about a git repository, you'll notice it, but it's not always the case).Rawls
@Craiova I think the first comment was looking for something like git bisect undo, which would be pretty nice...Susiesuslik
Well, he could add an alias for that in his .gitconfig: bisect-undo = 'git bisect log | sed -e '$ d' > /tmp/trimmed-log; git bisect reset; git bisect replay /tmp/trimmed-log; rm /tmp/trimmed-log;. Then he could do git bisect-undo all he wants :)Craiova
L
133

You can output a record of what was done using

$ git bisect log > bisect.log

Open that file in an editor and edit/remove the faulty line. Then you can replay it with

$ git bisect replay bisect.log

This is documented in git help bisect.

Lazaretto answered 21/12, 2011 at 18:49 Comment(3)
Do you have to also git bisect reset as @Canicular answer indicated?Whitby
@ColinD I did not have to reset first. I'm using git version 2.19.1.windows.1Vaso
I had to reset first, using git version 2.25.1.Kirchhoff
L
2

If you want to undo your last instruction in one go, without having to use a text editor, you can use the following command:

git bisect log | head -n -2 > /tmp/fixed_bisect.log ; git bisect replay /tmp/fixed_bisect.log

It will remove the two last lines of the log (because there's a comment + the actual command it corresponds to), then save that to file and directly reuse it with bisect replay. (Unfortunately, bisect replay can't receive input directly with the pipe, so it needs to create a temporary intermediate file.)

To make it even easier, you can add an alias to your .bashrc or equivalent:

# bisect undo alias
alias bisectundo="git bisect log | head -n -2 > /tmp/fixed_bisect.log ; git bisect replay /tmp/fixed_bisect.log"

In a new session, you will be able to simply use bisectundo to go one step back.


If you get an "illegal line count" error for the negative number argument (which is because some implementations of head don't support it), you can use this longer form, which bases the number of lines to keep on the length of the log using wc:

git bisect log | head -n $(($(git bisect log | wc -l)-2)) > /tmp/fixed_bisect.log ; git bisect replay /tmp/fixed_bisect.log
Lucey answered 25/1, 2023 at 14:56 Comment(2)
This is a good idea, but your suggestion gives a head: illegal line count -- -2Elk
@Elk I adapted my answer with an option for when that happens.Lucey

© 2022 - 2024 — McMap. All rights reserved.