0

I'm trying to track down an error that was introduced somewhere in a certain commit. Unfortunately, that commit consists of a lot of files and I'm not sure where the problem arose. I'd like to do something like "git bisect", but that would only help if I could split the problem commit into a bunch of smaller commits.

I don't have any problem with creating a branch, reverting the problem commit, then re-applying it as a series of commits, but I don't know how to do that. Researching this question here on Stack Overflow results in a lot of answers related to rewriting the history (like this or this), which I don't want to do because the commit in question happened in the past and it's been pushed to a central repository.

How can I split a single commit modifying multiple files into a series of commits, one for each file?

Edit: maybe I haven't explained the question clearly enough.

I have a repository with commits that look like this

A --- B --- C --- D

Commit B is a problem. A bug was introduced and it's been narrowed down to commit B. But commit B consists of a large number of changes to a large number of files, so it's unclear exactly what caused the bug.

What I'd like to do is this:

A --- B --- C --- D --- revert B --- B1 --- B2  --- ... --- Bn

Where B1, B2, ... Bn are commits of the individual files that were changed in commit B.

If I can get the problematic commit broken down, I can then use git bisect to narrow down the problem further.

Is there a way to do this?

Kryten
  • 15,230
  • 6
  • 45
  • 68
  • 1
    My recommendation here is to just fix the bug, and apply the patch. Rewriting the history of a published branch has all sorts of negative caveats associated with it, not to mention that doing what you want would require quite a bit of Git olympics. – Tim Biegeleisen Feb 19 '20 at 17:07

1 Answers1

0

Given you already know which commit introduced the error and is just trying to use git to help you understand more about the code:

1.) Create a branch on the commit you want to investigate

e.g. git branch investigation abcdef1 (where abcdef1 is the hash or the problematic commit)

ps: investigation is just a name I chose for the branch... Can be something else...


2.) Go to the new branch you've just created

e.g. git checkout investigation


3.) Undo the problematic commit (only affects this new branch, so no damage to the original history in your main branch)

e.g. git reset HEAD~1

At this point, all the files that were added/changed/deleted will be back to their original state just before the commit was done. You can do a git status to see all the differences.

From here, you can start creating the commits as you wish, either one by file, or grouping some of them... It's up to you.


Of course, this is just for you to track down where the bug is... You would not merge this branch back to master or anything like that... It's just a throwaway branch for the purposes of investigating the bug... Once you know where the bug is, go back to master (or whatever branch is your trunk one) and fix the bug there, and then just delete the investigation branch.

C. Augusto Proiete
  • 24,684
  • 2
  • 63
  • 91
  • Brilliant! Thanks! It seems like a bit of a hack to use `git reset` in this way, but it should work. Thanks very much, I'll give it a try. – Kryten Feb 19 '20 at 18:15