【My Study Note】Diff Commands
Diff Commands
You probably know that the git status command tells you which of your files have been changed. The git diff command it goes a step further and tells you what exactly these changes are.
When used together, you can think of them as a file system. Git status tells you the file names, but to open the file and see the contents, you’ll need to use git diff.
Diff is used to make comparisons against files on your local repository. It can also be used against commits and against branches.
When I go into my local repository, I’ll find a file called READ ME that I’d like to update slightly.
% ls -la
total 8
drwxr-xr-x 5 naoya admin 160 6 Dec 23:34 .
drwxr-xr-x 3 naoya admin 96 6 Dec 23:33 ..
drwxr-xr-x 13 naoya admin 416 6 Dec 23:36 .git
-rw-r--r-- 1 naoya admin 15 6 Dec 23:33 README.md
-rw-r--r-- 1 naoya admin 0 6 Dec 23:34 result.txt
You can do this with any editor such as VS code. I can also do this by executing the Vim command to enter the file for editing, remove a few words, and then save it (by typing :wq!).
vim README.md
Next, I’m going to use the git diff tool to compare the updated file against the head. Because we haven’t yet completed a commit. It’s not available for a comparison against another commit. This then returns an output showing the changes that occurred in each file. Here, the line starting with a minus symbol represents what it originally was. While the line with a plus symbol shows what it is now.
% git diff HEAD README.md
diff --git a/README.md b/README.md
index 4b0f1eb..3418871 100644
--- a/README.md
+++ b/README.md
@@ -1 +1 @@
-# repo-exercise
\ No newline at end of file
+# repo-exercisesnoiijknkn:~
In addition to individual files, you can also make comparisons against previous commits. I’ll start by using the git log command to display my history of commits. I’ll also use the pretty flag here so that each one is shown in one line.
% git log --pretty=onelin
e
e18a1f1c695f35264dbdcd7d8ac9cfaae69e9680 (HEAD -> main, origin/main, origin/HEAD) Successful exercise
66bb228919ff4443110cdc2a9027554759fab0ce Initial commit
Each commit has its own ID code. So I’ll perform a git diff command on the codes from the most recent commit and from the very first one. Git hub will go through all the files, note all the changes that have occurred, and return the differentiation between the two.
git diff e18a1f1c695f35264dbdcd7d8ac9cfaae69e9680 66bb228919ff4443110cdc2a9027554759fab0ce
diff --git a/result.txt b/result.txt
deleted file mode 100644
index e69de29..0000000
Making comparisons against branches
If I perform the command git branch, it will display all the branches that are available in the repository.
git branch
feature/testing-branches
* main
I can then use the git diff command to pass in my main branch, followed by my feature branch as the second option. Once again, this will display all the changes that have occurred between the two.
git diff main feature/testing-branches
This article is about how to use git diff command to keep track of changes across your files, branches and commits. This tool can help you to stay on top of updates and avoid mistakes or overlap.