当前处在B分支,要得到上面的结果,只需执行如下命令
git rebase --onto master b的commit hash code B
这个不仅可以针对不同的分支,也能作用于同一个分支上。所以针对上面的情况可以只对分支B进行操作,等价命令如下:
git rebase --onto a的commit hash code b的commit hash code B --interactive
当我们要修改commit信息的名称时,如果要修改的commit处在第一个时,可以使用
git commit --amend
如果不是第一个时,我们就要使用到rebase的--interactive可选参数了,可以简写为-i。
git rebase -i commit hash code
参数后面的commit hash code为需要修改的commit的前一个。执行之后就会出现如下类似的信息:
pick 137cf0a First coommit pick 163dc38 Second commit # Rebase f9aee6e..163dc38 onto f9aee6e (2 command(s)) # # Commands: # p, pick = use commit # r, reword = use commit, but edit the commit message # e, edit = use commit, but stop for amending # s, squash = use commit, but meld into previous commit # f, fixup = like squash, but discard this commit's log message # x, exec = run command (the rest of the line) using shell # # These lines can be re-ordered; they are executed from top to bottom. # # If you remove a line here THAT COMMIT WILL BE LOST. # # However, if you remove everything, the rebase will be aborted. # # Note that empty commits are commented out
根据提示我们可以有6个可选择的操作。相信提示已经说的很明显了,对于我们这种要修改First coommit的情况,需要使用r。
r 137cf0a First commit pick 163dc38 Second commit
执行之后会跳到修该First coomit的界面,进行修改即可。
First commit # Please enter the commit message for your changes. Lines starting # with '#' will be ignored, and an empty message aborts the commit. # # Date: Thu Jan 26 23:07:10 2017 +0800 # # rebase in progress; onto f9aee6e # You are currently editing a commit while rebasing branch 'master' on 'f9aee6e'. # # Changes to be committed: # new file: file1
至于其他的操作项,有兴趣的可以自己去尝试一下。例如s操作就可以用来合并commit。 branch
相信branch都很熟悉,我这里要说的是他的另一种可能会用到的情况。场景是这样的:如果在你进行创建新的分支时,并不想从当前的commit信息节点进行创建分支。