Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git - branch from previous commit and split commit history [duplicate]

Tags:

git

Is it possible to take the following setup and effectively split a branch into two branches based at a commit history?

eg: start with the following branch and commit history:

branch 1

git log:
commit 1
commit 2
commit 3
commit 4

Create branch 2 at commit 3's id and alter things to the following:

branch 1

git log:
commit 1
commit 2

branch 2
git log: 
commit 1
commit 2
commit 3
commit 4

Use case: I started a new feature on an existing branch not yet in master and committed a bunch of work to that branch. I want to branch off of the last commit before my work and take my work with me and leave that branch clean of all my work.

like image 717
JDillon522 Avatar asked Sep 25 '26 08:09

JDillon522


1 Answers

You can do a git checkout some_commit_sha to go to a specific commit, and git checkout -b some_branch_name to create a branch from a commit and go to that branch.

Putting the two of those together, you can do git checkout -b some_branch commit_2_sha

like image 122
Grexis Avatar answered Sep 28 '26 17:09

Grexis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!