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
662 views
in Technique[技术] by (71.8m points)

github - Cleanup git master branch and move some commit to new branch?

I have a clone of a repo on Github in which I created a new feature for upstream. The problem is, I did that in my master branch which also contains other stuff I pulled in from other clones. That totally broke my ability to create a reasonable pull request.

So I want to do the following:

  1. Restore my master branch to be exactly the same as upstream master.
  2. Create a new branch.
  3. Move some of my old commits to the new branch.
  4. Create a pull request off the branch.

And, in the future, I will do all my work in branches and create pull requests off them while leaving my master branch alone and just merging down whatever comes in from upstream.

My questions are:

  1. Is this a reasonable approach?
  2. How would I actually do steps 1 and 3?
See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

Make a new branch to hold stuff

$ git branch old_master

Send to remote for backup (just incase)

$ git checkout old_master
$ git push origin old_master

Reset local master to the commit before you started modifying stuff

$ git checkout master
$ git reset --hard 037hadh527bn

Merge in changes from upstream master

$ git pull upstream master

Now DELETE master on remote repo

On github this won't work without first going into the admin section for the fork and setting the default branch to something other than master temporarily as they try and protect you from blowing stuff away.

$ git push origin :master

And recreate it

$ git push origin master

On github you should now set the default branch back to master


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

...