Creating a git branch for previous commits: what's the best way to do it?
Imagine the following:
- A bunch of related commits are done on a 'development' branch 开发者_StackOverflow社区
- These commits should have actually been done to a 'feature_x' branch
- The 'feature_x' branch should be merged into the 'development' branch
Graphed example:
Current situation:
development (HEAD) A--B--C--D--E
Desired situation:
development A--B
\
feature_x C--D--E
How do I create this feature branch, group these previous commits into the branch, and make the 'development' branch look like no individual commits have been done to it?
You don't really need much to to that:
git checkout devel
git checkout -b feature_x
# now devel and feature_x refer to commit E
git branch -f devel <hash of commit B>
# now you're still on feature_x, but devel is at commit B
A--B--C--D--E(dev branch)
\
\
F--G(feature branch)
I assume you want to become like this
A--(dev branch)
\
\
F--G--B--C--D--E(feature branch)
You can use git rebase
to do this
on the HEAD of dev branch create a temp branch for rebasing purpose
git branch temp
git rebase feature_branch
move your devbranch to commit A
git checkout dev_branch
git reset --hard <shasum of A>
move your feature branch to commit E and delete temp branch
git checkout feature_branch
git reset --hard <shasum of E>
git branch -D temp
精彩评论