Using Git Status and Git Diff

(For this post I’m using Lab 3 from Brent Laster’s book Professional Git. I recommend this book if you are new to Git, like myself)

I’ve shown in my previous posts how to move files in-between the various stages in git: Working Directory -> Staging -> Local Repository. With files being able to be in different stages, you need a way to keep track of where everything is, and what version is where. This is where the two commands git status and git diff come into play. Git status provides status information on changes in the local environment that have not been committed yet. Git diff shows the differences between content at the various stages in your environment. Let’s run through an example.

I’m going to continue working the mylab repository that I’ve been working in on my previous posts. First, I’ll execute a git status.

I currently have no changes to be staged or committed. I’m going to create a new file, file3.c, and then check the status again.

The file is considered untracked. This means I’ve created the file in the local working directory, but I haven’t added it into Git yet. Now let’s stage the file and check its status.

The file is now being tracked, as we have added it to the staging area. At this point I have the same file in the staging area that I do my working directory, but nothing has been added yet to the local repository.

Next, I’m going to modify the same file that is in my working directory.

The file is listed twice. The first listing is the one in the staging area, waiting to be committed. The second is the updated file in my working directory. I have not moved it to the staging area yet, so it shows as not staged for commit. Note however, the file is still considered tracked, because I have moved a previous version into the staging area, therefore Git is aware of the file.

I can execute a git diff command to see the different between the files in the working directory vs the files in staging.

The a/ file is the working directory version, while the b/ file is the staging area file. Here I can see that in the file in the working directory, content was removed and change was added.

I’m going to commit the changes in staging and do another status check.

Now when I execute a git status I only see the file in the working directory that has not been staged. Let’s stage that file.

As expected, I see the file listed as waiting to be committed. Now let’s make a change to the file in the working directory again.

I am back to the situation I was in earlier, with a version of the file in my working directory, and a version in my staging area. If I execute a git diff then it will diff the version in the working area against the version in staging. If I execute a git diff –staged then it will diff the version in staging against the version in the local repository.

I can also compare the differences between the file in my local directory with the version in the local repository.

  • Add Your Comment