Using Git Show to View Contents of Files

Overview

In this tutorial, you will be shown how to view different versions of files in your Git repository. You will see how to output a clean, full index of a file stored in your repository using the Git Show command.

Have you ever viewed multiple versions of a file stored in a public Git repository system, such a Github, Bitbucket, or Gitlab? Viewing a clean, full index of a file’s previous version is useful for auditing code, peering back at a previous state to understand how it worked in the past.

While it’s nice being able to flip between versions of a file in a GUI, sometimes we need the same functionality from the command-line. We can accomplish the same using the git show command.

Git Show

The Git Show command allows us to view files as they existed in a previous state.

Output a file’s contents from a previous version of a file

git show <version>:<file>

The version can be a commit ID, tag, or even a branch name. The file must be the path to a file. For example, the following would output a contents of a file named internal/example/module.go file from a tagged commit called “release-23”.

git show release-23:internal/example/module.go

The only output would be the file’s contents. No metadata from Git or anything else to clutter to output, giving you a clean copy of the file’s previous state.

package example

type Module struct {
    name string
}

func (m *Module) SetName(name string) {
    m.name = name
}

func (m *Module) GetName() string {
    return m.name
}