Finn is missing
Finn is missing

Reputation: 47

how can create a new branch from already existing commits?

I have branch which contains three commits. Now I want to create three new branches of these three commits, each commit should be separate branch, how can I achieve this?

Upvotes: 0

Views: 286

Answers (4)

Joseph Silber
Joseph Silber

Reputation: 219938

Create new branches from the original commit, then cherry-pick the commit you want:

# From the master branch
git branch branch-1 HEAD~2

git checkout -b branch-2 master~3
git cherry-pick master~1

git checkout -b branch-3 master~3
git cherry-pick master

Upvotes: 0

AnoE
AnoE

Reputation: 8345

About git branches

Reading the question and the comments (from the OP), I believe there is confusion about what a branch is, in git. A branch is simply a little sticky note pointing to a commit. Branches are not a "heavy" object in git, so you do not really "create new branches with those commits", nor does it make sense to say that "each commit should be a different branch"; you just label one commit "mybranch1", the other commit "mybranch2" and so on. The commands have been given by @TimBiegeleisen (git branch <name> <commit>).

Reading a little primer on how git structures its data may help. http://gitready.com/beginner/2009/02/17/how-git-stores-your-data.html

Upvotes: 1

Oliver Charlesworth
Oliver Charlesworth

Reputation: 272497

I'm assuming that you want three independent branches, each with a single commit taken from the original branch.

You can do this by creating orphan branches, and then cherry-picking the commits.

For example:

git checkout --orphan branch1
git reset --hard
git cherry-pick <hash-of-commit-1>

and so on for the other two commits.

Upvotes: 0

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521239

You want to use git branch in the following way:

git branch branch_name <sha1-of-commit>

So since you want three branches from these three commits you should use:

git branch branch1 <commit #1>
git branch branch2 <commit #2>
git branch branch3 <commit #3>

To find the SHA-1 hashes of the three commits, you can type git log on the branch in question. Inspect the log until finding the three commits from which you want to branch.

Upvotes: 0

Related Questions