Dhs*_*kdh 673 git timestamp commit git-rewrite-history
如何修改现有的,未删除的提交的答案?描述了一种修改先前尚未向上游推送的提交消息的方法.新消息继承原始提交的时间戳.这似乎合乎逻辑,但有没有办法重新设定时间?
Pau*_*ijs 721
您可以执行交互式rebase,并为要更改其日期的提交选择编辑.当rebase进程停止修改提交时,例如:
git commit --amend --date="Wed Feb 16 14:00 2011 +0100"
Run Code Online (Sandbox Code Playgroud)
然后继续交互式rebase.
更新(响应studgeek的评论):更改提交日期而不是作者日期:
GIT_COMMITTER_DATE="Wed Feb 16 14:00 2011 +0100" git commit --amend
Run Code Online (Sandbox Code Playgroud)
上面的行设置了一个环境变量GIT_COMMITTER_DATE,用于修改提交.
一切都在Git Bash中测试过.
Dus*_*tin 497
使用git filter-branch与ENV筛选GIT_AUTHOR_DATE,并GIT_COMMITTER_DATE为提交具体的哈希你正在寻找修复.
这将使该和所有未来的哈希失效.
例:
如果你想更改提交的日期119f9ecf58069b265ab22f1f97d2b648faf932e0,你可以这样做:
git filter-branch --env-filter \
'if [ $GIT_COMMIT = 119f9ecf58069b265ab22f1f97d2b648faf932e0 ]
then
export GIT_AUTHOR_DATE="Fri Jan 2 21:38:53 2009 -0800"
export GIT_COMMITTER_DATE="Sat May 19 01:01:01 2007 -0700"
fi'
Run Code Online (Sandbox Code Playgroud)
Luk*_*man 364
在一个命令中处理所有这些建议的更好方法是
LC_ALL=C GIT_COMMITTER_DATE="$(date)" git commit --amend --no-edit --date "$(date)"
Run Code Online (Sandbox Code Playgroud)
这会将最后一次提交的提交和作者日期设置为"现在".
Mig*_*rro 165
做吧git commit --amend --reset-author --no-edit.对于较旧的提交,您可以执行交互式rebase并选择edit要修改其日期的提交.
git rebase -i <ref>
Run Code Online (Sandbox Code Playgroud)
然后使用--reset-author和修改提交--no-edit将作者日期更改为当前日期:
git commit --amend --reset-author --no-edit
Run Code Online (Sandbox Code Playgroud)
最后继续使用您的交互式rebase:
git rebase --continue
Run Code Online (Sandbox Code Playgroud)
big*_*ato 115
我为此写了一个脚本和Homebrew包.超级易于安装,您可以在GitHub PotatoLabs/git-redate页面上找到它.
句法:
git redate -c 3
Run Code Online (Sandbox Code Playgroud)
你只需要运行git redate,你就可以编辑最近5次提交的vim中的所有日期(还有一个-c选项可以让你想要返回多少次提交,它只是默认为5).如果您有任何问题,意见或建议,请与我们联系!
Ort*_*kni 83
每个提交都与两个日期相关联,即提交者日期和作者日期.您可以使用以下方式查看这些日
git log --format=fuller
Run Code Online (Sandbox Code Playgroud)
如果要更改最近6次提交的作者日期和提交者日期,可以使用交互式rebase:
git rebase -i HEAD~6
Run Code Online (Sandbox Code Playgroud)
.
pick c95a4b7 Modification 1
pick 1bc0b44 Modification 2
pick de19ad3 Modification 3
pick c110e7e Modification 4
pick 342256c Modification 5
pick 5108205 Modification 6
# Rebase eadedca..5108205 onto eadedca (6 commands)
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit
Run Code Online (Sandbox Code Playgroud)
对于要更改日期的所有提交,请替换pick为edit(或仅e),然后保存并退出编辑器.
您现在可以通过以ISO-8601格式指定作者日期和提交者日期来修改每个提交:
GIT_COMMITTER_DATE="2017-10-08T09:51:07" git commit --amend --date="2017-10-08T09:51:07"
Run Code Online (Sandbox Code Playgroud)
第一个日期是提交日期,第二个日期是作者日期.
然后转到下一个提交:
git rebase --continue
Run Code Online (Sandbox Code Playgroud)
重复此过程,直到您修改所有提交.检查你的进展git status.
Mat*_*tag 61
其他答案对于编辑多个提交日期不是很方便。几年后我又回到这个问题来分享一项技术。
要更改最后 4 次提交的日期:
git rebase -i HEAD~4
Run Code Online (Sandbox Code Playgroud)
按如下方式编辑变基,插入exec行以根据需要修改日期:
pick 4ca564e Do something
exec git commit --amend --no-edit --date "1 Oct 2019 12:00:00 PDT"
pick 1670583 Add another thing
exec git commit --amend --no-edit --date "2 Oct 2019 12:00:00 PDT"
pick b54021c Add some tests
exec git commit --amend --no-edit --date "3 Oct 2019 12:00:00 PDT"
pick e8f6653 Fix the broken thing
exec git commit --amend --no-edit --date "4 Oct 2019 12:00:00 PDT"
Run Code Online (Sandbox Code Playgroud)
Von*_*onC 42
在theosp的答案的基础上,我写了一个名为git-cdc(更改日期提交)的脚本,我把它放在我的PATH.
名称很重要:git-xxx您PATH可以在任何地方输入:
git xxx
# here
git cdc ...
Run Code Online (Sandbox Code Playgroud)
即使在Windows上,该脚本仍然使用bash(因为Git将从其msys环境调用它)
#!/bin/bash
# commit
# date YYYY-mm-dd HH:MM:SS
commit="$1" datecal="$2"
temp_branch="temp-rebasing-branch"
current_branch="$(git rev-parse --abbrev-ref HEAD)"
date_timestamp=$(date -d "$datecal" +%s)
date_r=$(date -R -d "$datecal")
if [[ -z "$commit" ]]; then
exit 0
fi
git checkout -b "$temp_branch" "$commit"
GIT_COMMITTER_DATE="$date_timestamp" GIT_AUTHOR_DATE="$date_timestamp" git commit --amend --no-edit --date "$date_r"
git checkout "$current_branch"
git rebase --autostash --committer-date-is-author-date "$commit" --onto "$temp_branch"
git branch -d "$temp_branch"
Run Code Online (Sandbox Code Playgroud)
有了它,你可以输入:
git cdc @~ "2014-07-04 20:32:45"
Run Code Online (Sandbox Code Playgroud)
这会将HEAD(@~)之前提交的作者/提交日期重置为指定日期.
git cdc @~ "2 days ago"
Run Code Online (Sandbox Code Playgroud)
这会将HEAD(@~)之前提交的作者/提交日期重置为同一小时,但是2天前.
Ilya Semenov 在评论中提到:
对于OS X,您还可以安装GNU
coreutils(brew install coreutils),将其添加到PATH(PATH="/usr/local/opt/coreutils/libexec/gnubin:$PATH")然后使用"2 days ago"语法.
Har*_*ren 39
git commit --amend --date="now"
Run Code Online (Sandbox Code Playgroud)
yoz*_*hik 25
2022 年 7 月的信息:
这与当前时间戳的工作效果非常惊人:
git commit --amend --date=now --no-edit
Run Code Online (Sandbox Code Playgroud)
而这个 - 具有任何日期格式:
git commit --amend --date="Mon Jul 25 10:37:36 2022 +0300" --no-edit
Run Code Online (Sandbox Code Playgroud)
Per*_*ell 24
阅读完所有答案后,我想出了一种更简洁、更方便的方法来一次编辑多个提交的日期,而无需交互式地重新定位:
git rebase HEAD~4 --exec "git commit --amend --no-edit --date 'now'"
Run Code Online (Sandbox Code Playgroud)
它会更改提交者和作者日期。
Sér*_*gio 18
如果它是前一次提交.
git rebase -i HEAD~2
git commit --amend --date=now
Run Code Online (Sandbox Code Playgroud)
如果你已经推送到orgin并且可以强制使用:
git push --force
Run Code Online (Sandbox Code Playgroud)
如果你不能强制推送,如果它被推,你不能改变提交!.
eol*_*old 15
这是一个方便的别名,它将上次提交的提交和作者时间更改为接受的时间date --date:
[alias]
cd = "!d=\"$(date -d \"$1\")\" && shift && GIT_COMMITTER_DATE=\"$d\" \
git commit --amend --date \"$d\""
Run Code Online (Sandbox Code Playgroud)
用法: git cd <date_arg>
例子:
git cd now # update the last commit time to current time
git cd '1 hour ago' # set time to 1 hour ago
Run Code Online (Sandbox Code Playgroud)
编辑: 这是一个更自动化的版本,它检查索引是否干净(没有未提交的更改)并重用最后一次提交消息,否则失败(防呆):
[alias]
cd = "!d=\"$(date -d \"$1\")\" && shift && \
git diff-index --cached --quiet HEAD --ignore-submodules -- && \
GIT_COMMITTER_DATE=\"$d\" git commit --amend -C HEAD --date \"$d\"" \
|| echo >&2 "error: date change failed: index not clean!"
Run Code Online (Sandbox Code Playgroud)
the*_*osp 13
以下bash函数将更改当前分支上的任何提交时间.
如果您已经推送了提交或者在另一个分支中使用提交,请小心不要使用.
# rewrite_commit_date(commit, date_timestamp)
#
# !! Commit has to be on the current branch, and only on the current branch !!
#
# Usage example:
#
# 1. Set commit 0c935403 date to now:
#
# rewrite_commit_date 0c935403
#
# 2. Set commit 0c935403 date to 1402221655:
#
# rewrite_commit_date 0c935403 1402221655
#
rewrite_commit_date () {
local commit="$1" date_timestamp="$2"
local date temp_branch="temp-rebasing-branch"
local current_branch="$(git rev-parse --abbrev-ref HEAD)"
if [[ -z "$date_timestamp" ]]; then
date="$(date -R)"
else
date="$(date -R --date "@$date_timestamp")"
fi
git checkout -b "$temp_branch" "$commit"
GIT_COMMITTER_DATE="$date" git commit --amend --date "$date"
git checkout "$current_branch"
git rebase "$commit" --onto "$temp_branch"
git branch -d "$temp_branch"
}
Run Code Online (Sandbox Code Playgroud)
Kar*_*awy 13
我创建了这个npm包来改变旧提交的日期.
https://github.com/bitriddler/git-change-date
样品用法:
npm install -g git-change-date
cd [your-directory]
git-change-date
Run Code Online (Sandbox Code Playgroud)
系统将提示您选择要修改的提交,然后输入新日期.
如果要通过特定哈希更改提交,请运行此命令 git-change-date --hash=[hash]
Jan*_*n H 10
要更改作者日期和提交日期:
GIT_COMMITTER_DATE="Wed Sep 23 9:40 2015 +0200" git commit --amend --date "Wed Sep 23 9:40 2015 +0200"
Run Code Online (Sandbox Code Playgroud)
小智 10
如果提交尚未推送,那么我可以使用类似的东西:
git commit --amend --date=" Wed Mar 25 10:05:44 2020 +0300"
Run Code Online (Sandbox Code Playgroud)
之后 git bash 用已经应用的日期打开编辑器,所以你只需要通过在 VI 编辑器命令模式中输入“:wq”来保存它,你就可以推送它
Tho*_*wig 10
最简单的修改上次提交日期的方法
git commit --amend --date="12/31/2021 @ 14:00"
Run Code Online (Sandbox Code Playgroud)
将最近 5 次提交的日期更新为当前日期(此方法不允许更新初始提交):
\ngit rebase HEAD~5 --exec "git commit --amend --no-edit --date 'now'"\nRun Code Online (Sandbox Code Playgroud)\n对于提交 95f5074\xe2\x80\xa615074db2之后的所有提交:
\ngit rebase 95f5074\xe2\x80\xa615074db2 --exec "git commit --amend --no-edit --date 'now'"\nRun Code Online (Sandbox Code Playgroud)\n对于所有提交(包括初始提交):
\ngit rebase --root --exec "git commit --amend --no-edit --date 'now'"\nRun Code Online (Sandbox Code Playgroud)\n添加-i交互模式。
运行git log --format=fuller --show-signature以验证更改。
运行git push -f以更新远程存储库(\xe2\x9a\xa0\xef\xb8\x8f危险区域)
这是有影响的。例如:
\n.gitconfig,这意味着您的密钥将用于签署提交(如果 Git 配置为签署提交)GIT_COMMITTER_DATE="Sun Nov 20 21:02 2022 +0530" git commit --amend --no-edit --date="Sun Nov 20 21:02 2022 +0530"
git push -f
Run Code Online (Sandbox Code Playgroud)
每次都有效。
如果你想获得另一个提交的确切日期(比如你修改了一个提交并希望它具有原始pre-rebase版本的日期):
git commit --amend --date="$(git show -s --format=%ai a383243)"
Run Code Online (Sandbox Code Playgroud)
这纠正头部的日期提交要准确提交a383243之日起(包括多个数字,如果有不清楚的地方).它还会弹出一个编辑器窗口,以便您可以编辑提交消息.
这是作者日期,这是你通常关心的 - 请参阅提交者日期的其他答案.
小智 7
对于那些使用 Powershell 的人
git rebase DESIRED_REF^ -i
$commitDateString = "2020-01-22T22:22:22"
$env:GIT_COMMITTER_DATE = $commitDateString
git commit --amend --date $commitDateString
$env:GIT_COMMITTER_DATE = ""
git rebase --continue
Run Code Online (Sandbox Code Playgroud)
归功于https://mnaoumov.wordpress.com/2012/09/23/git-change-date-of-commit/
编辑作者日期和最近 3 次提交的提交日期:
git rebase -i HEAD~3 --committer-date-is-author-date --exec "git commit --amend --no-edit --date=now"
Run Code Online (Sandbox Code Playgroud)
该--exec命令附加在 rebase 中的每一行之后,您可以使用 选择作者日期--date=...,提交者日期将与作者日期相同。
GIT_COMMITTER_DATE="$(date)" git commit --amend --no-edit --date "$(date)"
Run Code Online (Sandbox Code Playgroud)
GIT_COMMITTER_DATE="Mon 20 Aug 2018 20:19:19 BST" git commit --amend --no-edit --date "Mon 20 Aug 2018 20:19:19 BST"
Run Code Online (Sandbox Code Playgroud)
变基到之前所说的提交并停止修改:
git rebase <commit-hash>^ -ipick为e(编辑):wq在 VIM 中)GIT_COMMITTER_DATE="$(date)" git commit --amend --no-edit --date "$(date)"GIT_COMMITTER_DATE="Mon 20 Aug 2018 20:19:19 BST" git commit --amend --no-edit --date "Mon 20 Aug 2018 20:19:19 BST"来源:https : //codewithhugo.com/change-the-date-of-a-git-commit/
如果要在标准Windows命令行中执行接受的答案(/sf/answers/31832531/),则需要以下命令:
git filter-branch -f --env-filter "if [ $GIT_COMMIT = 578e6a450ff5318981367fe1f6f2390ce60ee045 ]; then export GIT_AUTHOR_DATE='2009-10-16T16:00+03:00'; export GIT_COMMITTER_DATE=$GIT_AUTHOR_DATE; fi"
Run Code Online (Sandbox Code Playgroud)
笔记:
^),但是我没有成功。非常感谢Colin Svingen的博客文章。即使他的代码对我不起作用,它也帮助我找到了正确的解决方案。
除了马特·蒙塔格的回答之外:
如果您需要在 rebase 命令后将时间戳重置为当前时间
git rebase -i HEAD~2
Run Code Online (Sandbox Code Playgroud)
您可以使用以下选项之一
pick 4ca564e Do something
exec git commit --amend --no-edit --date=now
pick 1670583 Add another thing
exec git commit --amend --no-edit --reset-author
Run Code Online (Sandbox Code Playgroud)
两者都会起作用
| 归档时间: |
|
| 查看次数: |
266388 次 |
| 最近记录: |