💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、豆包、星火、月之暗面及文生图、文生视频 广告
## 7.6 重写历史 ### 7.6.1 变基工具 **修改最后一次提交** ``` $ git commit --amend ``` 使用这个技巧的时候需要小心,因为修正会改变提交的 SHA-1 校验和。 它类似于一个小的变基 - 如果已经推送了最后一次提交就不要修正它。 **修改多个提交信息** Git 没有一个改变历史工具,但是可以使用变基工具来变基一系列提交。 ``` $ git rebase -i HEAD~3 ``` 运行这个命令会在文本编辑器上提供一个提交的列表: ``` pick f7f3f6d changed my name a bit pick 310154e updated README formatting and added blame pick a5f4a0d added cat-file # Rebase 710f0f8..a5f4a0d onto 710f0f8 # # 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 ``` 将需要修改提交信息的提交前的 `pick` 改为 `reword` ,保存退出后 git 会逐一让你为每个需要修改的提交提供新的提交信息。 **修改多个提交** 将需要修改的提交前的 `pick` 改为 `edit` ,保存退出后 git 会切回需要修改的提交。然后运行 ``` $ git commit --amend ``` 修改提交信息,然后退出编辑器。 然后,运行 ``` $ git rebase --continue ``` 修改会引用到后续的每一次提交中,如果需要将不止一处的 pick 改为 edit,需要在每一个修改为 edit 的提交上重复这些步骤。 每一次,Git 将会停止,让你修正提交,然后继续直到完成。 **重新排序提交** 也可以使用交互式变基来重新排序或完全移除提交。 **压缩提交** 通过交互式变基工具,也可以将一连串提交压缩成一个单独的提交。 将需要被压缩的提交前的 `pick` 改为 `squash` ,保存退出后,git 为让你提供一个新的提交信息。 **拆分提交** 拆分一个提交会撤消这个提交,然后多次地部分地暂存与提交直到完成你所需次数的提交。 ### 7.6.2 filter-branch filter-branch 可以通过脚本的方式改写大量提交。 **从每一个提交移除一个文件** ``` $ git filter-branch --tree-filter 'rm -f [filename]' HEAD ``` `--tree-filter`选项在检出项目的每一个提交后运行指定的命令然后重新提交结果。 **使一个子目录做为新的根目录** ``` $ git filter-branch --subdirectory-filter [foldname] HEAD ``` **全局修改邮箱地址** ``` $ git filter-branch --commit-filter ' if [ "$GIT_AUTHOR_EMAIL" = "schacon@localhost" ]; then GIT_AUTHOR_NAME="[name]"; GIT_AUTHOR_EMAIL="[email]"; git commit-tree "$@"; else git commit-tree "$@"; fi' HEAD ```