28

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?

jub0bs
  • 60,866
  • 25
  • 183
  • 186
lapots
  • 12,553
  • 32
  • 121
  • 242

2 Answers2

60

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.

Community
  • 1
  • 1
jub0bs
  • 60,866
  • 25
  • 183
  • 186
  • 3
    Nice explanation on how git really works its magic! I wish the git handbook was written as well as this post. – 9301293 Feb 07 '18 at 01:13
0

When you add a file, it's marking that as a file that you will commit once you run the git commit command. A shortcut to add all modified files automatically is to commit with git commit -a. Another shortcut if you are adding multiple new files at the same time is to run git add -A.

Acey
  • 8,048
  • 4
  • 30
  • 46
  • 4
    This doesn't really answer the question that was asked. – John Kugelman Aug 17 '14 at 18:37
  • The question regarded git basics, in the authors words. I interpreted the question as 'what does 'adding' do' and I gave its usage in a couple of scenarios. The other answer is far far far from git basics. Also, I answered the question provided in the title which has since been modified by an editor. – Acey Aug 17 '14 at 18:50
  • @Acey You're right. Perhaps the new title (I changed it) is too far from the original meaning. How about "What does 'adding to the index' really mean in Git?"? – jub0bs Aug 17 '14 at 18:56
  • Yes I think that is more true to the intent – Acey Aug 17 '14 at 18:58