如何修改现有的、未推送的提交?描述一种修改尚未推送到上游的先前提交消息的方法。新消息继承原始提交的时间戳。这似乎合乎逻辑,但有没有办法也重新设定时间呢?
当前回答
只需执行git commit - modify -reset-author -no-edit。对于较旧的提交,您可以执行交互式的重基操作,并选择要修改其日期的提交的编辑。
git rebase -i <ref>
然后使用——reset-author和——no-edit修改提交,将作者日期更改为当前日期:
git commit --amend --reset-author --no-edit
最后继续您的互动rebase:
git rebase --continue
其他回答
每次提交都与两个日期相关联,提交者日期和作者日期。你可以用以下方法查看这些日期:
git log --format=fuller
如果你想改变最近6次提交的作者日期和提交者日期,你可以简单地使用交互式rebase:
git rebase -i HEAD~6
.
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
对于您想要更改日期的所有提交,将pick替换为edit(或只是e),然后保存并退出编辑器。
你现在可以通过指定作者日期和ISO-8601格式的提交日期来修改每次提交:
GIT_COMMITTER_DATE="2017-10-08T09:51:07" git commit --amend --date="2017-10-08T09:51:07"
第一个日期是提交日期,第二个日期是作者日期。
然后进行下一次提交:
git rebase --continue
重复该过程,直到修改所有提交。用git状态检查你的进程。
对于那些使用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
来源:https://mnaoumov.wordpress.com/2012/09/23/git-change-date-of-commit/
我为此写了一个脚本和Homebrew包。超级容易安装,你可以在GitHub PotatoLabs/git-redate页面上找到它。
语法:
git redate -c 3
你只需要运行git redate,你就可以在vim中编辑最近5次提交的所有日期(还有一个-c选项来决定你想要返回多少次提交,它默认是5)。如果你有任何问题,评论或建议,请告诉我!
使用带有env过滤器的git filter-branch,该过滤器为您希望修复的提交的特定散列设置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'
您可以进行交互式的更改,并选择编辑您想更改日期的提交。当rebase进程停止修改你输入的提交时,例如:
git commit --amend --date="Wed Feb 16 14:00 2011 +0100" --no-edit
注:date=now将使用当前时间。
之后,您将继续您的交互rebase。
修改提交日期而不是作者日期:
GIT_COMMITTER_DATE="Wed Feb 16 14:00 2011 +0100" git commit --amend --no-edit
上面的代码行设置了一个环境变量GIT_COMMITTER_DATE,该变量用于修改提交。
一切都在Git Bash中测试。
推荐文章
- “git restore”命令是什么?“git restore”和“git reset”之间有什么区别?
- Git合并与强制覆盖
- Git拉另一个分支
- 在Bash命令提示符上添加git分支
- 如何更改Git日志日期格式
- git pull -rebase和git pull -ff-only之间的区别
- GitHub -致命:无法读取用户名https://github.com':没有这样的文件或目录
- 撤消git平分错误
- 使用.gitconfig配置diff工具
- 如何配置Mac OS X术语,使git有颜色?
- Visual Studio Code: .git文件夹/文件隐藏
- 如何使用yyyyMMddHHmmss格式格式化当前时间?
- “node_modules”文件夹应该包含在git存储库中吗
- 为什么git-rebase给了我合并冲突,而我所做的只是压缩提交?
- 当我试图推到原点时,为什么Git告诉我“没有这样的远程‘原点’”?