在git中添加历史记录

时间:2016-07-05 22:09:42

标签: git git-rewrite-history

我有一个私人git存储库,我在其中跟踪个人项目。在我使用源代码管理之前,我也有这个项目的副本。我想将这些副本添加到此存储库的历史记录中。由于只有我可以访问存储库,所以我应该能够在没有常见问题的情况下重写历史记录。

最好的方法是什么?我应该创建一个新的空白项目并连续添加旧项目,然后将当前存储库重播到其上,还是有其他方法?如果我这样做,这是否会导致远程仓库出现问题(我使用bitbucket)?

1 个答案:

答案 0 :(得分:2)

  

“我应该创建一个新的空白项目并连续添加旧项目   项目,然后重播当前的存储库?“

假设“史前”与现有历史完全融合,您可以将两个历史记录与两个git命令一起移植。从新的git存储库中运行:

git --git-dir=/path/to/original/.git log -m -p --reverse --first-parent --binary --pretty=email | git am --keep-non-patch --whitespace=fix

这很难看,所以这里的格式很奇怪但很明显(只要你复制和粘贴一体机,它仍然可以运行):

git --git-dir=/path/to/original/.git \
  log -m -p --reverse --first-parent --binary --pretty=email |
git am --keep-non-patch --whitespace=fix

之后运行“git log”命令以确保一切正确。一旦你开心,我建议删除或重命名现有的bitbucket repo,并将其推到原位。

警告#1:“git log --first-parent”从历史中删除所有分支和合并,因此结果只是一个线性的提交序列。 (有关--first-parent:GIT: How can I prevent foxtrot merges in my 'master' branch?https://bit-booster.blogspot.ca/2016/02/no-foxtrots-allowed.html)的更多信息。

警告#2:“git am”讨厌空提交(例如,git commit --allow-empty),所以如果您有任何这些提交,则必须将该过程分解为三个步骤:1。 git log> git_am.log。 2.从“git_am.log”中删除空补丁。 3. cat git_am.log | git am

以下是关于上面使用的“git log”选项的一些注意事项:

-m
   Include patches caused by merge commits.

-p
   Show patches in the "git log" output.

--reverse
   Reverse "git log" ordering (since we want oldest-to-newest).

--first-parent
   Only include 1st parent of merge commits.

--binary
   Include binary-diffs in output.

--pretty=email
   Have "git log" output its data in the "email" format
   that "git am" is expecting.

我留下查找“git am”选项作为读者的练习(例如,“git help am”)。