git command for making one branch like another

前端 未结 9 1169
半阙折子戏
半阙折子戏 2020-11-22 11:36

I\'m trying to take a branch with changes and bring it back to be identical to the upstream it diverged from. The changes are both local and have been pushed to github, so n

相关标签:
9条回答
  • 2020-11-22 11:56

    Heavy handed, but hell, what can possibly go wrong?

    • Check out the branch X you want to look like the Y
    • cp -r .git /tmp
    • Check out branch Y git checkout y
    • rm -rf .git && cp -r /tmp/.git .
    • Commit & push any difference
    • DONE.
    0 讨论(0)
  • 2020-11-22 12:01

    Another simulation for git merge -s theirs ref-to-be-merged:

    git merge --no-ff -s ours ref-to-be-merged         # enforce a merge commit; content is still wrong
    git reset --hard HEAD^2; git reset --soft HEAD@{1} # fix the content
    git commit --amend
    

    An alternative to the double reset would be applying the reverse patch:

    git diff --binary ref-to-be-merged | git apply -R --index
    
    0 讨论(0)
  • 2020-11-22 12:03

    Use git reset BACKWARDS!

    You can make a branch look like any other commit with git reset, but you have to do it in a round-about way.

    To make a branch on commit <old> look like a commit <new>, you can do

    git reset --hard <new>
    

    in order to make <new> the contents of the working tree.

    Then do

    git reset --mixed <old> 
    

    to change the branch back to the original commit but leaving working tree in the <new> state.

    Then you can add and commit the changes, in order to make your branch exactly match the contents of the <new> commit.

    It's counter-intuitive that to move from the <old> state to the <new> you need to do a git reset from <new> to <old>. However with the option --mixed the working tree is left at <new> and the branch pointer set to <old>, so that when the changes are committed the branch looks how we want.

    Warning

    Don't lose track of your commits, e.g. forget what <old> is when doing git reset --hard <new>.

    0 讨论(0)
提交回复
热议问题