I'm doing some regression testing looking for a bug I introduced in a project, basically cloning the current repository, removing x commits and testing to see where the bug appeared... I wanted to explore other alternatives.
The usual thing is to create new branches from the current moment of work in the repository and at this moment I wish I had created a new branch x commits ago... Since I didn't, I would like to know if it is possible to create a new branch from a given commit , for example from 4 commits ago... any way to do it?
Although Nicolas' answer directly answers your question, since you are looking for alternative ways to isolate the commit that introduced a defect to your project, I suggest you investigate using git bisect .
git bisect
it is precisely designed to optimally retrieve (using binary search) the commit that introduced a problem.For it to work properly, you must provide 2 initial reference points:
For example, let's say you know that your current branch has the problem. And although you don't know in which precise commit the problem appeared, you are sure that 100 commits ago (say) the problem didn't exist. Then you would start the investigation as follows:
... where
git bisect bad
it indicates that the current commit has the problem, andgit bisect good abc123def456
it indicates that you are at least sure that the problem did not exist in the commitabc123def456
.With this information in hand, git now automatically chooses (using the binary search algorithm) a commit to checkout.
Now it's your turn to test that commit to see if the defect exists or not. If the chosen commit has the defect, then you must indicate it with the command:
But if the commit does not have the defect, then you must indicate it with:
Depending on what you reply, git now automatically picks another commit to checkout. And again, you have to test this commit to see if the defect is present or not, and you must indicate it with
git bisect bad
orgit bisect good
.This process continues successively (and binary to minimize the number of commits you need to examine) until eventually git tells you which commit is guilty of introducing the defect.
Finally, once the investigation is over, you end up with:
...and this returns you to your initial state before you started the
git bisect
.You can use this option:
It is also valid to use symbolic references, but I use this option on a daily basis, it seems simpler and more flexible to me.