Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Go back N commits in Git to find commit that causes test regressions

Is there a command that will let me checkout a commit based on its distance from the current commit instead of using commit IDs?

Use Case

Basically I am thinking of setting up a cron job type script to do the following on a build server:

  • Pull down the latest of a specific git branch (git pull dev).
  • Build it, run the tests.
  • If the pass percentage is lower than the last stored percentage:
    • Recursively go back a commit, build, run tests, until it finds the commit where the percentage changed.
    • Log the commits that introduced regressions.

I have a rough idea for how this would hinge together but it won't work unless I can go back one commit periodically.

If there is no specific command I suppose I could grep the commit log and take the first one each time?

I appreciate any ideas or help!

Unlike: How to undo last commit(s) in Git?

I want to go back "N" number of commits.

like image 463
chrispepper1989 Avatar asked May 24 '13 15:05

chrispepper1989


People also ask

How can you find which commit caused a bug?

DESCRIPTION. This command uses a binary search algorithm to find which commit in your project's history introduced a bug. You use it by first telling it a "bad" commit that is known to contain the bug, and a "good" commit that is known to be before the bug was introduced.

How do you look at a previous commit?

If you want to look at previous commits, you can use git log and its many arguments. If you want to checkout an actual commit to view the files in an editor, just use git checkout to move to any commit you want. When you are finished, just do git checkout master to return to your current state.


1 Answers

git checkout HEAD~N

will checkout the Nth commit before your current commit.

If you have merge commits in your history, you may be interested in

git checkout HEAD^N

which will checkout the Nth parent of a merge commit (most merge commits have 2 parents).

So, to always go back one commit following the first parent of any merge commits:

git checkout HEAD^1

You may also be interested in git bisect - Find by binary search the change that introduced a bug.

like image 181
Peter Lundgren Avatar answered Sep 28 '22 10:09

Peter Lundgren