简体   繁体   English

Git 修复现有提交历史记录

[英]Git Fix existing commits history

I have a batch exe that script all database object and then automatically commit into a git repository.我有一个批处理 exe 脚本所有数据库对象,然后自动提交到 git 存储库。

For some reason ,that I've already fixed, actually i have this history:出于某种原因,我已经修复了,实际上我有这样的历史:

  • Commit 1: File sampleStoredProcedure.sql ...addedd提交 1:文件 sampleStoredProcedure.sql ...已添加
  • Commit 2: File sampleStoredProcedure.sql ...modified提交 2:文件 sampleStoredProcedure.sql ...已修改
  • Commit 3: File sampleStoredProcedure.sql ...deleted提交 3:文件 sampleStoredProcedure.sql ...已删除
  • Commit 4: File sampleStoredProcedure.sql ...added提交 4:文件 sampleStoredProcedure.sql ...已添加
  • Commit 5: File sampleStoredProcedure.sql ...deleted提交 5:文件 sampleStoredProcedure.sql ...已删除
  • Commit 6: File sampleStoredProcedure.sql ...added提交 6:文件 sampleStoredProcedure.sql ...已添加
  • Commit 7: File sampleStoredProcedure.sql ...modified提交 7:文件 sampleStoredProcedure.sql ...已修改

At commit 6, file is logically different from commit 4 But if i show file history i don't see nothing because the file is new for git system.在提交 6 时,文件在逻辑上与提交 4 不同但是如果我显示文件历史记录,我什么也看不到,因为该文件是 git 系统的新文件。

There is a way to solve this mess ?有没有办法解决这个烂摊子?

I wish this history:我希望这段历史:

  • Commit 1: File sampleStoredProcedure.sql ...addedd提交 1:文件 sampleStoredProcedure.sql ...已添加
  • Commit 2: File sampleStoredProcedure.sql ...modified提交 2:文件 sampleStoredProcedure.sql ...已修改
  • Commit 3: File sampleStoredProcedure.sql ...deleted --remove this提交 3:文件 sampleStoredProcedure.sql ...deleted --remove this
  • Commit 4: File sampleStoredProcedure.sql ...added --set as change of commit 2提交 4:文件 sampleStoredProcedure.sql ...已添加 --set 作为提交 2 的更改
  • Commit 5: File sampleStoredProcedure.sql ...deleted -remove this提交 5:文件 sampleStoredProcedure.sql ...已删除 - 删除此
  • Commit 6: File sampleStoredProcedure.sql ...added --set as change of commit 4提交 6:文件 sampleStoredProcedure.sql ...已添加 --set 作为提交 4 的更改
  • Commit 7: File sampleStoredProcedure.sql ...modified提交 7:文件 sampleStoredProcedure.sql ...已修改

Thanks for your support谢谢您的支持

You can use an interactive rebase to squash the commits where you deleted and re-added the file.您可以使用交互式 rebase压缩删除和重新添加文件的提交。

I'd recommend making a backup of your repository before following the next steps.我建议在执行后续步骤之前备份您的存储库。

  1. Get the ref of "Commit 1", eg f79c595bce675cf6109ce9c7d477741a55a9a247 .获取“Commit 1”的引用,例如f79c595bce675cf6109ce9c7d477741a55a9a247
  2. Run git rebase --interactive <ref> .运行git rebase --interactive <ref>
  3. You should see a file with containing something like this:您应该会看到一个包含以下内容的文件:
pick f79c595 File sampleStoredProcedure.sql ...added
pick 141asd1 File sampleStoredProcedure.sql ...modified
pick 6d2b123 File sampleStoredProcedure.sql ...deleted
pick asd123a File sampleStoredProcedure.sql ...added
  1. Change pick to fixup for all of the lines where the file was added again, ie with commit message "...added."将再次添加文件的所有行的pick更改为fixup ,即带有提交消息“... added”。
  2. Save the file.保存文件。
  3. New files will be opened where you can edit the commit message of the new commits if you want to.将打开新文件,您可以在其中编辑新提交的提交消息(如果需要)。

If you have many commits and your commit messages all have the same format, you can use a macro or script in your editor to find all of the relevant commits.如果您有许多提交并且您的提交消息都具有相同的格式,您可以在编辑器中使用宏或脚本来查找所有相关的提交。 As an example, here is how I would do it in Vim:举个例子,下面是我在 Vim 中的做法:

  1. Start the interactive rebase with Vim GIT_EDITOR=vim git rebase --interactive <ref> .使用 Vim GIT_EDITOR=vim git rebase --interactive <ref>启动交互式 rebase。
  2. Type: qw/added<enter>^dwifixup <esc>jq:%normal @w<enter> qw/added<enter>^dwifixup <esc>jq:%normal @w<enter>qw/added<enter>^dwifixup <esc>jq:%normal @w<enter>
  3. Save and quit with :wq .使用:wq保存并退出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM