资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

git如何修改commit时间

这篇文章主要介绍“git如何修改commit时间”,在日常操作中,相信很多人在git如何修改commit时间问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”git如何修改commit时间”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

创新互联建站主要从事网站建设、成都网站设计、网页设计、企业做网站、公司建网站等业务。立足成都服务台儿,十余年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:18980820575

在git中,可以利用“git commit --amend”命令来修改commit时间;该方法既可以修改最近一次提交的日期,也可以修改指定某次提交的日期,其中日期的格式需要是“ISO-8601”格式,语法为“GIT_COMMITTER_DATE="时间" git commit --amend --date="时间"”。

本文操作环境:Windows10系统、Git2.30.0版、Dell G3电脑。

git修改commit时间

1.修改最近一次提交的作者日期和提交者日期

如果要修改最近一次commit的作者日期和提交者日期,直接使用 git commit --amend即可

注:日期格式须为ISO-8601格式

GIT_COMMITTER_DATE="2017-10-08T09:51:07" git commit --amend --date="2017-10-08T09:51:07"

2.修改某次提交的作者日期和提交者日期

如果要更改某次(可以是最近一次也可以是非最近一次)提交的作者日期和提交者日期,可以使用交互式rebase:

  • 执行git rebase -i COMMIT_SHA , 此COMMIT_SHA为待修改日期的commit的前一个commit的commit sha

  • 在vi弹出交互信息中将待修改日期的commit前的pick修改为e

  • 执行日期修改命令 GIT_COMMITTER_DATE="2017-10-08T09:51:07" git commit --amend --date="2017-10-08T09:51:07"

  • 执行 git rebase --continue转到下一个commit

  • 重复此过程,直到修改所有提交。 通过git status可查看进展。

3.修改示例

当前git log提交信息如下

admin@DESKTOP-PC MINGW64 /e/TestProj/ModifyTimeTest (master)
$ git log --oneline
2fe64c4 (HEAD -> master) modify Readme.md 3
6b98331 modify Readme.md 2
98ddd80 modify Readme.md 1
fcfc064 add Readme.md

假设此时需要修改 6b98331 modify Readme.md 2这一commit的作者日期和提交者日期

修改步骤为:

执行交互式变基命令 git rebase -i 98ddd80

在弹出的vi编辑信息中,将 6b98331提交前的pick修改为e,随后执行 :wq 保存

e  6b98331 modify Readme.md 2      # 此处原为pick,将pick修改为e / edit
pick 2fe64c4 modify Readme.md 3
# Rebase 98ddd80..2fe64c4 onto 98ddd80 (2 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
# b, break = stop here (continue rebase later with 'git rebase --continue')
# d, drop  = remove commit
# l, label 

执行 GIT_COMMITTER_DATE="2021-10-22T15:10:07" git commit --amend --date="2021-10-22T15:10:07" 将作者日期和提交者日期均修改为2021-10-22T15:10:07。然后可选择在弹出的vi信息编辑窗中可修改提交日志,然后执行 :wq 保存

然后执行 git rebase --continue转到下一个提交,直到保存所有修改。完成后再使用git log查看提交信息即可看到提交信息已被修改

上述示例的完整日志如下:

admin@DESKTOP-PC MINGW64 /e/TestProj/ModifyTimeTest (master)
$ git log --oneline
2fe64c4 (HEAD -> master) modify Readme.md 3
6b98331 modify Readme.md 2
98ddd80 modify Readme.md 1
fcfc064 add Readme.md
admin@DESKTOP-PC MINGW64 /e/TestProj/ModifyTimeTest (master)
$ git rebase -i 98ddd80
Stopped at 6b98331...  modify Readme.md 2
You can amend the commit now, with
  git commit --amend
Once you are satisfied with your changes, run
  git rebase --continue
admin@DESKTOP-PC MINGW64 /e/TestProj/ModifyTimeTest (master|REBASE 1/2)
$ GIT_COMMITTER_DATE="2021-10-22T15:10:07" git commit --amend --date="2021-10-22T15:10:07"
[detached HEAD 137f41d] modify Readme.md 2
 Date: Fri Oct 22 15:10:07 2021 +0800
 1 file changed, 16 insertions(+)
admin@DESKTOP-PC MINGW64 /e/TestProj/ModifyTimeTest (master|REBASE 1/2)
$ git status
interactive rebase in progress; onto 98ddd80
Last command done (1 command done):
   edit 6b98331 modify Readme.md 2
Next command to do (1 remaining command):
   pick 2fe64c4 modify Readme.md 3
  (use "git rebase --edit-todo" to view and edit)
You are currently editing a commit while rebasing branch 'master' on '98ddd80'.
  (use "git commit --amend" to amend the current commit)
  (use "git rebase --continue" once you are satisfied with your changes)
nothing to commit, working tree clean
admin@DESKTOP-PC MINGW64 /e/TestProj/ModifyTimeTest (master|REBASE 1/2)
$ git rebase --continue
Successfully rebased and updated refs/heads/master.
admin@DESKTOP-PC MINGW64 /e/TestProj/ModifyTimeTest (master)
$ git status
On branch master
nothing to commit, working tree clean

到此,关于“git如何修改commit时间”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注创新互联网站,小编会继续努力为大家带来更多实用的文章!


网站名称:git如何修改commit时间
本文来源:http://cdkjz.cn/article/pposoi.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220