Can I create a new Git branch from a previous "commit"?


I am doing some regression tests looking for an error that I introduced in a project, basically cloning the current repository, eliminating x commits and trying to see where it appeared in error ... I wanted to explore other alternatives.

The usual thing is to create new branches from the current working time in the repository and at this time I wish I had created a new branch x commits back ... As I did not, I would like to know if It is possible to create a new branch from a commit determined , for example 4 commits back ... any way to do it?

asked by Orici 12.10.2017 в 13:35

2 answers


You can use this option:

git branch <nombre-rama> <sha1-commit>

It is also valid to use symbolic references, but this option is used daily, it seems simpler and more flexible.

answered by 12.10.2017 / 13:44

Although Nicolas' answer answers directly to your question, since you are looking for alternative ways to isolate the commit that introduced a flaw in your project, I suggest you investigate the use of git bisect .

git bisect is designed precisely to optimally achieve (using a binary search) the commit that introduced a problem.

To work properly, you must provide 2 initial reference points:

  • a commit that is known to include the defect (probably the current commit)
  • a commit that definitely does not include the defect
  • For example, let's say you know that in your current branch you have the problem. And although you do not know in what precise commit the problem appeared, you are sure that it does 100 commits (to say something) the problem did not exist. Then you would start the investigation in the following way:

    git bisect start
    git bisect bad
    git bisect good abc123def456

    ... where git bisect bad indicates that the current commit has the problem, and git bisect good abc123def456 indicates that at least you are sure that the problem did not exist in commit abc123def456 .

    With this information in hand, git now automatically chooses (using the binary search algorithm) a commit to a checkout.

    Now it's your turn to try 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:

    git bisect bad

    But if the commit does not have the defect, then you must indicate it with:

    git bisect good

    Depending on what you say, now git automatically chooses another commit to which you checkout. And again, you have to try this commit to see if the defect is present or not, and you must indicate it with git bisect bad or git bisect good .

    This process follows successively (and in a binary way to minimize the number of commits you need to examine) until eventually git tells you what the commit is guilty of having entered the defect.

    Finally, once the investigation is finished, you end up with:

    git bisect reset

    ... and this returns you to your initial state before you started the git bisect .

    answered by 12.10.2017 в 14:38