【问题标题】:Git: Bulk change of commit datesGit:提交日期的批量更改
【发布时间】:2020-07-06 21:07:30
【问题描述】:

当我需要更改各种提交的提交日期时,我使用交互式 rebase 并一一更改。

如何在一个命令中更改它们?换句话说,我需要将给定的命令应用于将在交互式 rebase 中列出的所有提交。

谢谢

【问题讨论】:

  • 日期应该怎么改?全部设置为同一时间,还是一小时后全部移动,等等?

标签: git git-rewrite-history


【解决方案1】:

过滤回购

git filter-branch 已弃用。相反,请使用git filter-repo。您需要安装它。

这里有一个excellent article,关于如何使用 git-filter-repo 来修改提交日期。 git-filter-repo documentation 很好地解释了--commit-callback 的概念。

一个非常简单的例子

让我们将所有提交日期的时区重置为零。

# Save this as ../change_time.py
def handle(commit):
    "Reset the timezone of all commits."
    date_str = commit.author_date.decode('utf-8')
    [seconds, timezone] = date_str.split()
    new_date = f"{seconds} +0000"
    commit.author_date = new_date.encode('utf-8')

handle(commit)

# You need to be in a freshly-cleaned repo. Or use --force.
git clone <...> your_repo
cd your_repo
# First just a dry run.
git filter-repo --dry-run --commit-callback "$(cat ../change_time.py)"
# And now do it for real
git filter-repo --commit-callback "$(cat ../change_time.py)"

【讨论】:

    【解决方案2】:

    改编自https://stackoverflow.com/a/750182/7976758

    #!/bin/sh
    
    git filter-branch --env-filter '
    GIT_AUTHOR_DATE="2000-12-21 23:45:00"
    GIT_COMMITTER_DATE="`date`" # now
    export GIT_AUTHOR_DATE GIT_COMMITTER_DATE
    ' --tag-name-filter cat -- --branches --tags
    

    参考https://git-scm.com/docs/git-filter-branch

    【讨论】:

    • git-filter-branch 已弃用。首选git filter-repo
    • official filter-branch docs 不得不说:“请使用替代的历史过滤工具,例如 git filter-repo。” (2) 运行“git filter-branch”(Git 2.25.2)时,您会收到类似的警告和建议使用 git filter-repo。
    【解决方案3】:

    git rebase 支持the --exec option,它会做到这一点。

    -x &lt;cmd&gt;

    --exec &lt;cmd&gt;

    在最终历史记录中创建提交的每一行之后附加“exec ”。 将被解释为一个或多个 shell 命令。任何失败的命令都会中断 rebase,退出代码为 1。

    【讨论】:

      猜你喜欢
      • 2012-10-13
      • 2018-04-25
      • 2019-12-28
      • 2011-04-18
      • 1970-01-01
      • 2014-02-02
      • 2011-05-02
      • 1970-01-01
      • 2014-10-01
      相关资源
      最近更新 更多