YK S
YK S

Reputation: 3440

Function of Git Staging area

I have gone through Git object model and read other articles about git but couldn't understand clearly about Git staging area.

What does staging area actually do?

Does it create the blob object and tree object when we do git add and when we do git commit the commit object is linked to the parent commit and corresponding tree object which is already linked to it’s blob/tree object?

Or

Does it just stores the info in index file (that something is changed and when we do git add it says ready for commit) and when we do git commit all the objects creation and linking happen between commit, tree, blob objects?

Upvotes: 1

Views: 492

Answers (1)

jthill
jthill

Reputation: 60255

"Staging area" is convenience jargon for the most common and popular way to use the index. Your first option,

Does it create the blob object and tree object when we do git add and when we do git commit the commit object is linked to the parent commit and corresponding tree object which is already linked to it’s blob/tree object?

has it almost exactly right: git add puts what you added in the repo, and git commit ties the commit object to the parent commit and the corresponding tree object - - - but that corresponding tree object isn't in the repo yet, git commit builds the tree (that ties pathnames to content) by consulting the index, which is exactly what it says it is: an index, tying pathnames to content.

So, git checkout updates the index to point to the content it checked out at each checked-out path, and git add updates the index to point to the content it added for each added path.

So you can use this as a "staging area", git commit only cares what you've added, not what's in your worktree. This is why git checkout, git reset, git commit and git add all have --patch options: there's what you checked out, what you've added, and what's in your worktree. At any point it might be useful to see differences between any of those, or "take back" part of the changes you made, maybe because they belong in another commit or just need further work.

Upvotes: 2

Related Questions