git rebase -i 使用记录

    xiaoxiao2021-03-25  124

    用 git rebase -i 命令即可实现,下面我将演示一下这个命令: ①首先,我有一个新的仓库,只有一个空提交。 Mac: demo$ git hi dccff7e 2015-09-18 | initial commit (empty) [fuhaiwei] ②在master分支添加一个文件,并提交。 Mac: demo$ touch master_file Mac: demo$ git add master_file Mac: demo$ git commit -m "add master_file" [master a4959da] add master_file 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 master_file Mac: demo$ git hi a4959da 2015-09-18 | add master_file [fuhaiwei] dccff7e 2015-09-18 | initial commit (empty) [fuhaiwei] ③新开一个分支develop,并添加三个提交。 Mac: demo$ git branch develop Mac: demo$ git checkout develop // 省略命令若干 Mac: demo$ git hi 28646e2 2015-09-18 | add develop3 [fuhaiwei] c341f75 2015-09-18 | add develop2 [fuhaiwei] 22f95c8 2015-09-18 | add develop1 [fuhaiwei] a4959da 2015-09-18 | add master_file [fuhaiwei] dccff7e 2015-09-18 | initial commit (empty) [fuhaiwei] ④使用 rebase -i 命令合并历史提交 Mac: demo$ git rebase -i a4959da 会出现如下界面: &lt;img src="https://pic1.zhimg.com/1cefcbf82450e17d61b521fda5fde164_b.png" data-rawwidth="956" data-rawheight="374" class="origin_image zh-lightbox-thumb" width="956" data-original="https://pic1.zhimg.com/1cefcbf82450e17d61b521fda5fde164_r.png"&gt;将想要合并进前一个提交的提交,前面的pick 改成 s,保存退出。 将想要合并进前一个提交的提交,前面的pick 改成 s,保存退出。 &lt;img src="https://pic4.zhimg.com/ca458f117adbd2d9bc1c8033da7676a7_b.png" data-rawwidth="958" data-rawheight="374" class="origin_image zh-lightbox-thumb" width="958" data-original="https://pic4.zhimg.com/ca458f117adbd2d9bc1c8033da7676a7_r.png"&gt;这时又会进入一个界面,是让你输入想要三个提交合并后的提交信息。 这时又会进入一个界面,是让你输入想要三个提交合并后的提交信息。 &lt;img src="https://pic3.zhimg.com/a14e1456a9e48645e1055c1462699e3e_b.png" data-rawwidth="856" data-rawheight="330" class="origin_image zh-lightbox-thumb" width="856" data-original="https://pic3.zhimg.com/a14e1456a9e48645e1055c1462699e3e_r.png"&gt;将前面的都删掉,在第一行输入想要的提交信息即可,第二行有个空行最好保留。保存退出。 将前面的都删掉,在第一行输入想要的提交信息即可,第二行有个空行最好保留。保存退出。 &lt;img src="https://pic2.zhimg.com/4d94c316440b4cbda8d7c944bed5ce19_b.png" data-rawwidth="954" data-rawheight="126" class="origin_image zh-lightbox-thumb" width="954" data-original="https://pic2.zhimg.com/4d94c316440b4cbda8d7c944bed5ce19_r.png"&gt; Mac: demo$ git rebase -i a4959da [detached HEAD 5ec37e0] add develop1 develop2 develop3 Date: Fri Sep 18 09:17:13 2015 +0800 3 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 develop1 create mode 100644 develop2 create mode 100644 develop3 Successfully rebased and updated refs/heads/develop. Mac: demo$ git hi 5ec37e0 2015-09-18 | add develop1 develop2 develop3 [fuhaiwei] a4959da 2015-09-18 | add master_file [fuhaiwei] dccff7e 2015-09-18 | initial commit (empty) [fuhaiwei] 可以看到,这时三个提交已经合并了。 ⑤将develop分支合并到master分支 Mac: demo$ git checkout master Switched to branch 'master' Mac: demo$ git merge develop Updating a4959da..5ec37e0 Fast-forward develop1 | 0 develop2 | 0 develop3 | 0 3 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 develop1 create mode 100644 develop2 create mode 100644 develop3 Mac: demo$ git hi 5ec37e0 2015-09-18 | add develop1 develop2 develop3 [fuhaiwei] a4959da 2015-09-18 | add master_file [fuhaiwei] dccff7e 2015-09-18 | initial commit (empty) [fuhaiwei] 总结:git rebase -i 可以交互性的改变提交历史,包括但不限于改变某次提交的内容,改变提交的顺序,去除某次提交,合并某些提交。 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 当然上面的问题还有更暴力的方法可以解决,就是先 reset 到修改之前的状态,然后在重新发起一次提交。 ① 首先恢复到合并之前的状态 Mac: demo$ git checkout develop Switched to branch 'develop' Mac: demo$ git reset --hard 28646e2 HEAD is now at 28646e2 add develop3 Mac: demo$ git hi 28646e2 2015-09-18 | add develop3 [fuhaiwei] c341f75 2015-09-18 | add develop2 [fuhaiwei] 22f95c8 2015-09-18 | add develop1 [fuhaiwei] a4959da 2015-09-18 | add master_file [fuhaiwei] dccff7e 2015-09-18 | initial commit (empty) [fuhaiwei] ②重置本地仓库和暂存区到修改之前的状态 Mac: demo$ git reset a4959da Mac: demo$ git hi a4959da 2015-09-18 | add master_file [fuhaiwei] dccff7e 2015-09-18 | initial commit (empty) [fuhaiwei] 注意此时本地仓库已经被重置,但是工作区的内容是修改之后的内容。 注意:这里千万不要打成 git reset --hard a4959da,要不然连工作区的内容都没了。 (万一不小心打错了,导致修改的数据都没了,不要慌,翻看前面的内容,找到最新提交的ID,git reset --hard 28646e2,即可找回。或者git reflog查看HEAD记录,找到这个ID) ③重新发起一次提交即可 Mac: demo$ git add . Mac: demo$ git status On branch develop Changes to be committed: (use "git reset HEAD <file>..." to unstage) new file: develop1 new file: develop2 new file: develop3 Mac: demo$ git commit -m "add develop1 develop2 develop3" [develop b36bff8] add develop1 develop2 develop3 3 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 develop1 create mode 100644 develop2 create mode 100644 develop3 Mac: demo$ git hi b36bff8 2015-09-18 | add develop1 develop2 develop3 [fuhaiwei] a4959da 2015-09-18 | add master_file [fuhaiwei] dccff7e 2015-09-18 | initial commit (empty) [fuhaiwei]
    转载请注明原文地址: https://ju.6miu.com/read-22395.html

    最新回复(0)