【问题标题】:Adding history in git在 git 中添加历史记录
【发布时间】:2016-07-05 22:09:42
【问题描述】:

我有一个私人 git 存储库,我在其中跟踪个人项目。在我使用源代码控制之前,我也有这个项目的副本。我想将这些副本添加到此存储库的历史记录中。由于只有我有权访问存储库,因此我应该能够重写历史记录而不会出现通常的问题。

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

【问题讨论】:

  • 重写历史会导致远程出现问题,因为它需要强制推送,而其他人将无法快进。只要您对此感到满意,那么就有很多方法可以完成您想做的事情。最好的取决于我认为的情况。
  • @JeffPuckettII:是的,我同意——没有其他人,只有我可以访问。

标签: git git-rewrite-history


【解决方案1】:

“我是否应该创建一个新的空白项目并依次添加旧的 项目,然后在其上重播当前存储库?”

是的。

假设“前历史”干净地合并到现有历史中,您可以使用两个 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 存储库,然后将其推送到它的位置。

Caveat #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 日志 > git_am.log。 2.从“git_am.log”中删除空补丁。 3. 猫 git_am.log |混帐我

以下是关于上面使用的“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”)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-17
    • 1970-01-01
    • 2020-10-01
    • 2023-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多