Go back N commits in Git to find commit that cause

2019-04-21 01:55发布

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.

2条回答
仙女界的扛把子
2楼-- · 2019-04-21 02:32
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.

查看更多
你好瞎i
3楼-- · 2019-04-21 02:53

Iterating over each commit and running the tests could be really inefficient if the number of commits is much larger than the number of commits that introduce errors. There's no need to run the tests against every commit in the history, to find the one that introduced the error! You really should look at git bisect, particularly git bisect run.

The question indicates you want to find every commit that introduced an error since some point in history, not just the most recently introduced one. I don't think you can do that with one invocation of git bisect; you'll have to put it in a loop in a small script.

查看更多
登录 后发表回答