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.
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 commit them to your repository, creating a new 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.
Examples
Let's say you're working on a web application project. You have cloned the repository to your local machine, and your working tree now contains all the project files, such as HTML, CSS, JavaScript, and configuration files.
project-root/
├── .git/
├── index.html
├── styles.css
├── app.js
└── config.json
Here, the .git
folder is the actual local Git repository, and the rest of the files and directories inside the project-root
are your working tree. It's important to note that the .git
folder is not part of the working tree because it's the repository itself.
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 .
Hi! I'm Alex, creator of GitByBit.
And this is Gitopedia—a reference that summarizes knowledge in my Git course.
Gitopedia is designed to be a personal knowledge base. When you progress through the main Git course in VS Code, you unlock all of these topics, one by one, bit by bit. This gives you time to digest the information and gradually build the mental model of Git. Here's an example of how it works:
Learning directly in VS Code lets you practice Git exactly as you would in real life, doing real jobs, writing real code. In addition, the course has access to your actual terminal, so it can point out mistakes, suggest workarounds, etc.
The course is FREE, there are no Ads or other bullshit. There are optional premium add-ons you can purchase, mainly to support my work (regrettably, I have to eat every day), but that's totally up to you.