INTRODUCTION TO GIT DIFF COMMAND
In our last article on working with the git distributed version control system, we explained how to use the git log command to track changes and updates made to our git repository. In this article, we will see how we could check what changes were made to the files or more precisely the differences between the committed or saved versions of a file in the repository. For viewing the actual differences between the different versions of a record, we will use the git log command and the git diff command. To understand the differences between the current state of the file in the repository compared to their last known state, we use the git diff command.
A GIT DIFF COMMAND CAN BE USEFUL FOR COMPARING
- Show local changes
- Show difference between stagged and remote repo
- Show difference between two commits
- Show difference between two files
- Show difference between two branches
- Show difference between two tags
Example 1: View difference between the last commit and current version of a file
Continuing to use the git repository at location /home/sahil/git/my_first_repo, I’ve now added a line to the file test.txt in this repository.
Continuing to use the git repository at location /home/sahil/git/my_first_repo, I’ve now added a line to the file test.txt in this repository.
[sahil@linuxnix my_first_repo]$ cat test.txt This is a test file! Added another line to test file [sahil@linuxnix my_first_repo]$ echo "Adding a third line to test file" >> test.txt [sahil@linuxnix my_first_repo]$ cat test.txt This is a test file! Added another line to test file Adding a third line to test file
If we run the git status command we will observe that we have a file that has been modified and is ready to be moved to the staging area.
[sahil@linuxnix my_first_repo]$ git status # On branch master # 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: test.txt # no changes added to commit (use "git add" and/or "git commit -a") [sahil@linuxnix my_first_repo]$
Now let’s run the git diff command.
[sahil@linuxnix my_first_repo]$ git diff diff --git a/test.txt b/test.txt index ba08292..06ee124 100644 --- a/test.txt +++ b/test.txt @@ -1,2 +1,3 @@ This is a test file! Added another line to test file +Adding a third line to test file
As you may observe the git diff command gives a lot of useful information. The first line shows the file that has been modified and given a form and a b form with the b form representing the latest uncommitted version of the file. The next three lines show some information as to which file represents the structure and which represents the b form. The fourth line shows the line number that has changed i.e., the file earlier comprised of two lines and now consist of three lines with the addition of one line. What follows is the text contained in the file. Notice the plus (+) symbol with the third line. This indicates that this line has been added after the last commit.
Example 2: View differences between the staged and last committed version of a file
Let’s assume that we wish to keep the modifications that we just made to the test.txt file and commit them.
Let’s assume that we wish to keep the modifications that we just made to the test.txt file and commit them.
[sahil@linuxnix my_first_repo]$ git add . [sahil@linuxnix my_first_repo]$ git commit -m "Added third line to test.txt" [master 41bfa8f] Added third line to test.txt 1 file changed, 1 insertion(+) [sahil@linuxnix my_first_repo]$
We can use the “.“symbol with the “git add command” to add everything that has changed inside the repository to the staging area. Now let’s add another line to the file test.txt and move it to the staging area.
[sahil@linuxnix my_first_repo]$ echo "Adding a fourth line to test file" >> test.txt [sahil@linuxnix my_first_repo]$ [sahil@linuxnix my_first_repo]$ git add .
What if we step away from our system after adding the file to the staging area and forget what change we made?
We should use the git diff command to check for differences. But if we run git diff now we won’t see any output.
We should use the git diff command to check for differences. But if we run git diff now we won’t see any output.
[sahil@linuxnix my_first_repo]$ git diff [sahil@linuxnix my_first_repo]$
This is because since the file is now in the staging area, git knows about the file and so it doesn’t see a difference. But what if we wanted to view the differences between the staged version of the file and the last committed version?
We can do that by using git diff with the –staged option.
We can do that by using git diff with the –staged option.
[sahil@linuxnix my_first_repo]$ git diff --staged diff --git a/test.txt b/test.txt index 06ee124..e945f40 100644 --- a/test.txt +++ b/test.txt @@ -1,3 +1,4 @@ This is a test file! Added another line to test file Adding a third line to test file +Adding a fourth line to test file
Using the –staged option shows us the differences between the staged version of the file and the last known committed version of the file.
Example 3: Using git log to view differences between committed versions of files
In the previous two cases, we saw how we could use the git diff command to see the changes made between the last known committed versions of files and the latest version of the file or the latest staged version of the file. But what if we needed to view differences among committed versions? To accomplish this requirement, we use the git log command with the -p option. Here is a snippet from the output of the command.
In the previous two cases, we saw how we could use the git diff command to see the changes made between the last known committed versions of files and the latest version of the file or the latest staged version of the file. But what if we needed to view differences among committed versions? To accomplish this requirement, we use the git log command with the -p option. Here is a snippet from the output of the command.
[sahil@linuxnix my_first_repo]$ git log --graph -p * commit 41bfa8f9317859849b385a1e6bd55e5ef4107101 | Author: Sahil Suri <sahil.suri@example.com> | Date: Fri Mar 9 18:55:03 2018 +0530 | | Added third line to test.txt | | diff --git a/test.txt b/test.txt | index ba08292..06ee124 100644 | --- a/test.txt | +++ b/test.txt | @@ -1,2 +1,3 @@ | This is a test file! | Added another line to test file | +Adding a third line to test file | * commit 23c977041acf1730accd0622ad47b71a6a40a32a | Author: Sahil Suri <sahil.suri@example.com> | Date: Fri Mar 9 12:31:20 2018 +0530 | | Added another line to test.txt | | diff --git a/test.txt b/test.txt | index c66d471..ba08292 100644 | --- a/test.txt | +++ b/test.txt | @@ -1 +1,2 @@ | This is a test file! | +Added another line to test file
What the -p option does is that it provides an output similar to that of the git diff command for each commit message displayed as part of history log. But the additional output here is the difference between the previous version of the file and the modification made as part of the current commit operation.
Example 4: Using git log to view only the number of lines modified during commits
If using the -p option with the git log command appears to provide a more verbose output than what you would like to view frequently then you should consider using the –stat option. This reports merely the differences between commits regarding the number of lines added or deleted from files along with the names of the files that were modified. Given below is a snippet of its output.
If using the -p option with the git log command appears to provide a more verbose output than what you would like to view frequently then you should consider using the –stat option. This reports merely the differences between commits regarding the number of lines added or deleted from files along with the names of the files that were modified. Given below is a snippet of its output.
[sahil@linuxnix my_first_repo]$ git log --graph --stat * commit 41bfa8f9317859849b385a1e6bd55e5ef4107101 | Author: Sahil Suri <sahil.suri@example.com> | Date: Fri Mar 9 18:55:03 2018 +0530 | | Added third line to test.txt | | test.txt | 1 + | 1 file changed, 1 insertion(+) | * commit 23c977041acf1730accd0622ad47b71a6a40a32a | Author: Sahil Suri <sahil.suri@example.com> | Date: Fri Mar 9 12:31:20 2018 +0530 | | Added another line to test.txt | | test.txt | 1 + | 1 file changed, 1 insertion(+) | * commit a291f69bcdd40b9f9bf409cafe51030ab9b67e38 | Author: Sahil Suri <sahil.suri@example.com> | Date: Tue Mar 6 18:30:54 2018 +0530 | | Added file test.txt nad updated .md files | | README.md | 1 - | README1.md | 1 + | test.txt | 1 + | 3 files changed, 2 insertions(+), 1 deletion(-)
Example5: Find the difference between two branches of a git repository.
git diff branch_1..branch_2
Example(clipped):
git diff bug/fix_gcc_compiler..feature/test-12430 diff --git a/.kitchen.vagrant.yml b/.kitchen.vagrant.yml index a552e35..7cbf9da 100644 --- a/.kitchen.vagrant.yml +++ b/.kitchen.vagrant.yml @@ -26,6 +26,11 @@ platforms: box_url: file:///opt/coh/boxes/w2k12r2-std_DE-r001.box port: 5985 communicator: winrm + #transport: + # name: winrm + # elevated: true + # elevated_username: System + # elevated_password: null - name: w2k16-std_DE driver: @@ -34,6 +39,11 @@ platforms: box_url: file:///opt/coh/boxes/w2k16-std_DE-r001.box port: 5985 communicator: winrm + #transport: + # name: winrm + # elevated: true + # elevated_username: System + # elevated_password: null - name: w2k16-std_core driver:
Example6: Get the difference between two tags.
git diff tag_1.._tag_2
Example:
To know the avilable list of tags uses git tag -l, then use above command to get the difference between two tags.
git diff 1.0.0..1.1.0
diff --git a/.kitchen.yml b/.kitchen.yml index b228e65..bc012c3 100644 --- a/.kitchen.yml +++ b/.kitchen.yml @@ -18,7 +18,7 @@ transport: provisioner: name: chef_zero - require_chef_omnibus: 12 + require_chef_omnibus: 13 http_proxy: https://thiscode4u.blogspot.com:3128 https_proxy: https://thiscode4u.blogspot.com:3128 client_rb: @@ -57,5 +57,6 @@ suites: repo_server: https://thiscode4u.blogspot.com:8080 repo_server_url: https://thiscode4u.blogspot.com:8080 region: SYD01 + hpaas_env: production excludes: - rhel6 diff --git a/CHANGELOG.md b/CHANGELOG.md index 99c4ed7..85af953 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,9 @@ www_linuxnix_protectv CHANGELOG
Example 7: Get the difference between two commits. To compare commits, we should know the commit hash which we can get using “git log” command.
git diff commit_1.._commit_2
Example:
git diff 8ccc9c1b9a8ae6ae337f010c736db5bc5812694e..382fbf3bec503f6cf73bdfca126ad6eeca2168ae diff --git a/.kitchen.yml b/.kitchen.yml index bc012c3..fba882b 100644 --- a/.kitchen.yml +++ b/.kitchen.yml @@ -57,6 +57,5 @@ suites: repo_server: https://thiscode4u.blogspot.com:8080 repo_server_url: https://thiscode4u.blogspot.com:8080 region: SYD01 - hpaas_env: production excludes: - rhel6
CONCLUSION
In this article, we explained how you could view the changes made to file between commits, between staging and the last known commit or even between an updated file and the previous known committed state of the file in git. We used the git diff and the git log commands to read and review these changes. Tracking updates in files in a crucial aspect of working with a version control system and therefore we suggest you become more familiar with both these commands by practicing them in your environments.
0 comments:
Post a Comment