修改最后一次提交
使用git commit --amend修改最后一次提交的提交信息或内容:
bash
git commit --amend -m "新的提交信息"
如需修改文件内容,先添加更改到暂存区:
bash
git add .
git commit --amend --no-edit
修改历史提交
通过交互式变基修改更早的提交:
bash
git rebase -i HEAD~3
在编辑器中将目标提交前的pick改为edit,保存后按流程操作:
bash
git add .
git commit --amend
git rebase --continue
强制推送更改
若已推送至远程仓库,需强制更新历史记录(谨慎使用):
bash
git push --force origin 分支名
注意事项
- 强制推送会覆盖远程历史,影响协作开发者
- 修改公共提交需团队协商