Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

github

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?
like image 810
Manfred Moser Avatar asked May 06 '11 19:05

Manfred Moser


People also ask

How do you move pushed commits to another branch?

Undo and Commit to New Branch Use git log to check how many commits you want to roll back. Then undo the commits with git reset HEAD~N where “N” is the number of commits you want to undo. Then create a new branch and check it out in one go and add and commit your changes again.


3 Answers

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

like image 159
wewals Avatar answered Oct 19 '22 19:10

wewals


This is almost a reasonable approach, but you're possibly taking things a bit out of order. First thing to do is it create a new branch where your current master points, so that you don't lose the convenient reference to the work you've already done:

git branch mywork-orig master

After that, you can reset master to upstream's view (assuming you have master checked out):

git reset --hard origin/master

Then you can make you own branch with the intended changes:

git checkout -b mywork

Make the changes that you want (cherry-pick them from mywork-orig, etc.), and send a pull request for that.

like image 32
Phil Miller Avatar answered Oct 19 '22 17:10

Phil Miller


This is late, but didn't see anyone suggest this much simpler method:

# make sure we're in master
git checkout master
# create new branch from current master
git branch tmp_master
# grab most recent changes from upstream w/o applying them
git fetch upstream
# force reset local master to match upstream/master
git reset --hard upstream/master

You saved your local changes to tmp_master and have forced updated master to match the most recent upstream/master. Now to get origin/master looking like upstream/master:

git push -f origin master

Now go ahead and cherry-pick out the commits, or rebase the changes on top of the current master. After which you would already have your new devel branch.

What you wanted to do is totally possible, just not in the order you asked. And it seemed others forgot you can fetch remote changes without actually applying them. Makes life a lot more simple.

like image 6
Trevor Norris Avatar answered Oct 19 '22 17:10

Trevor Norris