git commit --amend 就是解决一些很小的改动,使得你不想在重现创建一个commit而设定。如果你的commit已经push到了远程仓库,那么使用--amend修改commit后,git push时一定要使用 --force-with-lease 参数。否则就会报错:
error: 无法推送一些引用到 'https://github.com/×××/test.git'
提示:更新被拒绝,因为您当前分支的最新提交落后于其对应的远程分支。
提示:再次推送前,先与远程变更合并(如 'git pull ...')。详见
提示:'git push --help' 中的 'Note about fast-forwards' 小节。
下面是我的例子:
步骤1:查看已修改的状态
[root@localhost test]# git status
位于分支 master
您的分支与上游分支 'origin/master' 一致。
尚未暂存以备提交的变更:
(使用 "git add <file>..." 更新要提交的内容)
(使用 "git checkout -- <file>..." 丢弃工作区的改动)
修改: main.c
修改: test.S
修改尚未加入提交(使用 "git add" 和/或 "git commit -a")
FIXME:网上说如果你要追加修改的不是基于最新commit,那么只需要执行git reset --hard 要追加修改的commit id,然后在修改即可。但是我测试的结果是,如果我后退2个版本修改,结果把这2个版本都覆盖了
步骤2:把要修改的文件提交到本地库
[root@localhost test]# git add .
步骤3: commit
[root@localhost test]# git commit --amend -m "delete un used file 3"
[master d79a6e5] delete un used file 3
Date: Wed Dec 4 12:58:16 2019 +0800
2 files changed, 1 insertion(+), 13 deletions(-)
注意:-m “这里的内容和要追加的commit相同即可”,当然你也可以修改
步骤4:强制推送到远程分支
[root@localhost test]# git push --force-with-lease origin master
对象计数中: 4, 完成.
Delta compression using up to 4 threads.
压缩对象中: 100% (4/4), 完成.
写入对象中: 100% (4/4), 420 bytes | 0 bytes/s, 完成.
Total 4 (delta 2), reused 0 (delta 0)
remote: Resolving deltas: 100% (2/2), completed with 2 local objects.
To https://github.com/sunny868/test.git
26dedb2..d79a6e5 master -> master