请问 git 合并 dev分支 到 master 时候 怎样避免 dev分支 混乱的提交记录?

存在 dev 开发分支 以及 master 主分支,dev 分支开发完成后,合并 master 分支的时候,会把 dev 上混乱的 commit 提交记录都带过去了。

请问 dev 分支 合并到 master 分支的时候,能不能只保留最新的 commit 记录 或者只创建一个 新的提交记录?

求大大指教 T_T

阅读 8k
3 个回答

当然可以,这个也是比较常见的问题,所以git中有对应的操作——压缩提交。就像楼主所说的那样,你在dev分支的提交信息很乱或者很不合理,这时候你很想把它们压缩成一次提交。下面有两种方法,第一种方法简单而且好理解。

  • 方法一:使用重置命令

reset命令可以做一些有趣的事情,压缩提交就是其中一个。

首先,你应该可以找到dev分支上的第一次提交,然后获取到它的提交哈希值,然后使用下面的命令:

git reset --soft <hash-code-of-first-commit>

当然,如果你想压缩的提交并不多,例如只有三个的话,可以使用相对位置HEAD~3

然后提交即可,在提交信息中,你可以重新整理你的提交信息,这样就把dev上的所有提交压缩为一次提交。

git commit

具体原理可以参考Pro Git一书,如果需要,我可以在这里做详细的解释。

  • 方法二:使用变基命令

由于变基命令很多人并不熟悉,而且相对重置命令也更不好理解,所以这里只作为备选方案。但是你要知道变基命令不止可以压缩提交,也可以改变提交历史。

同样,你需要获取dev分支的第一次提交的哈希值。

运行git rebase命令,并指定-i选项:

git rebase -i <hash-code-of-first-commit>

然后git就会打开一个脚本文件,类似与下面这样:

 pick af0ddef whitespace yo
 pick 0c04f10 Prepare for 2.19.0
 pick 8264ac4 Remove a duplicated world

 # Rebase 9ee2d6f..8264ac4 onto 9ee2d6f (3 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
 # d, drop = remove commit
 #
 # 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.

接着,你需要根据自己的需要修改脚本,注释中已经注明命令的用法。由于我们需要压缩所有提交到第一次提交,因此我们需要将脚本改为如下所示的内容:

 pick af0ddef whitespace yo
 squash 0c04f10 Prepare for 2.19.0
 squash 8264ac4 Remove a duplicated world

 # Rebase 9ee2d6f..8264ac4 onto 9ee2d6f (3 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
 # d, drop = remove commit
 #
 # 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.

稍加思考,你应该可以理解其中的道理。

修改完后,保存退出,这时git会给你重新编辑这次提交信息的机会,如果需要你可以更改提交信息:

# This is a combination of 3 commits.
 # The first commit's message is:
 whitespace yo

 # This is the 2nd commit message:

 Prepare for 2.19.0

 # This is the 3rd commit message:

 Remove a duplicated world

 # Please enter the commit message for your changes. Lines starting
 # with '#' will be ignored, and an empty message aborts the commit.
 #
 # Author:    Josh Nichols <technicalpickles@github.com>
 # Date:      Fri May 6 18:51:49 2016 -0400
 #
 # interactive rebase in progress; onto 9ee2d6f
 # Last commands done (3 commands done):

rebase整理好dev后在合并到master

rebase先处理代码,完毕后会让你整理comments,所有历史提交的comments列出来,你把不想要的在行首用#注释掉就没了,也可以全部删了重新写。

可以啊,你先reset(git默认的reset操作为soft,只回滚commit记录,不回滚代码,所以你不用担心git reset会把你的代码回滚掉。如果你需要回滚代码,请用git reset --hard),重新commit,就可以合并成一条commit记录,然后merge

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进