使用 git --amend 打补丁
Posted GoldenaArcher
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用 git --amend 打补丁相关的知识,希望对你有一定的参考价值。
使用 git --amend 打补丁
git --amend
只能用来修改上一条 committed message,没办法修改好几条记录。
下面进入案例时间,我这里修改了两个文件,并且只 commit 了其中一个:
➜ basic git:(main) ✗ git status
On branch main
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: out.txt
Untracked files:
(use "git add <file>..." to include in what will be committed)
ch2.txt
no changes added to commit (use "git add" and/or "git commit -a")
➜ basic git:(main) ✗ git add out.txt
➜ basic git:(main) ✗ git commit -m "added new message"
[main 395fa2a] added new message
1 file changed, 3 insertions(+), 1 deletion(-)
➜ basic git:(main) ✗ git log
commit 395fa2ac3e6295569daa9120db2c26be5b46ee29 (HEAD -> main)
Author: Lou Han <>
Date: Fri Nov 4 16:56:17 2022 -0400
added new message
commit 2b86ba843ad89932bf2aedf575780417e200e1e3
Author: Lou Han <>
Date: Fri Nov 4 16:21:59 2022 -0400
This is a demo to show multiple lines
multiple lines
在已经 commit 了之后我突然发现有一个内容忘了加了,或者是上次消息有个 typo,需要修改一下,这个时候可以使用 --amend
这个 flag 进行实现:
➜ basic git:(main) ✗ git status
On branch main
Untracked files:
(use "git add <file>..." to include in what will be committed)
ch2.txt
nothing added to commit but untracked files present (use "git add" to track)
➜ basic git:(main) ✗ git add ch2.txt
➜ basic git:(main) ✗ git commit --amend
[main 65fc691] added new message
Date: Fri Nov 4 16:56:17 2022 -0400
2 files changed, 3 insertions(+), 1 deletion(-)
create mode 100644 ch2.txt
➜ basic git:(main) git log
commit 65fc6912f8204ad42f0af0261c03c799d83f82dc (HEAD -> main)
Author: Lou Han <>
Date: Fri Nov 4 16:56:17 2022 -0400
added new message
commit 2b86ba843ad89932bf2aedf575780417e200e1e3
Author: Lou Han <>
Date: Fri Nov 4 16:21:59 2022 -0400
This is a demo to show multiple lines
multiple lines
可以对比一下 hash value,第一次操作的时候,hash value 是 395fa2ac3e6295569daa9120db2c26be5b46ee29,而第二次操作的时候,hash value 已经变成了 65fc6912f8204ad42f0af0261c03c799d83f82dc。
官方文档说会重新创建一个 commit,并重置上一条 commit,大概等同于:
$ git reset --soft HEAD^
$ ... do something else to come up with the right tree ...
$ git commit -c ORIG_HEAD
惭愧,我现在就用 reset 做的,之后应该用 amend 打补丁了
以上是关于使用 git --amend 打补丁的主要内容,如果未能解决你的问题,请参考以下文章