以一个readme.txt为例,先做一个初始化commit。
echo "hello world">>readme.txt
添加一行内容- 使用
git add
添加前:
2.1git status
查看哪些文件发生了变化,可以看到modified: readme.txt
。
2.2git status -s
是简略版的git status
,可以看到M readme.txt
2.3git diff
或者git diff readme.txt
查看文件发生了哪些变化:
可以看到+hello world
- 使用
git add .
添加文件到缓冲区(staging area)后再次查看变化:
3.1git status
和git status -s
基本结果一样,只是信息从Changed not staged for commit
变成Changes to be committed
。
3.2git diff --cached
或者git diff --staged
查看缓冲区的文件变化(此时git diff
已经无信息了,因为文件都被加到了缓冲区)+hello world
git diff HEAD
。
添加另外一行内容echo "This is Simon">>readme.txt
,然后此时
4.1git diff
可以看到在helloworld
基础上+This is Simon
4.2git diff --cached
可以看到还是上次放到缓冲区的内容+hello world
4.3git diff HEAD
可以看到+helloworld
和+This is Simon
。所以git diff HEAD
是git diff
和git diff --cached
的合并。
总结:
git status
查看改变的文件列表
git status -s
查看改变的文件列表简化版
git diff
或者git diff filename
查看不在缓冲区的文件发生的改变
git diff --cached
或者git diff --staged
查看缓冲区的文件发生的改变
git diff HEAD
是git diff
和git diff --cached
的合并