git commit 将暂存区内容提交到版本库
本文主要记录 git commit 几个常用用法,日常开发中,这几个参数基本够用了
// 将暂存区内容提交到版本库
git commit -m [message]
// 跳过 git add,将所有已被跟踪的文件更改直接提交到版本库
git commit -am [message]
// 修改上一次的提交信息 (删除当前最新的一条记录,重新提交)
git commit --amend -m <message>
基本使用
git commit -m [message]
将某些文件提交到版本库 (可以跳过 git add,但必须是已被跟踪的文件)
git commit [file1] [file2] -m [message]
如果需要将所有已被跟踪的文件更改内容,不想使用 git add,直接添加到版本库
总结: git commit -m 用于提交暂存区中的文件,git commit -am 用于提交已被跟踪的文件
# 将已被跟踪的文件提交的本地库中
git commit -am <message>
commit 命令的 -am 参数等价于执行了下面两个命令
# 将已被跟踪的文件提交到暂存区
git add -u
# 将暂存区中的文件提交到本地库
git commit -m <message>
在日常开发中,难免会出现提交时备注信息打错了,想要修改它。
# 进入 vi 编辑模式,最上方就是提交时填写的备注信息
git commit --amend
# 无需进入 vi 编辑模式,修改上次提交记录的备注信息
git commit --amend -m <message>
修改备注信息后,保存退出即可。该操作会修改上次提交的索引 id
创建一个 git 仓库,用于测试 git commit 背后做了什么操作
echo 'hello git' >> 1.txt
echo 'hello svn' >> 2.txt
git init
git add .
# 提交到版本库,注意 .git 目录下的变化
git commit -m 'add 1.txt 2.txt'
一、执行 git commit 后会在 .git/objects
目录中下面生成两个文件(一个是 commit 类型,一个是 tree 类型)
第二次执行 commit 后,查看这次 commit id 的文件内容,其中 parent
后面的值是上一次提交记录的 commit id
当文件存放在目录中时,tree 类型的 objects 文件中还会有一个 tree(新增了一个 user/4.txt 文件)
二、假设当前在 master 分支,那么执行 git commit 后会在 .git/refs/heads/master
中写入最新提交记录的 commit id
$ cat .git/refs/heads/master
f08f522f3210eccbcff1d5e16c3da72435583f5e