Question

I have a project on github that is cloned to a local repo. Somewhere in the last 40 commits I changed something that broke the code without realizing it till now. So I have a two part question:

What is the best way to "roll back", or I guess, revert my local repo to the commit 40 commits ago. Without pushing to the master repo obv.

And what is the best way to then step forward through the commits to allow me to build after each step and find the one that caused the code to break?

i.e. In my head I imagine a sequence something like:

  • rollback to commit
  • build code
  • did it break?
  • if not move forward to next commit

loop:

  • did it break
  • if not move forward to next commit and jump to loop
  • else I've found the change with the issue.

Im using egit plugin in eclipse, though can use git bash if that makes things simpler, but I am pretty much a git cmd n00b, so I will need it step by step.

Was it helpful?

Solution

To go back to the last known working commit:

$ git checkout HEAD~40

Will checkout a detached HEAD state to 40 commits before the HEAD (or you can just use the commit hash).

To find the bad commit you can use git bisect, like so:

$ git bisect start
$ git bisect HEAD
$ git bisect HEAD~40

This process will do a binary search on the bad commit-good commit range to help you find where the bug is. See the docs for more details on how to run this process. But generally speaking, it's best to have a script that can run your tests (and exists with exit code 0 upon success), which can automate the entire process:

$ git bisect run run_tests.sh args
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top