Hey you! Are you using git? I thought so. But how much is git helping in your daily coder's life? How much time do you spend identifying where a bug is? And how much time do you spend identifying why anyone introduced that bug in the first place.
But how much is git helping in your daily coder's life? How much time do you spend identifying where a bug is? And how much time do you spend identifying why anyone introduced that bug in the first place.
I've found recently that git, if well used, can help us in more ways than just keeping track of changes for the sake of tracking them but to help us in our endless quest of fixing the bugs we coded a while back.
This git command has a simple concept. To find the commit that introduced a bug we must inform git about a bad and a good revision. After that, git, using a binary search algorithm, presents us with possible commits that may be good or bad, we, humans (for now, however, you can script this step, see https://git-scm.com/docs/git-bisect#_bisect_run), just tell it if this commit still has that bug or not.
With this algorithm, we are able to identify the origin of a bug for example in a range of 675 commits with just 10 steps.
The commands used in the following demonstration are:
git log # see commits
git log --pretty=format:"%h - %an, %ar : %s" # see commits displayed in a prettier and smaller way
git bisect bad # mark current version as bad
git checkout <revision> # go to prior commit
git bisect good # mark current version as good
git show <revision> # show changes on a specific revision
git bisect reset # resets current bisect session
In this demonstration, we have a file “index.html” with a bug that says that git bisect is a “bad” tool.
As we can see in the image above, that text doesn’t look right, someone changed a word from “good” to “bad”. One way we can approach this is just checking the git history for that line, but if we have many commits that’s slow and git bisect can rescue us.
We can first run a git log to see what we are dealing with.
git log --pretty=format:"%h - %an, %ar : %s"
In this case, we see that the author of the commits doesn’t even use helpful commit messages (so why are you even reading his stuff?).
We know for a fact that when we added the file the contents the word “bad” wasn’t there so let's check out that revision.
git checkout 16d6ba6
So with this, we can start our investigation.
git bisect start # this will also checkout the head version (if we were in a detached head state)
git bisect bad # mark this as bad
git bisect good 16d6ba6 # mark this revision (or tag) as good
We can now see that in 3 steps we’ll find the first bad commit. So let’s get to it.
We can check out what changed in this commit easily with the git show command.
Don’t forget that you checked out a commit, if you are going to make changes, please use the git bisect reset command or git checkout before start working on a fix.
It doesn’t really matter the size of the project, or the number of files in it, as long as you can check out previous revisions and confirm that that revision is buggy or not easily, through the command line, your IDE, your browser or by compiling your application you should be able to use this to your advantage.
git bisect skip # in case you don’t want to test the suggested revision, you can skip it
This is an article written by Pedro Caseiro, Software Engineer @md3
Pedro started out as a self-taught PHP and Javascript web developer. With about 6 years of professional experience, he's a fan of SPA. Recently he started to transition to back-end Python developments.
Cookie | Duration | Description |
---|---|---|
_GRECAPTCHA | 6 months | Set by google api recaptcha. Used to validate form submission. It does not store any personal data. |
cookielawinfo-checkbox-analytics | 11 months | This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics". |
cookielawinfo-checkbox-necessary | 11 months | This cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary". |
CookieLawInfoConsent | 6 months | The cookie is set by the GDPR Cookie Consent plugin and is used to store the consented cookie categories options. It does not store any personal data. |
pll_language | 6 months | The cookie is set by Polylang plugin and is used to store the selected language. It does not store any personal data. |
viewed_cookie_policy | 11 months | The cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data. |
Cookie | Duration | Description |
---|---|---|
_ga | 2 years | This cookie is set by Google Analytics. The cookie is used to calculate visitor, session, campaign data and track site usage for the site's analytics report. Cookies store information anonymously and assign a randomly generated number to identify unique visitors. |
_ga_122676620_1 | 2 years | Set by Google to distinguish users. Connections on the same site only. |
_gac_gb_UA-122676620-1 | 90 days | This cookie is set by Google Analytics. Connections on the same site only. |
_gid | 24 hours | This cookie is set by Google Analytics. The cookie is used to store information about how visitors use a website and helps create an analytics report on how the website is doing. The data collected, including the number of visitors, the source from which they came and the pages visited in an anonymous form. |