Git:推送后更改时间戳

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17986168/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 08:51:44  来源:igfitidea点击:

Git: Change timestamp after pushing

gitgithub

提问by Josh Whittington

How do I edit a commit's timestamp after (accidentally) pushing it to the remote repository?

在(意外地)将提交推送到远程存储库后,如何编辑提交的时间戳?

I changed my system time clock to test behavior in my app, and forgot to set it back before
git add -u .and git push. Now my GitHub repo shows multiple files that were edited "in a month".

我更改了系统时钟以测试我的应用程序中的行为,但忘记在
git add -u .git push. 现在,我的 GitHub 存储库显示了“一个月内”编辑的多个文件。

I have followed the advice here– specifically the comments to the OP that recommend git push --force, but unfortunately the edit dates stay the same.

我遵循了这里的建议——特别是对 OP 的评论推荐git push --force,但不幸的是,编辑日期保持不变。

采纳答案by chris-l

Well, that article is about changing the commit message, and then force pushing the change. But here in this case, you first need to fix the commit timestamp, before doing the git push --force.

好吧,那篇文章是关于更改提交消息,然后强制推送更改。但在这种情况下,您首先需要修复提交时间戳,然后再执行git push --force.

Read this article to see how to fix the timestamp: How can one change the timestamp of an old commit in Git?

阅读本文以了解如何修复时间戳:如何在 Git 中更改旧提交的时间戳?

And then force the push.

然后强行推动。

(Btw, on github, it may take a few refreshes before you see the change. Also, I assume you know that you are breaking the history and if somebody else cloned the repo, you broke them, etc etc, right? good!)

(顺便说一句,在 github 上,在您看到更改之前可能需要刷新几次。另外,我假设您知道您正在破坏历史记录,如果其他人克隆了存储库,您破坏了它们等等,对吧?很好!)

回答by VonC

You could try the --dateoption of git commit, as described in this blog postby Alan Kelder:

你可以尝试--date的选择git commit,因为在此描述的博客文章艾伦Kelder

Modify/Reset timestamp for already committed change

修改/重置已提交更改的时间戳

The above works great for uncommitted changes, but what if you committed the changes and would like to modify the AuthorDatetime? Again, git makes that easy for the last commit:

以上对于未提交的更改非常有效,但是如果您提交了更改并想修改AuthorDate时间怎么办?同样,git 使最后一次提交变得容易:

$ git commit --amend --date='<see documentation for formats>' -C HEAD

Modify/Reset timestamps for several already committed changes

修改/重置多个已提交更改的时间戳

But what if you need to go back several commits? I ran into this situation as well and used the following Bash script to reset the AuthorDatetime, here's what it does.

For a list of files, it will get the hash of the last commit for each file, get the last modification date of the file, then reset the author timestamp to the file's actual modification date. Be sure to run it from the toplevel of the working tree (git will complain if you don't).

但是如果你需要返回多次提交怎么办?我也遇到了这种情况并使用以下 Bash 脚本来重置AuthorDate时间,这就是它的作用。

对于文件列表,它将获取每个文件的最后一次提交的哈希值,获取文件的最后修改日期,然后将作者时间戳重置为文件的实际修改日期。确保从工作树的顶层运行它(如果不这样做,git 会抱怨)。

files="
foo.txt
bar.txt
"

for file in $files; do
  dir=""; commit=""; date="";
  dir=$(dirname $(find -name $file))
  commit=$(git --no-pager log -1 --pretty=%H -- path "$dir/$file")
  date="$(stat -c %y $dir/$file)"
  if [ -z "$commit" ] || [ -z "$date" ]; then
    echo "ERROR: required var $commit or $date is empty"
  else
    echo "INFO: resetting authorship date of commit '$commit' for file '$dir/$file' to '$date'"
    git filter-branch --env-filter \
      "if test $GIT_COMMIT = '$commit'; then
         export GIT_AUTHOR_DATE
         GIT_AUTHOR_DATE='$date'
       fi" &&
    rm -fr "$(git rev-parse --git-dir)/refs/original/"
  fi
  echo
done

You could use the above script to reset both the AuthorDateand CommitDate(just add GIT_COMMITTER_DATEto the code above using the example from the Reference below).
Though modifying the CommitDatefor shared repositories sounds messy as you could be confusing others.

您可以使用上面的脚本来重置AuthorDateCommitDate(只需GIT_COMMITTER_DATE使用下面参考中的示例添加到上面的代码中)。
尽管修改CommitDate共享存储库听起来很混乱,因为您可能会让其他人感到困惑。