📅  最后修改于: 2023-12-03 15:00:57.629000             🧑  作者: Mango
Git是一个分布式版本控制系统,用于跟踪文件的变化和协调多人在同一个项目上的开发工作。Git的"差异"是指通过比较不同版本的文件来确定文件之间的变化。Git提供了一些功能强大的工具来查看和比较文件的差异,以帮助程序员更好地理解和管理代码的变化。
使用git diff
命令可以查看当前工作区与暂存区之间的差异。此命令将显示所有已修改但未添加到暂存区的文件的变化。
$ git diff
diff --git a/file.txt b/file.txt
index 1234567..2345678 100644
--- a/file.txt
+++ b/file.txt
@@ -1,3 +1,3 @@
This is some content in the file.
-Here is a line that has been modified.
+Here is a line that has been changed.
This is another line.
使用git diff HEAD
命令可以查看当前工作区与上一次提交之间的差异。此命令将显示所有已修改但未添加到暂存区的文件的变化。
$ git diff HEAD
diff --git a/file.txt b/file.txt
index 1234567..3456789 100644
--- a/file.txt
+++ b/file.txt
@@ -1,3 +1,3 @@
This is some content in the file.
-Here is a line that has been modified.
+Here is a line that has been changed.
This is another line.
使用git diff commit1 commit2
命令可以比较两个提交之间的差异。可以使用提交的哈希值、分支名或标签名来指定提交。
$ git diff commit1 commit2
diff --git a/file.txt b/file.txt
index 1234567..4567890 100644
--- a/file.txt
+++ b/file.txt
@@ -1,3 +1,3 @@
This is some content in the file.
-Here is a line that has been modified.
+Here is a line that has been changed.
This is another line.
使用git diff branch1 branch2
命令可以比较两个分支之间的差异。此命令将显示两个分支之间所有已修改但尚未合并的文件。
$ git diff branch1 branch2
diff --git a/file.txt b/file.txt
index 1234567..5678901 100644
--- a/file.txt
+++ b/file.txt
@@ -1,3 +1,3 @@
This is some content in the file.
-Here is a line that has been modified.
+Here is a line that has been changed.
This is another line.
以上就是Git差异的基本用法。通过比较不同的版本、分支或提交之间的差异,程序员可以更容易地理解代码的变化,从而更好地管理和维护代码库。