ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

如何修改Git某次commit提交的时间

2021-10-22 15:30:29  阅读:520  来源: 互联网

标签:10 Git md -- 某次 git Readme commit


如何修改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:

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

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

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

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

  5. 重复此过程,直到修改所有提交。 通过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的作者日期和提交者日期

修改步骤为:

  1. 执行交互式变基命令 git rebase -i 98ddd80
  2. 在弹出的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 <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
# f, fixup <commit> = like "squash", but discard this commit's log message
# x, exec <command> = run command (the rest of the line) using shell
# b, break = stop here (continue rebase later with 'git rebase --continue')
# d, drop <commit> = remove commit
# l, label <label> = label current HEAD with a name
# t, reset <label> = reset HEAD to a label
# m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
# .       create a merge commit using the original merge commit's
# .       message (or the oneline, if no original merge commit was
# .       specified). Use -c <commit> to reword the commit message.
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
  1. 执行 GIT_COMMITTER_DATE="2021-10-22T15:10:07" git commit --amend --date="2021-10-22T15:10:07" 将作者日期和提交者日期均修改为2021-10-22T15:10:07。然后可选择在弹出的vi信息编辑窗中可修改提交日志,然后执行 :wq 保存

  2. 然后执行 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

标签:10,Git,md,--,某次,git,Readme,commit
来源: https://blog.csdn.net/shadow_2011/article/details/120906876

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有