20775

What does the git staging area contain when the working directory is clean?

Question:

Does the staging area contain the snapshot of content of the last commit? Or something else?

<hr />

If git status shows:

On branch master nothing to commit, working directory clean

then if I issue git diff --cached

so HEAD is compared to what?

Answer1:

Whether or not the working directory is clean doesn't really have anything to do with what the staging area contains. The staging area contains stuff that's been staged for commit (e.g. with git add). Yes, any files that <em>haven't</em> been staged will be in the same state as they are on HEAD, unless you've done something weird. git diff --cached is comparing the contents of the staging area with HEAD (It can also be called as git diff --staged). Since git commit turns the contents of the staging area into a commit, git diff --staged shows you what you would commit if you committed now.

Since your git status output says "Nothing to commit", it's already telling you that there are no staged changes, and git diff --staged should report nothing.

Answer2:

<strong>Prerequisite reading</strong>: <a href="http://www.git-scm.com/book/en/v2/Git-Internals-Git-Objects" rel="nofollow">Read about Git objects, particularly trees and blobs, first</a>.

<strong>Short answer</strong>: the index always contains the IDs of the blobs of the files in HEAD, plus a bunch of flags to track changes to the files. It does <em>not</em> contain a snapshot of the last commit, nor is it a tree object (exception below).

<a href="https://git.kernel.org/cgit/git/git.git/tree/Documentation/technical/index-format.txt?id=HEAD" rel="nofollow">The really long answer can be read here</a>.

<strong>Slightly longer answer</strong>: The index (.git/index) always stores a list of the blob IDs and filepaths of all the files in HEAD, plus metadata about the files (permissions, modification times, owners, etc...).

The index can also contain pre-computed <a href="http://www.git-scm.com/book/en/v2/Git-Internals-Git-Objects" rel="nofollow">tree objects (how Git stores directories)</a> to speed up committing. It also stores information about conflicts.

So an "empty" index contains a list of all the filepaths, their blob IDs, meta information about the files, and space to store conflict information. Because it only stores the blob IDs (160 bits) the index avoids being redundant with HEAD. Index files for my projects are anywhere from less than 1K to 500K for large projects like Perl and Git.

You can poke around the index using <a href="https://libgit2.github.com/libgit2/#HEAD/group/index/git_index_read" rel="nofollow">libgit2</a> which has wrappers in many programming languages, for example <a href="https://metacpan.org/pod/Git::Raw" rel="nofollow">Git::Raw</a> in Perl.

Recommend

  • How can Chrome extensions basically cURL other pages?
  • what happens to finally block in the following cases?
  • Changing One Tag Name in an XML File Using XSLT
  • Python Floating Point Formatting
  • How to cache images only in disk using Kingfisher?
  • How to monitor transaction isolation level changes in SQL Profiler or in any other tool
  • Xcode 6 - Press button to play sound
  • Compact framework voice recognize API or library
  • Compare variables PHP
  • Why do native C++ projects have a TargetFrameworkVersion?
  • SharePoint Designer 2010 - Determine if today's date is within x days of a start date column us
  • rails - convert DateTime to UTC before saving to server
  • Vim ctags behaves strangely
  • AutoHotKey - how to send control and same key multiple times
  • Android libgdx prefs getting lost
  • How to remove left and right margins from all wrapped rows in flexbox (without nth-child or js)
  • undefined reference to `SOIL_load_OGL_texture'?
  • WP7 difficulties binding data to listbox itemssource - won't refresh
  • Ruby 1.8.6 Array#uniq not removing duplicate hashes
  • Configure Spring's MappingJacksonHttpMessageConverter
  • How Get arguments value using inline assembly in C without Glibc?
  • How to make R's read_csv2() recognise the text characters properly
  • AppleScript : find open tab in safari by name and open it
  • Implementation of State Monad
  • How do I pass the string value parameter of the selected list item from an auto-populated dropdown l
  • Deserializing XML into class C#
  • Updated Ionic CLI but shows previous version (Windows)
  • Function pointer “assignment from incompatible pointer type” only when using vararg ellipsis
  • Why winpcap requires both .lib and .dll to run?
  • jqPlot EnhancedLegendRenderer plugin does not toggle series for Pie charts
  • How do I rollback to a specific git commit
  • Is there a mandatory requirement to switch app.yaml?
  • Benchmarking RAM performance - UWP and C#
  • How to set the response of a form post action to a iframe source?
  • apache spark aggregate function using min value
  • python draw pie shapes with colour filled
  • Are Kotlin's Float, Int etc optimised to built-in types in the JVM? [duplicate]
  • unknown Exception android
  • Busy indicator not showing up in wpf window [duplicate]
  • How to Embed XSL into XML