Home >Development Tools >git >How to modify the commit time in git
In git, you can use the "git commit --amend" command to modify the commit time; this method can not only modify the date of the most recent submission, but also modify the date of a specified submission, in which the format of the date It needs to be in "ISO-8601" format, and the syntax is "GIT_COMMITTER_DATE="time" git commit --amend --date="time"".
The operating environment of this article: Windows 10 system, Git version 2.30.0, Dell G3 computer.
1. Modify the author date and submitter date of the most recent commit
If you want to modify the author of the most recent commit For date and submitter date, just use git commit --amend directly
Note: The date format must be ISO-8601 format
GIT_COMMITTER_DATE="2017-10-08T09:51:07" git commit --amend --date="2017-10-08T09:51:07"
2. Modify the author date of a certain submission and submitter date
If you want to change the author date and submitter date of a certain submission (it can be the most recent or non-recent), you can use interactive rebase:
Execute git rebase -i COMMIT_SHA. This COMMIT_SHA is the commit sha of the previous commit of the commit to be modified.
In the vi pop-up interactive information, change the date to be modified. The pick before commit is modified to e
Execute the date modification command GIT_COMMITTER_DATE="2017-10-08T09:51:07" git commit --amend --date="2017-10- 08T09:51:07"
Execute git rebase --continue to the next commit
Repeat this process until all commits are modified. Progress can be viewed through git status.
3. Modification example
The current git log submission information is as follows
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
Assume that 6b98331 modify Readme needs to be modified at this time. The author date and submitter date of the commit md 2
The modification steps are:
Execute the interactive rebase command git rebase -i 98ddd80
In the pop-up vi editor In the information, change the pick before 6b98331 is submitted to e, and then execute: wq Save
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.
Execute GIT_COMMITTER_DATE="2021-10-22T15:10:07" git commit --amend --date="2021 -10-22T15:10:07" Modify both the author date and submitter date to 2021-10-22T15:10:07. Then you can choose to modify the commit log in the pop-up vi information editing window, and then execute :wq to save
and then execute git rebase --continue to go to the next commit until all changes are saved. After completion, use git log to view the submission information and you will see that the submission information has been modified.
The complete log of the above example is as follows:
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
Recommended learning: "Git Tutorial 》
The above is the detailed content of How to modify the commit time in git. For more information, please follow other related articles on the PHP Chinese website!