amn amn - 16 days ago 7
Git Question

git: How to move last N commits made to master, into own branch?

I have a repository where I had been working on

master
branch having last adde some 10 or so commits which I now wish were on another branch, as they describe work that I now see is experimental (I am still learning good git practices). Basically I would like to have these last 10 commits start from a point in
master
to form own branch, so that I can have
master
for "release"/"stable" kind of commits only.

What I have now, rightmost
X
is the last commit actually good for release:

b--b (feature B)
/
X--X--X--Z--Z--Z--Z--Z--Z (master)
\
a--a--a (feature A)


You can see that both
X
and
Z
are on master, while what I want is commits marked by
Z
(my "feature Z" work) to lie on their own feature branch, so tip of
master is at the rightmost
X`. To illustrate the desired graph:

b--b (feature B)
/
X--X--X (master)
\ \
\ Z--Z--Z--Z--Z--Z (feature Z - the new branch I want)
\
a--a--a (feature A)


That way I will have my
master
reserved for quality stuff, merging in A, B and Z features as needed.

So how do I move the "Z" commits on their own branch?

JB. JB.
Answer
git checkout master
git branch feature-Z
git reset <commit_id>

where commit_id is an identifier of that last X commit before b branches off.