Skip to content

Instantly share code, notes, and snippets.

@natesire
Last active June 20, 2021 19:10
Show Gist options
  • Save natesire/ed95e6079f466941d52ffdc1213ccda2 to your computer and use it in GitHub Desktop.
Save natesire/ed95e6079f466941d52ffdc1213ccda2 to your computer and use it in GitHub Desktop.
Find Memory Leak or Bug with Git Bisect
git bisect start
git bisect bad
git bisect good commithashxxxxx
Git bisect will use the divide and conquer (binary search) strategy to speed up finding where a bug was introduced into the code.
IMPORTANT NOTE! It’s easy to mix up GOOD and BAD.
I realized that one of my unit/integration tests was faulty. My authentication system was redirecting to the wrong URL. However, this had been going on for a while. Users had been clicking the homepage icon to work around the problem. I needed a fast way to find when the bug was introduced. So I asked on the Python Slack group even though this happened on my Rails stack. XXXXX gave me this excellent solution. XXXX said that the solution came to him from reading the Pro Git book cover to cover.
Ok! Now that you found the first commit that contains the bad code! You need to compare that commit with the commit just before it…
You might need to remove each change one by one.
git diff badcommitxxx goodcomitxxx2 dir_name_xxx
See the commit prior to the current one you are on
git show HEAD^1
git checkout c661ac37a4b2196b4a3165e3460f0374c55a1eb8 — Gemfile.lock
You can delete each file 1 by 1 until you find the offending file. Then you might need to do a more granular comparison if there’s too many changes in 1 file. There is another reason why it’s good to commit 1 change at a time in a file. If you have a bunch of changes in 1 file, this will require more tedious granular comparisons.
Rails Steps
bin/rails db:environment:set RAILS_ENV=test
rails db:drop
rails db:setup
rails db:migrate
bundle exec rspec spec/integrations/sign_in_spec.rb
git stash
git bisect bad
rspec
git bisect good
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment