Git笔记:提交及更新备注和日期

git commit -m "commit message" # 提交所有更新过的文件

1 、git commit [file1] [file2] -m "备注"

$ git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   add.txt
        new file:   delete.txt
        new file:   update.txt

Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        deleted:    delete.txt
        modified:   update.txt

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        new.txt


$ git commit add.txt -m "commit add.txt"  # 提交指定文件
[master 918269c] commit add.txt
 1 file changed, 1 insertion(+)
 create mode 100644 add.txt

$ git status
On branch master
Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory) # 仅add.txt从暂存区提交
        deleted:    delete.txt
        modified:   update.txt

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        new.txt

no changes added to commit (use "git add" and/or "git commit -a")

2、 git commit --amend 修改最近一次提交的备注

$ git commit --amend  # 修改上次提交的备注信息
[master 097a12e] commit add.txt  修改--amend
 Date: Fri Jun 6 08:19:51 2025 +0800
 3 files changed, 3 insertions(+)
 create mode 100644 add.txt
 create mode 100644 delete.txt
 create mode 100644 update.txt

修改之前提交的备注
step1:git rebase -i HEAD~2 # HEAD~2 倒数2个,进入vim模式
pick删除修改为e或edit(修改对应提交所在行的)
step2:git commit --amend # 进入vim模式,修改备注(对应上述commit-hash)
step3: git rebase --continue # 完成修改

29447@GW64 /d/myProject (main)
$ git log --oneline -3
b6bdac6 (HEAD -> main) test_branch add W 13:36
0a93bc3 test_branch add E  # 期望修改倒数第2次提交
7d90224 Merge branch 'test_branch'

29447@GW64 /d/myProject (main)
$ git rebase -i HEAD~2 # HEAD~2 倒数2个,进入vim模式
# pick删除修改为e或edit(修改对应提交所在行的)

Stopped at 0a93bc3...  test_branch add Z 13:35
You can amend the commit now, with
  git commit --amend
Once you are satisfied with your changes, run
  git rebase --continue

29447@GW64 /d/myProject (main|REBASE 1/2)
$ git commit --amend  # 进入vim模式,修改备注(对应上述commit-hash)
# 注意保存

[detached HEAD 0a93bc3 ] test_branch add Z 13:35
 Date: Mon Jun 16 13:33:27 2025 +0800
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 E

29447@GW64 /d/myProject (main|REBASE 1/2)
$ git rebase --continue  # 完成修改
Successfully rebased and updated refs/heads/main.

29447@GW64 /d/myProject (main)
$ git log --oneline -3
b6bdac6 (HEAD -> main) test_branch add W 13:36
0a93bc3 test_branch add Z 13:35  # 修改成功
7d90224 Merge branch 'test_branch'

$

git commit --amend --date="YYYY-MM-DDTHH:MM:SS" 更新提交日期
YYYY-MM-DD 表示年月日
HH:MM:SS 表示时分秒
PS:之前提交的日期,则需要与rebase结合使用
若针对已经推送远程的提交修改,则实际效果相当于多一次commit(本地修改与远程仓库中hash不一致)

$ git log --graph  -2
*   commit ebd0f75edc33e3d5a4190040541ee01a3439ca3a (HEAD -> temp_branch, main)
|\  Merge: de6b667 53c7f75
| | Author: bai-cao <2944717202@qq.com>
| | Date:   Wed Jun 18 12:29:25 2025 +0800
| |
| |     Merge branch 'new_root'
| |
| *   commit 53c7f7555082b0b4abd900c62ff4cadece5643c6
| |\  Merge: 373f4da 64eb279
| | | Author: bai-cao <2944717202@qq.com>
| | | Date:   Tue Jun 17 16:32:14 2025 +0800
| | |
| | |     edit root&git pull origin main:Merge branch 'main' of origin (2025/6/17 16:32)

$ git commit --amend --date="2025-06-17T23:30:00"  # 进入vim编辑,可以同步修改备注
[temp_branch c68e6db] temp_branch: edit[Merge branch 'new_root']
 Date: Tue Jun 17 23:30:00 2025 +0800

$ git log --graph  -2
*   commit c68e6dbf380533d6a57ed337c46b7f7d111bb77d (HEAD -> temp_branch)
|\  Merge: de6b667 53c7f75
| | Author: bai-cao <2944717202@qq.com>
| | Date:   Tue Jun 17 23:30:00 2025 +0800 # 提交日期已修改
| |
| |     temp_branch: edit[Merge branch 'new_root']  # 已同步修改
| |
| *   commit 53c7f7555082b0b4abd900c62ff4cadece5643c6
| |\  Merge: 373f4da 64eb279
| | | Author: bai-cao <2944717202@qq.com>
| | | Date:   Tue Jun 17 16:32:14 2025 +0800
| | |
| | |     edit root&git pull origin main:Merge branch 'main' of origin (2025/6/17 16:32)


3、 git commit -a 已跟踪文件add与commit同步操作

$ git commit -am "new"
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        new.txt
nothing added to commit but untracked files present (use "git add" to track) # 不处理未被跟踪的文件

# 不指定文件[file]
$ git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   add.txt
        modified:   file1.txt
        modified:   update.txt
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        new.txt
no changes added to commit (use "git add" and/or "git commit -a")

$ git commit -a add.txt -m "modified add.txt"
fatal: paths 'add.txt ...' with -a does not make sense  # -a不能与 git commit file 一起使用

$ git commit -a -m "modified add.txt"  # 已跟踪过的文件,删改后可以同步跟踪提交
[master ed39a46] modified add.txt
 3 files changed, 3 insertions(+), 3 deletions(-)

$ git status
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        new.txt

nothing added to commit but untracked files present (use "git add" to track)
$ git status
On branch master
Changes to be committed:  # 暂存区,即add后的文件
  (use "git restore --staged <file>..." to unstage)
        new file:   new.txt

$ git status
On branch master
Changes to be committed:  # 暂存区,即add后的文件
  (use "git restore --staged <file>..." to unstage)
        new file:   new.txt
Changes not staged for commit: # add或commit后修改或删除
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   new.txt

$ git commit -m "commit 1"
[master 7bdd6dd] commit 1
 1 file changed, 1 insertion(+)   # 版本库新增文件
 create mode 100644 new.txt

$ git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   new.txt
no changes added to commit (use "git add" and/or "git commit -a")

$ git commit -am "commit 2"
[master 03193ee] commit 2
 1 file changed, 1 insertion(+), 1 deletion(-)    # 版本库原文件移除,新增新文件

$ git status
On branch master
nothing to commit, working tree clean

4、参考

1、git修改历史提交(commit)信息
2、Git更新git提交的作者日期(amendment)|极客教程

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容