What's the difference between HEAD, working tree and index, in Git?

What's the difference between HEAD, working tree and index, in Git?

 

 

Git Gud: The Working Tree, Staging Area, and Local Repo

 

 

When I first started learning git, I honestly felt like I was learning basic commands with little reason. I was learning what to do without learning why I was doing it and or what was actually happening in the grand scheme of things. Yes, I was saving data and changes to my local code so that others could see my revisions and work on the same files at the same time. But what was really happening and why was it happening? It felt as if I was learning how to perform calculus微积分 without understanding why I was doing the calculations. I was mindlessly repeating the same actions without realizing what was happening or why I was doing it hoping that everything would work out well for me. Most of the times I was lucky, and I was able to contribute to teams and code without a hitch. Other times, not so much.

So with this article, I wanted to give you the information that helped me understand what git is, what it can do, and the commands that I find to be the most helpful when working in a git repository. Keep in mind this article is meant to be very brief. It is not my intention to go very deep into the world of git.

Let’s begin!

 

Now lets dive a little deeper into the git workflow.

There are three core areas to git. These are the Working Tree, the Staging Area (also known as Index), and the Local Repository. When working in a git repository files and modifications will travel from the Working Tree to the Staging Area and finish at the Local Repository. Thus we will talk about these three areas in that order.

 

The Working Tree

The Working Tree is the area where you are currently working. It is where your files live. This area is also known as the “untracked” area of git. Any changes to files will be marked and seen in the Working Tree. Here if you make changes and do not explicitly save them to git, you will lose the changes made to your files. This loss of changes occurs because git is not aware of the files or changes in the Working Tree until you tell it to pay attention to them. If you make changes to files in your working tree git will recognize that they are modified, but until you tell git “Hey pay attention to these files,” it won’t save anything that goes on in them.

How can you see what is in your Working Tree? Run the command git status. This command will show you two things: The files in your Working Tree and the files in your Staging Area. It will look something like the image below if you don’t have anything in your Staging Area.

 

 Image of the Working Tree

The Staging Area (Index):

The Staging Area is when git starts tracking and saving changes that occur in files. These saved changes reflect in the .git directory. That is about it when it comes to the Staging Area. You tell git that I want to track these specific files, then git says okay and moves them from you Working Tree to the Staging Area and says “Cool, I know about this file in its entirety.” However, if you make any more additional changes after adding a file to the Staging Area, git will not know about those specific changes until you tell it to see them. You explicitly have to tell git to notice the edits in your files.

How can you see what is in your Staging Area? Run the command git status like before. It will look something like the image below.

 

 Image showing the Working Tree (red) and Staging Area (green)

How do you add files to your Staging Area? Running the command git add #filename# will add a specific file to the Staging Area from your Working Tree. If you want to add everything from the Working Tree, then run the command git add . The . operator is a wildcard meaning all files.

 

The Local Repository:

The Local Repository is everything in your .git directory. Mainly what you will see in your Local Repository are all of your checkpoints or commits. It is the area that saves everything (so don’t delete it). That’s it.

How do you add items from your Staging Area to your Local Repository? The git command git commit takes all changes in the Staging Area, wraps them together and puts them in your Local Repository. A commit is simply a checkpoint telling git to track all changes that have occurred up to this point using our last commit as a comparison. After committing, your Staging Area will be empty.

 

posted @ 2018-09-20 15:49  ChuckLu  阅读(351)  评论(0编辑  收藏  举报