Create new branch from commit ID

Create new branch from commit ID

You can create a new branch from the previous commits of any branch without reverting commits. Just one simple command!

ยท

1 min read

Due to unwanted push/pull/merge, your branch often messes up with unwanted code. Some unnecessary and buggy commit lines up in your branch's commit history.

In this case, you wish to create a new branch from the last correct commit.

The naive approach is to hard reset the branch to specific commit, then create a new branch. ๐Ÿ˜

But hold on... You can accomplish that without the resetting stuff. ๐Ÿ˜Ž

Here are the steps :

  1. Checkout to the branch you need to create a new branch from.

  2. Run git log: Get the ID of the commit from which you want to create a new branch.

  3. Execute git checkout -b <branch_name> <commit_id>

And there you go... ๐ŸŽ‰

Now you have created a new branch based on the commit_id that was entered in the command.

ย