惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
V
V2EX
WordPress大学
WordPress大学
U
Unit 42
I
InfoQ
A
About on SuperTechFans
宝玉的分享
宝玉的分享
J
Java Code Geeks
博客园 - 司徒正美
爱范儿
爱范儿
Engineering at Meta
Engineering at Meta
G
Google Developers Blog
人人都是产品经理
人人都是产品经理
小众软件
小众软件
Microsoft Security Blog
Microsoft Security Blog
L
LangChain Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Hugging Face - Blog
Hugging Face - Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
aimingoo的专栏
aimingoo的专栏
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Last Week in AI
Last Week in AI
腾讯CDC
Recent Announcements
Recent Announcements

Oskyla 烹茶室

修复 Joplin on KDE 菜单栏显示问题 Copy Fail:Linux 内核 2017 年至今的高危漏洞(附临时缓解方案) | CVE-2026-31431 Hermes Agent — 在 K3s / K8s 中运行指南 在 K3s 节点上安装并使用 nerdctl Mouser:轻量开源的罗技鼠标驱动替代方案 Claude Opus 4.7:优缺点与评测信息汇总 openFuyao NPU-Operator故障排查 openFuyao 2603 共测测试报告 openFuyao InferNex AI推理集成部署 310P(300I Pro) 环境问题记录及解决 ceph mon Operation not permitted 问题解决 Ascend 310P + openFuyao + NPU-Operator 故障排查 KDE Plasma6 禁用全局菜单,恢复正常应用菜单 终极指南:在 Linux 裸机服务器上快速部署 Moltbot (原 Clawbot) 并集成飞书 Windows 配置 Claude Code 解决 settings.json 不生效 Windows 配置 Claude Code 全流程 2025-12-31 | 年终总结 AI 生图精品提示词|第二期:城市星球 AI 生图精品提示词|第一期 Kubernetes kubectl --raw 使用指南 彻底解决阿里云和 tailscale 冲突 2025-10-21 | 沉淀思维 macOS 单独为鼠标或触控板开启自然滚动 2025-10-16 | 负载高低 2025-10-15 | 睡眠周期 2025-10-14 | 转换情绪与独立观点 go 拉取 gitcode.com 私有 mod Git 将某个文件恢复到其他分支的状态 SSH 通过跳板机连接 lxc 使用 chronyc 构建 ntp 服务 2025-10-13 | 独立思考于未来能源
Git 覆写上次提交
Tianlun Song · 2024-12-12 · via Oskyla 烹茶室

本文 首发于 🌱 煎茶转载 请注明 来源

前言

在日常使用 Git 版本控制工具的时候,我们有时会遇到需要修改上次commit提交信息的情况,例如:修改上次提交信息中的错误内容,或者想为上次提交加入些新内容等等。

Git 覆写上次提交(amend)

我们可以使用git commit命令的amend选项来覆写上次提交。

$ git commit --amend

代码清单:使用--amend选项覆写上次提交

执行此命令后,git会自动弹出编辑界面,让你可以修改上次的提交信息

$ git commit --amend -m "an updated commit message"

代码清单:使用-m选项快速修改

而使用--no-edit选项,表示不更动上次已经提交的信息。适用于添加新内容到上次提交的情况。

$ git commit --amend --no-edit

代码清单:使用--no-edit选项追加内容

而使用--author选项,则可以仅修改提交者信息。

$ git commit --amend --author='someone <someone@example.com>'

代码清单:使用--author选项修改提交者

注意事项

--amend

    Replace the tip of the current branch by creating a new
    commit. The recorded tree is prepared as usual (including
    the effect of the -i and -o options and explicit pathspec),
    and the message from the original commit is used as the
    starting point, instead of an empty message, when no other
    message is specified from the command line via options such
    as -m, -F, -c, etc. The new commit has the same parents and
    author as the current one (the --reset-author option can
    countermand this).

    It is a rough equivalent for:

                $ git reset --soft HEAD^
                $ ... do something else to come up with the right tree ...
                $ git commit -c ORIG_HEAD

    but can be used to amend a merge commit.

    You should understand the implications of rewriting history
    if you amend a commit that has already been published. (See
    the "RECOVERING FROM UPSTREAM REBASE" section in git-
    rebase(1).)

注:关于--amend的说明文档

需要注意的是:amend选项不会直接修改上次的提交,而是会新创建出一枚提交,包含了上次提交的全部内容与新添加的内容,原先的提交会被“删除”。可以观察到,修改前后的commithash值是不同的。

所以,对于已经推送到远程仓库的提交,需谨慎使用amend

$ git push <remote> <branch> --force

代码清单:git强制覆盖远程分支

对于已经推送到远程仓库的提交,可以使用强制推送来覆盖(谨慎使用)。

注意事项图 By Pikicast

图:git commit --amend 注意事项图「取自Pikicast」

References

#Git