How to create branch from a previous commit using git

In this article you learn that how to create branch from a previous commit using git?

 

To create a new branch from a previous commit using Git, you can use the following steps:

 

  • Open a terminal or command prompt in the root directory of your Git-managed project.
  • Run the following command to find the commit hash of the commit you want to base your new branch on:

 

git log

 

Copy the commit hash of the commit you want to base your new branch on.

Run the following command to create a new branch based on the previous commit:

git branch new-branch <commit-hash>

 

This command tells Git to create a new branch named new-branch based on the specified commit hash. The new branch will contain the state of your project as it was at the time of the specified commit.

 

  • Run the following command to switch to the new branch:

 

git checkout new-branch

 

This command tells Git to switch to the new branch, so that you can start making changes from the state of your project as it was at the time of the specified commit.


here are the following few examples?



Create the branch using a commit hash:

git branch branch_name <commit-hash>

 

Create the branch using a symbolic reference:

git branch branch_name HEAD~3

 

To checkout the branch while creating it,

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

 

Note: Creating a new branch from a previous commit allows you to work on a specific version of your project without affecting the state of other branches. You can make changes to the new branch, and then merge the changes back into the main branch when you're ready.


Tags:

git

Share:

Related posts