005-添加和提交文件
查看仓库的状态
命令:
git status
示例:
$ git status
On branch master
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
file1.txt
nothing added to commit but untracked files present (use "git add" to track)
添加文件到暂存区
命令:
git add <file>
示例:
$ git add file1.txt
$ git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: file1.txt
git支持使用通配符,例如:
$ echo "file3" > file3.txt
$ echo "file4" > file4.txt
$ echo "file5" > file4.md
$ echo "file6" > file4.sh
$ mv file4.md file5.md
$ git status
On branch master
Untracked files:
(use "git add <file>..." to include in what will be committed)
file2.txt
file3.txt
file4.sh
file4.txt
file5.md
nothing added to commit but untracked files present (use "git add" to track)
$ git add *.txt
$ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: file2.txt
new file: file3.txt
new file: file4.txt
Untracked files:
(use "git add <file>..." to include in what will be committed)
file4.sh
file5.md
$ git add .
$ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: file2.txt
new file: file3.txt
new file: file4.sh
new file: file4.txt
new file: file5.md
提交文件到仓库
只提交暂存区的内容,不会提交工作区的内容。命令:
git commit -m "提交信息"
示例:
$ git commit -m "这是第一次提交"
[master (root-commit) 8feb17e] 这是第一次提交
1 file changed, 1 insertion(+)
create mode 100644 file1.txt
$ git status
On branch master
Untracked files:
(use "git add <file>..." to include in what will be committed)
file2.txt
nothing added to commit but untracked files present (use "git add" to track)
可以看到,file1.txt已经被提交到仓库了,而file2.txt还在工作区,没有被提交到仓库。
查看提交历史
git log
$ git log
commit 23fbc97586b1f783a69a489c2c493c214ee9f47f (HEAD -> master)
Author: DaijieWu <2651106052@qq.com>
Date: Sat Apr 4 17:57:42 2026 +0800
这是第二次提交
commit 8feb17eb6925ac04da77a023ecfd4635bb68cd16
Author: DaijieWu <2651106052@qq.com>
Date: Sat Apr 4 17:54:16 2026 +0800
这是第一次提交
查看提交历史(简洁版)
git log --oneline
$ git log --oneline
23fbc97 (HEAD -> master) 这是第二次提交
8feb17e 这是第一次提交