git commit 命令

git commit 命令

基本语法

bash 复制代码
git commit [选项] -m "提交信息"

常见选项

  1. -m <message> 指定提交信息,用于描述本次提交的内容。
bash 复制代码
git commit -m "修复了登录界面的显示问题"
  1. -a 自动暂存所有被修改过的文件(未包括新建文件),然后进行提交。
bash 复制代码
git commit -a -m "修改了多个文件的样式"
  1. --amend 修改最后一次提交,包括提交信息或新增文件。适用于刚刚提交后发现遗漏内容的情况。
bash 复制代码
# 修改提交信息
git commit --amend -m "新的提交信息"

# 添加漏掉的文件
git add 漏掉的文件
git commit --amend
  1. -v--verbose

显示具体的变更内容,便于确认提交内容。

  1. --no-edit

--amend 配合使用,保留上一次的提交信息,而不打开编辑器修改。

  1. --dry-run

模拟提交过程,不实际执行提交操作,用于检查提交是否会成功。

bash 复制代码
git commit --dry-run -m "测试提交"
  1. --quiet

减少输出内容,安静模式。

  1. --message-file=<file>-F <file>

从指定文件中读取提交信息。

  1. --allow-empty

允许创建一个空提交(无实际更改)。常用于标记版本、测试或文档更新。

bash 复制代码
git commit --allow-empty -m "这是一个空提交"
  1. --signoff-s

在提交信息的末尾添加"签名",通常用于开源项目。

bash 复制代码
git commit -s -m "添加了新功能"

结果类似:

复制代码
添加了新功能

Signed-off-by: Your Name <[email protected]>
  1. --reset-author

重置提交的作者信息为当前用户,适用于接手他人提交的情况。

  1. --date

指定提交的日期。

bash 复制代码
git commit --date="2024-12-31T12:34:56" -m "指定日期提交"

提交步骤

  1. 查看当前状态

    使用 git status 查看有哪些文件需要提交。

    bash 复制代码
    git status
  2. 添加文件到暂存区

    使用 git add 将文件添加到暂存区。

    bash 复制代码
    git add 文件名
    git add .
  3. 提交更改

    使用 git commit 提交暂存区的更改。

    bash 复制代码
    git commit -m "提交描述"
  4. 查看提交历史

    使用 git log 查看提交记录。

    bash 复制代码
    git log

示例

提交特定文件

bash 复制代码
git add index.html
git commit -m "更新了主页的 HTML 内容"

修改上一次提交

bash 复制代码
git commit --amend -m "更新了提交信息"

创建空提交

bash 复制代码
git commit --allow-empty -m "标记为版本 1.0"

签名提交

bash 复制代码
git commit -s -m "修复了错误日志打印问题"

提交时显示变更内容

bash 复制代码
git commit -v -m "优化代码结构"

相关推荐
江边垂钓者8 小时前
Git简介和发展
git
大卫小东(Sheldon)13 小时前
GIM: 调用AI自动生成git提交消息的工具
git·rust
程序设计实验室14 小时前
如何清理误提交到git的历史大文件?
git
江边垂钓者15 小时前
Git初始化相关配置
git
wumu_Love19 小时前
git 报错:错误:RPC 失败。curl 28 Failed to connect to github.com port 443 after 75000
git·rpc·github
powerfulzyh19 小时前
Git 时光机:修改Commit信息
git
极小狐21 小时前
如何使用极狐GitLab 软件包仓库功能托管 terraform?
linux·运维·git·ssh·gitlab·terraform
等等,要下雨2 天前
git常用命令
git
一直在学习的小白~2 天前
Sourcetree安装使用的详细教程
git
陈苏同学2 天前
从 Git 到 GitHub - 使用 Git 进行版本控制 - Git 常用命令
git·github