git-show

Learn via video courses
Topics Covered

Overview

Git is a distributed version control system that is open-source. Git allows developers to work and manage the same source code. Git has many commands that we can use to perform various actions. However, there are many Git commands and concepts that are still not known to people. One such command is git show.

In this article, we'll learn about the concept of git config show.

Pre-Requisites

Show of Git is an intermediate concept, a beginner will not see this command in use often. Hence, this concept will only make sense to you if you know the following topics:

  • Basic Git commands and concepts
    • git init
    • git commit
    • git status
    • git log
    • git branch
  • Git Objects

Introduction to Git Show

Developers can view the contents of Git objects in a Git repository with the powerful git show command.

Git uses four primary types of Git objects to track your code changes as you add and commit them: Blobs, Trees, Commits, and Tags. These items are kept by Git in its object database. The database is hidden inside the .git/ subdirectory.

Let's discuss what git config show will display about these four objects:

  1. Blobs: It presents the simple data stored in the blob file(it's a lower-level object type).
  2. Trees: It presents the name of the file and directory stored within the tree file.
  3. Commits: Similar to what we would get in a Git log, along with a textual difference of the changes made in that commit.
  4. Tags: As git tag is just a marker for a commit, it shows the same thing as it does in Commits.

Use git show to query any of these Git object types. Now, let's discuss some of the usages in detail.

Git Show Basic Usage

Syntax

Let's first see the second parameter, the <object> parameter.

The default value of the object parameter is HEAD. Except in the case of a detached head state, the most recent commit on the current branch is typically referenced by the Git HEAD command. As a result, if you run git show alone, you will be requesting the most recent commit on the active branch.

Below is an example of a git show command when performed on a directory with only one commit.

GIT SHOW

The output produced by the command git show HEAD will exactly be the same. The output provides a textual difference of the changes contained in the HEAD commit as well as the commit's commit information. This can be useful when we want the information provided by the git diff plus the commit information provided by the git log.

Git Show Options

We discussed the <object>, the second parameter of the git show command. Now let's see into the first parameter - the options of the command. Git show config provides many different options to add to the functionalities of the command. Some of them are listed below:

We'll discuss the important Show options in the subsequent subheadings.

Git Show Pretty Format

The --pretty is an option provided by Show, it is used to style and customize the output.

Syntax

Using —pretty and —format differ mostly in two ways:

  • If none of the formatting settings are specified, --pretty will use the medium format by default. It is necessary to supply at least one parameter when using the --format.

  • You can construct your custom formats and enter custom text inside the Git configuration using --pretty. If you try to add custom text, the --format command will give an error.

Let's see an example of the pretty option. Let's create a custom pretty format that subdues the textual difference, shows the author's name in green, the author's email in blue, and the shorten commit hash.

Command

Output

USER

Git Show Diff Formats

The textual diff can be formatted using a wide range of settings provided by the Show command. Diff formatting offers the flexibility to make stylistic adjustments in addition to narrowing your search results to just show you the specific diffs you wish to see.

We can use the git show --color=never to get the output in one neutral color.

Command

Output

COLOR NEVER

We can get the Show data in raw format using the command git show --raw.

Command

Output

RAW

The list of commands Shows diff formatting is extensive. If you want to learn more about such commands, check out the official documentation.

Git Show Example

  1. The first step is to initialize Git in your folder using the command git init

GIT INIT

  1. Then you add files in your folder and commit those files in Git using the command git commit -m "commit message" after using git add ..

GIT ADD

  1. Make some changes in your file and create a new commit, using the same commit command.

  2. Now do a Git log to get information about your commits using the command git log.

GIT LOG

  1. Now use the git show <commit ID> command and paste any of the two commit IDs you get from the Git log.

HEADMAIN

We can see that we get a lot more information than we get in the Git log. I can see in green color the new line added, and in red color, we have the line that existed before this commit. In blue, we have the number of lines and words added.

Difference Between Git Log and Git Show

The commands git show and git log are pretty similar and share certain functionality. The variations exist because they were designed with slightly different objectives in mind. Git log's default behavior provides a more comprehensive snapshot than Git show.

For instance, the straightforward git log command shows a detailed log of every commit in your Git repository. On the other hand, git show just shows the most recent commit made to the repository and by default includes the textual diff.

Conclusion

That's all for this article folks, I hope you enjoyed reading it. In this article, we sailed through the concept of Git Show and its parameters. We learned about:

  • The git show command: Used to view the contents of Git objects in a Git repository.
  • Git Show Options: Various Git Show options like- online, pretty, format, s, etc.
  • Git Show Diff Formatting: Learned about Diff formatting in git show and different commands used to do so.

Now you can use the git show command in your next project when you want to get more information about your Git objects.