0

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?

  • Should those other branches keep the history? Meaning, do you want one branch with 1 of the commits, the other branch with 2 of them and so forth. Or do you want each branch to only have a single one of those 3 commits? – Joseph Silber Aug 22 '16 at 12:28
  • I want each branch to only have a single one of those 3 commits – Finn is missing Aug 22 '16 at 12:41

4 Answers4

1

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

AnoE
  • 8,048
  • 1
  • 21
  • 36
  • `nor does it make sense to say that "each commit should be a different branch"` why? If you've worked on 3 separate things on the master branch, but then decide you want to explore each one of those separately, that would make perfect sense. – Joseph Silber Aug 22 '16 at 13:11
  • I'm (intentionally) splitting words here. A commit *is not* a branch. I have the feeling that the OP is used to other VCSs, where branches are indeed "first class" objects. – AnoE Aug 22 '16 at 13:14
  • From "I have branch which contains three commits" it sounds like the OP knows that a branch is not a commit. – Joseph Silber Aug 22 '16 at 13:15
  • Let's just see if he finds this answer useful. :) – AnoE Aug 22 '16 at 13:18
0

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.

Community
  • 1
  • 1
Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
0

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.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
0

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
Joseph Silber
  • 214,931
  • 59
  • 362
  • 292