Key concepts

Working tree

The working tree (also known as the working directory or working copy) represents the files and directories of your project that you can edit, delete, and organize on your computer. The working tree is where you make changes to your files before you decide to commit those changes to your repository.

Example

Let's say you're working on a web application project. Your project is under version control with Git, and you see something like this in your file explorer:

project-root/
  ├── .git/
  ├── src/
  │   ├── components/
  │   │   ├── header.js
  │   │   └── footer.js
  │   ├── utils/
  │   │   └── helpers.js
  │   └── main.js
  ├── index.html
  ├── styles.css
  ├── app.js
  └── config.json

Q: Why is it called a working tree if it doesn't look like a tree at all?

A: Firstly, it's up side down. Secondly, it's a metaphor! The tree refers to the hierarchical structure of files and directories in your project. Just like a tree has branches and leaves, your project has directories (branches) and files (leaves).

The .git directory contains all the metadata and object database for the Git repository. It's hidden from the regular view of the project files because you're not meant to interact with it directly. However, when working in the terminal, you can often see this directory listed.

The rest of the files and directories (like src/, index.html, styles.css, etc.) make up your working tree. You can edit these files, add new ones, or delete existing ones as needed. These are the files that Git tracks for changes.

How it works

Git keeps track of the differences between the files in your working tree and the last commit on your current branch. This allows you to edit, add, and delete files freely in your working tree without immediately affecting the repository's commit history. Once you're satisfied with your changes, you can add them to the staging area (with a command like git add) and then create a new commit, preserving a snapshot in the project's history.

When you check out a branch or a specific commit, the files in your working tree are updated to match the state of the repository at that point. You can then make changes to these files, such as adding new features, fixing bugs, or making improvements to the codebase.

The working tree is important because:

  • It gives you a tangible, editable view of your project at its current state or at any branch you have checked out.
  • It allows you to work on your project's files and test your changes before deciding to create a new commit.
  • It enables you to see the effects of switching branches or pulling in changes from a remote repository immediately in your file system.

Related commands

Check the status of the working tree:

git status

Check the differences between the working tree and the last commit:

git diff

Discard changes in a file inside the working tree:

git restore path/to/file.txt

Add all changes in the current directory (.) to the staging area:

git add .
© 2024-2025 GitByBit.All rights reserved.