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

后端 未结 8 1325
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 21:04

    The issue with all provided solutions is, they do not allow you to rebase from the very first commit. If the first commit hash is XYZ and you do:

    git rebase -i XYZ
    

    You only rebase starting from the 2nd commit.

    If you want to rebase from the first commit you do:

    git rebase -i --root
    
    0 讨论(0)
  • 2020-12-12 21:07

    The problem with rebasing from a different branch

    The problem with git rebase -i master is that you may have merge conflicts that you don't necessarily want to deal with at the moment, or you may fix a conflict in one commit, only to fix it again in another commit during the course of the rebase.

    The problem with rebasing from a known commit

    The whole problem here is that you have to know which commit you have to refer to, either by its SHA, or HEAD~x, etc. This is only a minor annoyance but it is an annoyance.

    The better way

    If you instead want to rebase all the commits in your current branch, since the most recent commit it shared with its parent branch, you can add the following alias to .gitconfig:

    rbi = !sh -c \"git rebase -i `git merge-base $1 HEAD`\" -
    

    Usage

    git rbi parentBranch
    

    How it works

    This alias is just a shell script, that is using an argument which refers to the parent branch. That argument is passed into git merge-base in order to determine the most recent shared commit between that branch, and the current branch.

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