Git Amend
Git commit --amend
commit --amend
用于修改最近的 commit
。
它将 暂存区
中的更改与最新的 commit
合并,并创建一个新的 commit
。
这个新的 commit
将完全替换最近的 commit
。
Git Amend Commit Message
使用 --amend
可以做的最简单的事情之一就是更改 commit
消息。
让我们更新 README.md
并 commit
示例
git commit -m "Adding plines to reddme"
[master 07c5bc5] Adding plines to reddme
1 file changed, 3 insertions(+), 1 deletion(-)
现在让我们检查一下 log
示例
git log --oneline
07c5bc5 (HEAD -> master) Adding plines to reddme
9a9add8 (origin/master) Added .gitignore
81912ba Corrected spelling error
3fdaa5b Merge pull request #1 from w3schools-test/update-readme
836e5bf (origin/update-readme, update-readme) Updated readme for GitHub Branches
daf4f7c (origin/html-skeleton, html-skeleton) Updated index.html with basic meta
facaeae (gh-page/master) Merge branch 'master' of https://github.com/w3schools-test/hello-world
e7de78f Updated index.html. Resized image
5a04b6f Updated README.md with a line about focus
d29d69f Updated README.md with a line about GitHub
e0b6038 merged with hello-world-images after fixing conflicts
1f1584e added new image
dfa79db updated index.html with emergency fix
0312c55 Added image to Hello World
09f4acd Updated index.html with a new line
221ec6e First release of Hello World!
哦不!commit
消息里有拼写错误。真丢人。让我们 amend
一下
示例
git commit --amend -m "Added lines to README.md"
[master eaa69ce] Added lines to README.md
Date: Thu Apr 22 12:18:52 2021 +0200
1 file changed, 3 insertions(+), 1 deletion(-))
然后重新检查 log
示例
git log --oneline
eaa69ce (HEAD -> master) Added lines to README.md
9a9add8 (origin/master) Added .gitignore
81912ba Corrected spelling error
3fdaa5b Merge pull request #1 from w3schools-test/update-readme
836e5bf (origin/update-readme, update-readme) Updated readme for GitHub Branches
daf4f7c (origin/html-skeleton, html-skeleton) Updated index.html with basic meta
facaeae (gh-page/master) Merge branch 'master' of https://github.com/w3schools-test/hello-world
e7de78f Updated index.html. Resized image
5a04b6f Updated README.md with a line about focus
d29d69f Updated README.md with a line about GitHub
e0b6038 merged with hello-world-images after fixing conflicts
1f1584e added new image
dfa79db updated index.html with emergency fix
0312c55 Added image to Hello World
09f4acd Updated index.html with a new line
221ec6e First release of Hello World!
我们看到之前的 commit
被我们修正后的提交替换了!
警告: 修改仓库的 commit
历史可能会很危险。通常情况下,对您自己的本地仓库进行此类更改是可以的。但是,您应该避免对 remote
仓库进行重写历史的操作,特别是当其他人也在使用它们时。
Git Amend Files
使用 --amend
添加文件的方法与上面相同。在提交之前,只需将它们添加到 暂存区
即可。