I have a file that has broken somewhere down the line, and I have found the last point in which it was still fixed.
I would like to know how, using git, I can compare one file from two commits, or indeed if that is the best way to play this!!
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
To directly answer your question, suppose you want to compare the file
src/toaster.cbetween your currentmasterand the old commitf4l4f3l, you can just do:As an alternative, you can just look through all the changes to that file with:
More generally, however, if you’re trying to find the commit where a particular bug was introduced, git has a marvellous tool for this, called git bisect. If you tell this tool a working and non-working commit, it will give you a series of commits to test in between those, using a binary search strategy.
You would start bisecting with the command:
Then if your current commit has the bug, you just do:
Next, you need to find an older commit that definitely didn’t have the bug. This might have a particular tag, or perhaps you’ll just pick a commit that was some months ago. Suppose that one is called
a12b3d, then you would do:At that point, git will work out the next commit you’ll need to test, and do
git checkoutto move you to that commit. (These checkouts will all be with “detached HEAD”, so your original branch pointer is unchanged.) You then test that commit, and rungit bisect goodorgit bisect baddepending on whether it has the bug or not. This binary search between the revisions will quickly narrow down to the first bad commit, and report which one it is. Then to go back to what you were doing, you can do: