Git Plumbing and Attributes

Follow Us

Our Communities

Module 9 – Git Plumbing and Attributes

Welcome to Module 9, where we’ll dive beneath the surface and explore the inner workings of Git. Up until now, we’ve been using high-level commands to interact with our repositories. However, Git also provides a host of lower-level commands, known as plumbing commands, that interact directly with the Git database. Understanding these commands can give you deeper insights into how Git works and provides you with more flexibility and control.

In addition to plumbing commands, we’ll also explore Git attributes. Git attributes are a powerful tool that lets you customize Git’s behavior on a per-file basis. This can be especially useful when working with different types of files in your repository, each requiring different handling.

In this module, you’ll learn about key plumbing commands, how to use them, and how Git attributes can be used to customize how Git treats your files. The goal of this module is to equip you with the understanding and skills to leverage these lower-level aspects of Git for more advanced use cases. Let’s dive in!

Understanding Git Plumbing Commands

Git is comprised of a set of high-level commands known as ‘porcelain’ commands that are user-friendly and interact with the repository at a high level, and a set of low-level commands known as ‘plumbing’ commands that provide a low-level interface to the Git database.

  • Porcelain vs. Plumbing Commands: Porcelain commands are designed to be the user-friendly layer of Git, providing an easy-to-understand interface for most common tasks, such as git add, git commit, git push, and others. Plumbing commands, on the other hand, work at a lower level and are concerned with the individual operations that take place under the hood when you run the porcelain commands.

  • Exploring Git Objects with Plumbing Commands: Let’s take a deeper look into the Git objects. Every entity in Git – a commit, tree (directory), or blob (file) – is an object. Each object has a unique SHA-1 hash that identifies it. You can use plumbing commands like git hash-object, git cat-file, and git ls-tree to interact directly with these objects.

  • Manipulating the Git Index with Plumbing Commands: The Git index is a binary file (generally kept in .git/index) containing a sorted list of path names, each with permissions and the SHA1 of a blob object; git update-index can be used to manipulate this. For instance, you can use git update-index --add file.txt to manually stage a file. git write-tree can be used to create a tree object from the current index.

Working with Git Attributes

Git attributes are a flexible way to customize the behavior of Git in how it handles certain files based on their attributes.

  • Understanding Git Attributes: Git attributes allow you to specify different behaviors for individual files that you can define in your project. This can be done by placing a .gitattributes file in your Git repository’s root directory. The .gitattributes file is a simple text file that gives attributes to pathnames.

  • Defining and Applying Git Attributes: To set an attribute, you need to pick a key for the attribute (e.g., text, binary, etc.), decide on the intended effect, and then write an attribute line in the .gitattributes file. This line uses the pattern to match the files and then assigns the attribute key to it.

  • Common Uses of Git Attributes: Git attributes can be utilized in several ways: managing line endings, specifying diff patterns, defining merge strategies, and setting up filter drivers for smudging and cleaning data, among others.

By the end of this module, you should have a greater understanding of the inner workings of Git, how plumbing commands interact directly with the Git system, and how you can leverage Git attributes to customize Git’s behavior for your project.

Exercises and Practice

Exercise 1: Using Plumbing Commands: Try creating a new blob object using the git hash-object command. Then, use the git cat-file command to inspect the contents of the newly created blob object. What do you observe?

Exercise 2: Manipulating the Git Index: Use the git update-index command to manually add a new file to the Git index. Then, create a new tree object using the git write-tree command. Can you find the new tree object using the git cat-file command?

Exercise 3: Working with Git Attributes: Create a .gitattributes file in your repository and define some custom behavior for certain file types. For instance, you could define that all text files should use LF (Line Feed) line endings. Verify that your settings work as expected.

Exercise 4: Custom Diffing with Git Attributes: Define a custom diff pattern for files with a specific extension in your .gitattributes file. Commit changes to a file that matches your pattern and view the diff. Is the output consistent with your custom diff pattern?


By performing these exercises, you should gain a more practical understanding of how Git’s plumbing commands and attributes work. Remember that these topics delve into the more complex aspects of Git, so don’t be discouraged if it feels challenging at first. Practice is key to mastering these concepts!

Repository Maintenance


Recovering from Mistakes