Get the difference between two branches in Git

后端 未结 3 1508
無奈伤痛
無奈伤痛 2020-12-22 23:23

I did following (I simplified this comparing to a reality):

  • created a branch Branch1, switched to it
  • added file File1
3条回答
  •  别那么骄傲
    2020-12-23 00:20

    I would do an interactive rebase on HEAD~2 and squash the last two commits together. Assuming that you want to keep the history as is in Branch1 and simplify it in Branch2, do the following (current branch Branch1):

    git checkout -b Branch2
    git rebase -i 'HEAD~2'
    

    An editor will open up, showing something like

    pick 1b58da0 Added File1, changed File2
    pick d3f4f51 Delete File1
    

    and many explanatory comments how rebasing works. Change the last commit to a squash and close the editor.

    pick 1b58da0 Added File1, changed File2
    squash d3f4f51 Delete File1
    

    A new editor will open up where you can specify the new commit message. It would probably now just read

    Changed File2

    Close it and you're done, both commits are squashed together on Branch2 and Branch1 retains your original history. Note that if you don't need to retain the original history, you can just skip checking out Branch2 and work directly on Branch1. Only do that if you haven't published your last two commits on Branch1 already.

提交回复
热议问题