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:
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:
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.
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
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With