How to use the command 'git show' (with examples)

How to use the command 'git show' (with examples)

Git is a distributed version control system that allows developers to track changes to their codebase efficiently. The git show command is used to display various types of Git objects, such as commits, tags, and more. It provides detailed information about the specified object, including the commit hash, message, changes, and other metadata. In this article, we will explore several use cases of the git show command, along with examples and explanations for each command argument.

Use case 1: Show information about the latest commit

Code:

git show

Motivation:

The motivation for using this example is to quickly view the details of the latest commit in a repository. It can be useful for reviewing recent changes, understanding the commit message, and inspecting the modifications made.

Explanation:

  • git show: Executes the git show command to display information about the latest commit in the current branch.

Example output:

commit 12ab34cd56789ef0123456789abcdef12345678
Author: John Doe <johndoe@example.com>
Date:   Mon Jan 1 12:00:00 2022 -0500

    Update README.md

diff --git a/README.md b/README.md
index abcdef123..4567890ab 100644
--- a/README.md
+++ b/README.md
@@ -1,2 +1,3 @@
 # Sample Repository
-This is a sample repository.
+This is a sample repository created for demonstration purposes.

Use case 2: Show information about a given commit

Code:

git show <commit>

Motivation:

The motivation for using this example is to examine the details of a specific commit. It can be useful for understanding the changes made in a particular commit, reviewing the commit message, and inspecting the modifications made.

Explanation:

  • git show <commit>: Executes the git show command to display information about the specified commit.

Example output:

commit 123456789abcdef0123456789abcdef01234567
Author: Jane Smith <janesmith@example.com>
Date:   Fri Jan 1 12:00:00 2022 -0500

    Add new feature

diff --git a/src/main.js b/src/main.js
index 1234567ab..7890abcd 100644
--- a/src/main.js
+++ b/src/main.js
@@ -10,7 +10,7 @@ const multiply = (a, b) => {
 };

 const result = multiply(2, 3);
-console.log(result);
+console.log("Result:", result);

Use case 3: Show information about the commit associated with a given tag

Code:

git show <tag>

Motivation:

The motivation for using this example is to retrieve information about the commit associated with a specific tag. It can be useful for understanding the changes made in a tagged version, reviewing the commit message, and inspecting the modifications made.

Explanation:

  • git show <tag>: Executes the git show command to display information about the commit associated with the specified tag.

Example output:

commit 987654321fdecba987654321fdecba12345678
Author: John Doe <johndoe@example.com>
Date:   Thu Jan 1 12:00:00 2022 -0500

    Release v1.0.0

diff --git a/package.json b/package.json
index 123456789..abcdef012 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
 {
   "name": "my-app",
-  "version": "0.1.0",
+  "version": "1.0.0",
   "dependencies": {
     "react": "^16.8.0",
     "react-dom": "^16.8.0"

Use case 4: Show information about the 3rd commit from the HEAD of a branch

Code:

git show <branch>~3

Motivation:

The motivation for using this example is to view information about the 3rd commit from the HEAD of a specific branch. It can be useful for reviewing the details of historical commits and understanding the changes made in the project’s history.

Explanation:

  • git show <branch>~3: Executes the git show command to display information about the 3rd commit before the HEAD of the specified branch.

Example output:

commit 0123456789abcdefabcdef0123456789abcdef01
Author: John Doe <johndoe@example.com>
Date:   Mon Jan 1 12:00:00 2022 -0500

    Update config file

diff --git a/config.json b/config.json
index abcdef012..3456789ab 100644
--- a/config.json
+++ b/config.json
@@ -5,8 +5,8 @@
   "port": 8080,
   "database": {
     "host": "localhost",
-    "port": 27017,
-    "name": "mydb"
+    "port": 5432,
+    "name": "postgres"
   }
 }

Use case 5: Show a commit’s message in a single line, suppressing the diff output

Code:

git show --oneline -s <commit>

Motivation:

The motivation for using this example is to obtain a concise summary of a commit’s message without showing the detailed diff output. It can be useful for quickly browsing through the commit history and identifying the purpose of each commit.

Explanation:

  • git show --oneline -s <commit>: Executes the git show command with the --oneline and -s options to display a single-line summary of the specified commit’s message without including the detailed diff output.

Example output:

987654321 Release v1.0.0

Use case 6: Show only statistics about the changed files

Code:

git show --stat <commit>

Motivation:

The motivation for using this example is to obtain statistical information about the modifications made in a specific commit. It provides a summary of the added and removed lines per file, indicating the scope and impact of the changes.

Explanation:

  • git show --stat <commit>: Executes the git show command with the --stat option to display statistics (added/removed lines) about the changed files in the specified commit.

Example output:

src/main.js    | 4 ++--
src/utils.js   | 2 +-
2 files changed, 3 insertions(+), 3 deletions(-)

Use case 7: Show only the list of added, renamed, or deleted files

Code:

git show --summary <commit>

Motivation:

The motivation for using this example is to retrieve a concise list of the files that were added, renamed, or deleted in a specific commit. It can be helpful for quickly identifying the scope of file modifications within a commit.

Explanation:

  • git show --summary <commit>: Executes the git show command with the --summary option to display only the list of added, renamed, or deleted files in the specified commit.

Example output:

create mode 100644 src/index.html
rename src/main.js => src/app.js (89%)
delete mode 100644 src/utils.js

Use case 8: Show the contents of a file as it was at a given revision

Code:

git show <revision>:<path/to/file>

Motivation:

The motivation for using this example is to view the contents of a specific file as it existed in a particular revision (e.g., branch, tag, or commit). It can be useful for inspecting the historical state of a file and comparing it with the current version.

Explanation:

  • git show <revision>:<path/to/file>: Executes the git show command to display the contents of the specified file at the specified revision.

Example output:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed condimentum pretium nisl ac finibus. Aliquam posuere mi nec massa placerat vestibulum. In ac euismod mauris. Aenean semper placerat dictum. Sed malesuada mattis lacus et rhoncus. Nulla facilisi. Proin eu vehicula leo. Quisque consequat sapien sed nulla condimentum, at ullamcorper magna rutrum. Quisque eu nulla elementum, pellentesque purus ut, mattis metus.

Conclusion:

The git show command is a powerful tool for inspecting and analyzing Git objects, such as commits, tags, and more. By understanding the various use cases and command arguments, you can effectively retrieve information about the commit history, review changes, and compare file revisions. Whether you need to examine the latest commit, explore a specific commit’s details, or view file contents at a given revision, the git show command provides the necessary functionality for a comprehensive Git workflow.

Related Posts

Using grim (with examples)

Using grim (with examples)

Screenshot all outputs Code: grim Motivation: This command allows you to capture screenshots of all connected outputs.

Read More
How to use the command 'systemd-ask-password' (with examples)

How to use the command 'systemd-ask-password' (with examples)

This article will explain how to use the ‘systemd-ask-password’ command with various use cases.

Read More
How to use the command 'systemd-analyze' (with examples)

How to use the command 'systemd-analyze' (with examples)

Systemd is a system manager and init system for Linux operating systems.

Read More