您的位置:首页 > 其它

git - 查看已暂存和未暂存的更新以及文件修改之后暂存区、上次提交过的不同

2015-04-10 10:35 197 查看
实际上
git
status
的显示比较简单,仅仅是列出了修改过的文件,如果要查看具体修改了什么地方,可以用
git
diff
命令。稍后我们会详细介绍
git
diff
,不过现在,它已经能回答我们的两个问题了:当前做的哪些更新还没有暂存?有哪些更新已经暂存起来准备好了下次提交?
git
diff
会使用文件补丁的格式显示具体添加和删除的行。

假如再次修改
README
文件后暂存,然后编辑
benchmarks.rb
文件后先别暂存,运行
status
命令将会看到:
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)

new file:   README

Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)

modified:   benchmarks.rb


要查看尚未暂存的文件更新了哪些部分,不加参数直接输入
git
diff

$ git diff
diff --git a/benchmarks.rb b/benchmarks.rb
index 3cb747f..da65585 100644
--- a/benchmarks.rb
+++ b/benchmarks.rb
@@ -36,6 +36,10 @@ def main
@commit.parents[0].parents[0].parents[0]
end

+        run_code(x, 'commits 1') do
+          git.commits.size
+        end
+
run_code(x, 'commits 2') do
log = git.commits('master', 15)
log.size


此命令比较的是工作目录中当前文件和暂存区域快照之间的差异,也就是修改之后还没有暂存起来的变化内容。

若要看已经暂存起来的文件和上次提交时的快照之间的差异,可以用
git
diff --cached
命令。(Git 1.6.1 及更高版本还允许使用
git
diff --staged
,效果是相同的,但更好记些。)来看看实际的效果:
$ git diff --cached
diff --git a/README b/README
new file mode 100644
index 0000000..03902a1
--- /dev/null
+++ b/README2
@@ -0,0 +1,5 @@
+grit
+ by Tom Preston-Werner, Chris Wanstrath
+ http://github.com/mojombo/grit +
+Grit is a Ruby library for extracting information from a Git repository


请注意,单单
git
diff
不过是显示还没有暂存起来的改动,而不是这次工作和上次提交之间的差异。所以有时候你一下子暂存了所有更新过的文件后,运行
git
diff
后却什么也没有,就是这个原因。

像之前说的,暂存 benchmarks.rb 后再编辑,运行
git
status
会看到暂存前后的两个版本:
$ git add benchmarks.rb
$ echo '# test line' >> benchmarks.rb
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)

modified:   benchmarks.rb

Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)

modified:   benchmarks.rb


现在运行
git
diff
看暂存前后的变化:
$ git diff
diff --git a/benchmarks.rb b/benchmarks.rb
index e445e28..86b2f7c 100644
--- a/benchmarks.rb
+++ b/benchmarks.rb
@@ -127,3 +127,4 @@ end
main()

##pp Grit::GitRuby.cache_client.stats
+# test line


然后用
git
diff --cached
查看已经暂存起来的变化:
$ git diff --cached
diff --git a/benchmarks.rb b/benchmarks.rb
index 3cb747f..e445e28 100644
--- a/benchmarks.rb
+++ b/benchmarks.rb
@@ -36,6 +36,10 @@ def main
@commit.parents[0].parents[0].parents[0]
end

+        run_code(x, 'commits 1') do
+          git.commits.size
+        end
+
run_code(x, 'commits 2') do
log = git.commits('master', 15)
log.size

[/code]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: