DEV Community

Cover image for Git commands to get information about changes made to your files
Shehroz Irfan
Shehroz Irfan

Posted on

Git commands to get information about changes made to your files

Hi,
In this article you will learn git commands which you can use to get information about the changes made to your files. Below is the list of commands that you'll learn in this article.

  • git log
  • git log -p
  • git show
  • git log --stat
  • git diff
  • git diff --staged


git log

The git log command shows the list of all commits that have been made in the current git repository.
Let's see the output when using git log:

Image description

In the image above you can see, by default the git log shows the commit id, author, date, and commit message.


git log -p

The git log shows the list of all the commits. But what if you want to see the actual lines changed in each commit?
So, in this case you can use the -p flag with git log. Here p comes from patch, because using this flag gives us the patch that was created.
Let's see the output when using git log -p:

Image description

In the image above you can see that it shows the added lines with plus(+) and removed lines with minus(-), and as the amount of output text is longer than what fits on the screen, git automatically uses a paging tool that allows you to scroll using page up, page down and arrows keys. You can quit the paging tool by entering 'q' from your keyboard. In the output you have one commit below the other and each commit takes up the different space depending on how many lines were added or removed in that commit.


git show

To see the actual lines changed in a specific commit you can use git show command. The git show command takes the commit id and then shows the files changed in that commit. You can get the commit id of a specific commit by using git log. Let say you have a commit id: '8a0d3873714c16cb8cc3810fe4861a770892ea97', now you can see the files changed to this commit using:

git show 8a0d3873714c16cb8cc3810fe4861a770892ea97

Below is the output:

Image description

In the image above you can see that it only showed the patch of that specific commit. The benefit of using git show is that you don't need to scroll to a specific commit in the output of git log -p to see the files changed. You can directly see the files changed by providing the commit id to git show command.


git log --stat

It is the interesting command that shows the stats about commits such as how many files are changed and how many lines are added or removed.
Lets see the output when using git log --stat:

Image description

In the image above you can see it showed the stats such as the number of files changed and the number of insertions and deletions.


git diff

This command shows the changes made to the files and the output is same as linux diff -u command. But one point to be noted here is that git diff only shows the un-staged(the changes that are not added to the staging area) changes.
Let's see the output when using git diff:

Image description

In the image above you can see that git diff shows the changes made to the file by plus(+) and minus(-) representing the insertions and deletions.


git diff --staged

As I mentioned above git diff only shows the un-staged changes. So, if you want to see the changed made to the files that are staged you can use --staged flag with git diff. It will show the changes made to the files present in the staging area.

Summary

There are plenty of other commands that you can use to get information about changes made. You can always use the reference documentation to find out more.
Thanks for reading!

Discussion (0)