What is the correct way to merge upstream without losing changes?

前端 未结 3 1308
囚心锁ツ
囚心锁ツ 2021-02-02 04:07

I\'m in a bit of a pickle.

I started development on a fork of a repo a few months ago. I made some changes. I was about to push my code back to the master as a pull re

3条回答
  •  忘了有多久
    2021-02-02 04:55

    Ok, if your repo is fubar, then here's steps to recover:

    $ git remote update     # make sure origin and upstream are up to date
    $ git checkout master
    $ git branch my_changes   # just to make sure my stuff isn't lost
    $ git reset --hard upstream/master
    $ git status
    # On branch master
    # Your branch is behind 'origin/master' by 8 commits, and can be fast-forwarded.
    #
    

    Ignore that fast-forward crap, it won't help you, just reset the master.

    $ git push origin +master   # now, fork matches upstream/master
    

    Now, how to recover the previous work so it's reviewable?

    $ git diff --no-ext-diff --no-prefix master..my_changes > patchfile
    $ patch -p0 < patchfile   # apply the patchfile to master
    $ git diff     # to verify patch visually.
    $ rm patchfile   # clean up
    $ rake spec      # verify that it really works.
    $ git add .
    $ git status     # double triple verify
    $ git commit .
    $ git push origin master
    

    That sucked. I tried doing rebase, but it kept saying "no changes" and didn't ask me to check anything in? I'm pretty sure I don't understand what's going on here, which is why I posted my struggle so someone can explain how I could have avoided this mess and gotten from A to B more elegantly now that it's fully documented. I'm willing to chalk this up to git inexperience, but I figure that pulling changes should be a really common thing in git -- I must be missing something.

提交回复
热议问题