更改 git 历史记录,同时保留 GPG 签名的时间戳

问题描述 投票:0回答:1

我正在尝试从旧本地存储库的 git 历史记录中删除敏感数据,然后再将其推送到 Github,同时还维护/伪造原始 GPG 签名时间戳。

我知道不建议像这样更改 git 历史记录,但我确实需要删除这些敏感数据,同时维护 GPG 时间戳(即使我知道哈希值会有所不同)。

我以前可以通过以下方式做到这一点:

我会运行

git rebase -i {HEAD}
(HEAD 是我要编辑的提交之前的提交),然后选择编辑所需的提交,然后运行:

  1. sudo date -f "%s" {UNIX-TIMESTAMP}
  2. GIT_COMMITTER_DATE="$(date)" git commit -—amend --date="$(date)" -S

第一个命令是将机器的日期更改为我要编辑的过去提交的日期。 我在进行所需的更改并运行

git add .
后运行的第二个命令将修改提交并使用所需的日期对其进行签名,同时仍然保留所有后续提交的签名日期。

这曾经工作正常,但现在当我尝试运行它时,我想要编辑的提交之后的所有提交都将具有一个新的 GPG 签名时间戳,该时间戳与我选择编辑所需的 UNIX-TIMESTAMP 相同commit with,当我将其推送到 Github 时,这将是可见的。

我也尝试过

GIT_COMMITTER_DATE="$(date)" git commit -—amend --no-edit --date="$(date)" -S
,但它导致了同样的问题。

运行

git rebase —continue
后,我可以做什么来维护后续(和未编辑的)提交的 GPG 时间戳? 或者还有其他方法可以做到这一点吗?

git github version-control git-rebase gpg-signature
1个回答
0
投票

(首先,备份您的存储库!)

我认为 Git 本身不会支持在此类操作期间保留 GPG 时间戳。

识别您要编辑的提交的哈希值 (

Commit B
)。并启动一个交互式变基,从您要编辑的提交的父级开始。

git rebase -i <commit-hash>^

在交互式变基

edit
列表中使用
todo
标记要编辑的提交。并用您的更改修改提交。

git commit --amend -S

继续变基 (

git rebase --continue
)。

对于每个后续提交,将

GIT_COMMITTER_DATE
重置为原始提交日期。这可以通过脚本自动化。
您可以使用脚本自动重置
GIT_COMMITTER_DATE
。该脚本将:

  • 提取每次提交的原始提交日期。
  • GIT_COMMITTER_DATE
    设置为此原始日期。
  • 修改每个提交而不更改其内容 (
    git commit --amend --no-edit -S
    )。
#!/bin/bash

# The hash of the first commit to start processing (exclusive)
START_COMMIT="<commit-hash>"

# Checkout to a temporary branch to avoid directly modifying the main branch
git checkout -b temp-branch

# Iterate over each commit from HEAD to START_COMMIT
while [ $(git rev-parse HEAD) != $START_COMMIT ]; do
    # Get the original author date
    AUTHOR_DATE=$(git show -s --format=%aI HEAD)

    # Amend the commit without changing its message/content, but reset the author date
    GIT_COMMITTER_DATE="$AUTHOR_DATE" git commit --amend --no-edit -S --date="$AUTHOR_DATE"

    # Move to the previous commit
    git reset --soft HEAD~1
done

# Finally, rename the temporary branch back to the original branch name if needed
git checkout -b new-branch-name
git branch -D temp-branch

echo "All commits amended with original author dates."

或者,您也可以使用

git filter-branch
:

#!/bin/bash

# The range of commits to process, e.g., master..feature-branch
COMMIT_RANGE="<commit-range>"

# Function to extract and set the original author date for each commit
export -f set_original_date
set_original_date() {
    # Extract the original author date
    local AUTHOR_DATE=$(git log -1 --format=%aI $GIT_COMMIT)

    # Set the GIT_COMMITTER_DATE to the original author date
    export GIT_COMMITTER_DATE=$AUTHOR_DATE
}

# Run git filter-branch with the above function
git filter-branch --env-filter 'set_original_date' $COMMIT_RANGE

完成变基并确保所有日期正确后,您可以将更改推送到 GitHub。

© www.soinside.com 2019 - 2024. All rights reserved.