How to run git bisect only on commits that changed a specific file?

I have a mistake that was introduced a long time ago, and testing for it is painful. However, I strongly suspect that the changes that introduced the error occurred in the same source code file.

Is it possible to run git bisect in a subset of the commits that changed this file?

+6
source share
3 answers

Yes you can ... In manpages you will find this line:

git bisect start [--no-checkout] [<bad> [<good>...]] [--] [<paths>...] 

after two "-" you put patches of files or directories.

Example:

 git bisect start -- arch/i386 include/asm-i386 or 
+13
source

Git bisect avoids commit checking (see "Avoid commit checking" on the man page): when you halve and git selects the commit for testing, you can override its selection with git reset --hard <commit you want> .

Using git log , you can find the last commit that affected the file (or subdirectory) - this will return the hash:

 git log -1 --pretty=format:%H -- path_that_you_are_interested_in 

So, every time git bisect prompts you to check, you should run this command to make sure that you only check the commits that affected somepath :

 git reset --hard $(git log -1 --pretty=format:%H -- somepath) 

Now there is one more thing that we need to take care of. If there are no interesting commits (i.e., no somepath that modify somepath ) between the last checked good commit and the commit currently selected by git bisect , we may end up in a loop. To avoid this, we should use a conditional sentence:

 #!/bin/bash last_good=$(git bisect log | tail -1 | sed 's/git bisect good //') last_interesting=$(git log -1 --pretty=format:%H -- lily/) if [ "$last_good" == "$last_interesting" ]; then # there are no commits modifying somepath between previously # tested and currently checked-out one, so it must be good git bisect good else git reset --hard $(git log -1 --pretty=format:%H -- somepath) fi 
+1
source

One way is to create a temporary branch starting back in a known good place and just copy through everything [i.e. a script], which commits this file, and then performs a bisection on this temporary branch. This way you will skip all irrelevant commits.

This has been discussed on the Git mailing list for the last month or so of $ gmane / 232114 . During the discussion, no easy way was discovered to forward commits that did not have the corresponding changes (perhaps something would be useful, although, as they say, patches were welcome)

0
source

Source: https://habr.com/ru/post/953778/


All Articles