How do I use 'git rebase -i' to rebase all changes in a branch?

后端 未结 8 1364
Happy的楠姐
Happy的楠姐 2020-12-12 20:36

Here\'s an example:

>git status
# On branch master
nothing to commit (working directory clean)
>git checkout -b test-branch
>vi test.c
>git add t         


        
8条回答
  •  悲哀的现实
    2020-12-12 20:51

    Use gitk (*nix), or gitx (OS X) or similar on other platforms, and have a look at which commit was the root of your branch. Then run:

    git rebase -i 
    

    For example, I have a repository that I inspected using gitx:

    gitx screencap

    Now that I know the root hash I can run this:

    git rebase -i 38965ed29d89a4136e47b688ca10b522b6bc335f
    

    And my editor pops up with this and I can rearrange/squash/whatever as I please.

    pick 50b2cff File 1 changes.
    pick 345df08 File 2 changes.
    pick 9894931 File 3 changes.
    pick 9a62b92 File 4 changes.
    pick 640b1f8 File 5 changes.
    pick 1c437f7 File 6 changes.
    pick b014597 File 7 changes.
    pick b1f52bc File 8 changes.
    pick 40ae0fc File 9 changes.
    
    # Rebase 38965ed..40ae0fc onto 38965ed
    #
    # Commands:
    #  pick = use commit
    #  edit = use commit, but stop for amending
    #  squash = use commit, but meld into previous commit
    #
    # If you remove a line here THAT COMMIT WILL BE LOST.
    # However, if you remove everything, the rebase will be aborted.
    #
    

    I'm sure there's some magic way to convince git to figure out the root of the tree automatically, but I don't know what it is.

    EDIT: That magic is this:

    git log master..other_feature | cat
    

    Which will show you all the commits on that branch, and piping to cat will disable the pager so you see the first commit immediately.

    EDIT: combining the above gives a fully automated solution:

    git rebase -i  `git log master..other_feature --pretty=format:"%h" | tail -n 1`~
    

提交回复
热议问题