Search code examples
gitbranchcommitgit-branch

Branch from a previous commit using Git


If I have an n number of commits, how can I create a branch from the n-3 commit?


Solution

  • Create the branch using a commit hash:

    git branch branch_name <commit-hash>
    

    Or by using a symbolic reference:

    git branch branch_name HEAD~3
    

    To checkout the branch while creating it, use:

    git checkout -b branch_name <commit-hash or HEAD~3>