##整理提交记录

有时候完成一个功能或者修复一个问题需要多次提交,但是在向主发布分支发起 MR 时,太多次数的提交记录不美观,这个时候我们就需要整理提交记录。

整理提交记录一般使用 rebase。在终端中输入 git rebase -i origin/xxx,其中 xxx 代表你需要发起 MR 的分支(注:xxx一般为保护分支,-i代表rebase交互模式),

  1 pick 1204858 fix 123
  2 pick 379415c fix 12
  3 pick f7e189f 2233444
  4 pick ab3aa25 bug 1
  5 pick a8f5ee3 new function 1
  6
  7 # Rebase 2c6c58f..a8f5ee3 onto 2c6c58f (5 commands)
  8 #
  9 # Commands:
 10 # p, pick = use commit
 11 # r, reword = use commit, but edit the commit message
 12 # e, edit = use commit, but stop for amending
 13 # s, squash = use commit, but meld into previous commit
 14 # f, fixup = like "squash", but discard this commit's log message
 15 # x, exec = run command (the rest of the line) using shell
 16 # d, drop = remove commit

我们需要将上述提交合并成一个大提交,并 MR 到发布分支。具体操作是将除第一个pick之外的 pick 修改为 s,

s 代表squash,有整理,压缩的含义。

  1 pick 1204858 fix 123
  2 s 379415c fix 12
  3 s f7e189f 2233444
  4 s ab3aa25 bug 1
  5 s a8f5ee3 new function 1
  6
  7 # Rebase 2c6c58f..a8f5ee3 onto 2c6c58f (5 commands)
  8 #
  9 # Commands:
 10 # p, pick = use commit
 11 # r, reword = use commit, but edit the commit message
 12 # e, edit = use commit, but stop for amending
 13 # s, squash = use commit, but meld into previous commit
 14 # f, fixup = like "squash", but discard this commit's log message
 15 # x, exec = run command (the rest of the line) using shell
 16 # d, drop = remove commit

之后回到修改 commit 信息的窗口,这时修改提交信息,然后保存退出。

  1 # This is a combination of 5 commits.
  2 合并之后的大提交
  3
  4
  5
  6
  7
  8
  9
 10 # Please enter the commit message for your changes. Lines starting
 11 # with '#' will be ignored, and an empty message aborts the commit.
 12 #
 13 # Date:      Wed Oct 25 10:51:28 2017 +0800
 14 #
 15 # interactive rebase in progress; onto 2c6c58f
 16 # Last commands done (5 commands done):
 17 #    squash ab3aa25 bug 1
 18 #    squash a8f5ee3 new function 1

最后我们使用 git log 看一下提交记录,由下面的 log 可知,我们的确合并了多次提交,这个时候就可以往发布分支提一个好看的 MR 了。但是有时候,假设上述的5次提交记录已经被提交到增加的远程分支,我们在本地 rebase 后,再 push 到远端就会发生冲突,我们就需要强制 push,在push 是加上测试 -f 便可以实现。

commit 40bfba9817ab6748c62e55913a28400b7c07683b (HEAD -> dev)
Author: lux <583282897@qq.com>
Date:   Wed Oct 25 10:51:28 2017 +0800

    合并之后的大提交

commit 2c6c58fcc07617f4596b191a38fa6675ccf8ef00 (origin/master, origin/dev, origin/HEAD)
Author: lux <583282897@qq.com>
Date:   Sun Oct 8 17:51:24 2017 +0800

    edit

commit 26990fe3ec645d7d49df6b283b7db212394d8838
Author: lux <583282897@qq.com>
Date:   Wed Oct 4 17:35:11 2017 +0800

    init

commit 8c96f35366309939b591fa4b87e9d062f22adcf7

rebase 能是一把功能强大的双刃剑,有兴趣的话可以对比一下 merge和 rebase 的区别。rebase在一定程度上有助于产生更加清晰的提交,但是要重写提交记录,因此rebase之后只好只强制 push 到自己的远端分支,然后通过 MR合并到发布分支。

如果在 Idea 里面进行 rebase 操作就非常简单了,在 Version Control中的 Log 中选中你需要合并的最早的那条提交记录,然后右键就有一个Interactively Rebase from Here,然后点击它,会跳出一个菜单,也和上述一样,把第一个之后改成 s,确定,然后重写提交信息,确定就可以完成合并提交。

将当前的某些提交抽取到另外分支上

有时候有些提交已经提交到本地了,当后来本地有提交一些和之前提交无关的东西,但是你又想先把之前的提交推送到一个新的远端分支。

比如有以下本地提交记录:

1.修复 bug 不够多问题 40bfba9817ab6748c62e55913a28400b7c07683b

2.修复 饭不好吃问题 1204858cf7c1cab54fc28f196033b21933bfe7a2

3.修复天气比较冷问题 2c6c58fcc07617f4596b191a38fa6675ccf8ef00

假如我们只需要上线2的功能,1还没有完全弄好。这时我们从远处发布分支 check out 出一个新分支

,然后在新分支里面执行 git cherry-pick 1204858cf7c1cab54fc28f196033b21933bfe7a2,这个时候,这个2的提交就被放到新的分支,然后就可以在新分支提 MR 合并到发布分支了。

使用 git 同步虚拟机和本地代码修改

有时候程序是跑在虚拟机或者服务器里面(一般是本地跑不了太多的东西),那本地的代码修改需要同步到虚拟机。

现在的解决办法有以下几种

1.在虚拟机和本地使用 nfs 或者其他网络文件系统来共享文件夹,这个就可以保证两边的同步。这个在很久之前用过,但是总感觉很麻烦。

2.将本地修改推送到远端,然后虚拟机从远端 fetch,这个虽然也可以用,但是产生了远端提交记录,虽然是自己的分支,总感觉不合理,还要走服务器一趟。

3.使用 git 的remote 功能,这样就直接往虚拟机推送代码,用起来也简单一些

大致使用流程如下

  1. 添加本地项目新的跟踪远端:git remote add xnj username@host: /xxx/xxx,username为虚拟机的用户名,host 为虚拟机 ip,/xxx/xxx为项目在虚拟机内的绝对路径(注L:这样配置在每次推送代码时,都需要输入虚拟机密码,可以在~/.ssh/config 中配置私钥访问,就可以不用输入密码)
  2. 然后 push 代码:git push xnj xxx:yyy ,yyy 为你的分支名。但是在 push 时,如果虚拟机中的 yyy 已被 check out,那么 push 将会被拒绝,解决办法最简单就是先切换到其他,然后在 push,也可以 reset,还木有试过。

参考文档

1.Git Book

2.ihower

3.老大的指导