Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does 'adding to the index' really mean in Git?

Tags:

git

I've got a question regarding Git basics.

Basically, what does the action known as "add to the index" mean in Git? I understand it like this:

If for any file git calculates SHA-1 sum then basically adding to index means that it calculates SHA-1 sum and add file to the staging area.

Am I correct?

like image 901
lapots Avatar asked Aug 17 '14 16:08

lapots


People also ask

What is git index used for?

The Git index is a staging area between the working directory and repository. It is used to build up a set of changes that you want to commit together. To better understand the Git index, then first understand the working directory and repository.

What does add to index mean in eclipse?

so you've made a project and you want it to commit for the first time ever, then you press add to index, so it takes the whole project and then you press commit.

What is staging area or index in git?

The book Pro Git says that the staging area is just a list, or index, that says which files will be committed when a git commit is done, and now the name index is more commonly known as the "staging area".


1 Answers

A useful metaphor

"Adding a file to the index", "staging a file", "adding a file to the staging area" are all synonymous.

I personally prefer the term staging area to index because it lends itself to a useful metaphor. If committing is akin to "taking a snapshot", staging is about "composing the shot".

Imagine yourself as a professional photographer about to take a a class picture: you gather all your subjects and get them ready for the photo, you make sure that they're all there and that there are no intruders, that everything of importance is in the frame, etc. Then... Snap!

enter image description here

Of course, if you realise, right after taking the picture, that too many kids had their eyes closed (or that some kid was giving the teacher bunny ears!), you may want to scrap that first picture and take another, better one; in Git, that would correspond to amending the last commit. But I digress...

What happens when you add a (new) file to the index

To stage something, you would usually use the high-level ("porcelain") git add command... or the exact equivalent git stage (introduced by Scott Chacon around Git v1.6) which I find much more intuitive, but doesn't seem nearly as popular.

When you add a new file to the staging area, three things happen:

  1. the file contents are hashed,
  2. the file contents are stored in your repository's database,
  3. the file contents in your working tree are registered to the .git/index file.

Adding a file to the index with plumbing commands

As an experiment, to fix ideas, you can use low-level ("plumbing") Git commands to reproduce what git add does in that simple case. Start from a brand new repository:

$ cd ~/Desktop
$ mkdir teststage
$ cd teststage
$ git init

Before doing anything else, go ahead and peer into the .git/objects folder.

$ ls -la .git/objects

You will see it only contains two (empty) subdirecties: info and pack. Create a file, say README.md:

$ printf "hello\n" > README.md

Now let's stage README.md, one step at a time. First, use the lower-level git hash-object command to (1) hash the contents of README.md and (2) write the latter to the repository's database.

$ git hash-object -w README.md
27728344ab3ae5b8aa334418d1e1b0f5be0ea0cc

(-w means write, here.)

Now, if you look into the .git/objects folder, you will see that the new object (a blob) has been added to the database:

$ tree -la .git/objects/
.git/objects
├── 27
│   └── 728344ab3ae5b8aa334418d1e1b0f5be0ea0cc
├── info
└── pack

There is one thing left to complete the staging of README.md. We need to (3) register the file contents to the index. Have a look inside .git, there should be no file called index in it, yet. Now, if you run

$ git update-index --add --info-only README.md

and then have another look inside .git, you'll see that a binary index file has been created.

That's it. You've staged README.md. It's ready to go in your next commit. Check it for yourself:

$ git status
On branch master

Initial commit

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

    new file:   README.md

Now you can make your first commit, if you want.

like image 62
jub0bs Avatar answered Oct 16 '22 08:10

jub0bs