The most simple way is to create an orphan branch and then to cherry-pick range of commits to the new branch.
# create orphan branch
git checkout --orphan <branch>
git cherry-pick <SHA-1>...<SHA-1>
git checkout --orphan
Create a new orphan branch, named , started from and switch to it. The first commit made on this new branch will have no parents and it will be the root of a new history totally disconnected from all the other branches and commits.
How to add the desired commit into different branch.
git cherry-pick <SHA-1>...<SHA-1>
Apply the change introduced by the commit at the tip of the master branch and create a new commit(s) with this change.
The syntax of the ...
is a commit range. grab all commits from start (exclude) to the last one.
Read out the full git cherry-pick
documentation for all the options you can use
git checkout --orphan newbranch branchname~158
, commit, and thengit cherry-pick
in the entire range ofbranchname~158..branchname
to get the 158 remaining commits copied. This assumes there are no internal branch-and-merge operations in that range; if there are, you will have to work harder, count carefully, and/or usegit filter-branch
or a script doing a lot ofgit commit-tree
s to copy them. – Triformgit filter-branch
seems to be the easiest way to get the job done. What's your suggestion to make it work? – Lienlienhardfilter-branch
is that (like rebase) it copies commits. Unlike rebase, it copies them by keeping the original source tree intact by default, and simply mapping parent IDs. If you're comfortable with graph theory, basically it makes a new subgraph by copying the nodes of the specified subgraph (from the X..Y notation) and then pastes the branch label on the tip-most copied commit. Each commit (and if you set an index or tree filter, the index or tree) is passed through the various filters before doing a newgit commit-tree
with-p
arguments from the map. – Triform