Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
705 views
in Technique[技术] by (71.8m points)

git - How can I move a set of commits from master to a separate branch?

I have a series of commits (20+) that pertain to a particular feature that I am trying to remove from our master branch and into a separate branch.

I have a tag (rel_2009_07_18) on the commit that represents our latest stable release, so when on master, git log rel_2009_07_18..HEAD gets me the set of commits that I want to move to a separate branch. There are also some commits in this set that should remain, but I could just cherry-pick those as they are few.

I've looked at git filter-branch, but the commit option mentions leaving the changes in but removing the commit -- definitely don't want that. I also looked at git rebase, but that also mentions reapplying the commits to the upstream branch.

Is there a good option for moving these commits to a separate branch?

I'm not sure if this is a viable option and the repercussions in a distributed, albeit small (3 developers), environment. But could I perform the following little shift...

  1. Locally rename the master branch to master_plus_feature (or similar...)
  2. Checkout from the `rel_2009_07_18` tag
  3. Create a new branch master from this point
  4. Delete remote branches and re-push from local

Thoughts and suggestions? Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You write that you want to remove commits from 'master' branch. This might cause problems if 'master' branch was published (well, commits after rel_2009_07_18 are published), and somebody based their work on your 'master'. But perhaps it is not a problem in your case.

Both solutions below assume that you don't have uncomitted changes

If you can rewind 'master' branch:

$ git checkout master
$ git branch separate_branch
$ git reset --hard rel_2009_07_18

Now 'master' is at rel_2009_07_18 tag, and 'separate_branch" is at where 'master' was. The final result is exactly the same as in set of steps you proposed (rename 'master' to 'separate_branch', recreate 'master' at rel_2009_07_18), the only difference is in reflogs.

If you can't rewind 'master' branch

$ git checkout master
$ git branch separate_branch
$ git checkout rel_2009_07_18 -- .
$ git clean -df
$ git commit -m 'Reverted to state at rel_2009_07_18'

Note that this solution is not tested! YMMV.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...