Ryan has been working with web technologies since 2004 and is a lover all all things design, especially jQuery and HTML. Big fan of open source. Preferred Languages - Javascript/Java, Preferred Framework-jQuery, Preferred Design Tools - GIMP/Inkscape. Ryan has posted 40 posts at DZone. You can read more from them at their website. View Full User Profile

Git : Status Of Working And Staged Files

12.19.2012
| 1897 views |
  • submit to reddit

 I just came to know about this very neat feature of the git status command.

Basically, the status command gives you the the names of the files which you are tracking but have certain changes in them relative to your last commit. Now, Git has 3 places in which it keeps the code - the commit area, the stage area/index and the working directory. Lets take a few scenarios to explain the usefulness of this command


Scenario 1

Lets say that you have a file called Sample_File.txt in the working directory to which you made a few changes. But you have not staged it yet. So, the state of the file in the three repositories can be described as  (latest commit == staged)  != (working). To know which files have been modified in the working directory, in a very shorthand form, use git status with the -s option. Here's what I got when I ran it on my system.



Notice that the M is in Red, and there is a little bit space to its left, which means, that the file is modified in your working directory. (The extra space is explained in scenario 2) The status code M stands for 'Modified'. There are many other one letter status codes that you can take a peek at over here.

Scenario 2

Lets say that you have a file in your working directory, that has some changes but you staged it after making these changes. In this case the state of the file in the three repositories can be described as (latest commit)! = (staged  == working ) Now if you run git status -s, this is what you get


Notice that the M is in Green, but this time there no space to its left, which means, that the file is in a modified state in your staged directory. You can however notice that there is some extra space between the M and the file name this time. (Will be clarified in Scenario 3)

Scenario 3

Lets say that your made changes to your file, staged it and then edited it again. 
In this case the state of the file in the three repositories can be described as  (latest commit ! = staged  != working )


Now you can see clearly that you have 2 M's. One in red indicating the state of your file in the staged area and one in green indicating the state of your file in the working directory.
Its quite common that throughout your workflow, you would find yourself moving between the above scenarios sequentially. (Scenario 1 -> Scenario 2 -> Scenario 3) . The above set of commands should be able to help you keep track of what files were changed where.

References

The git manual reference
Published at DZone with permission of its author, Ryan Sukale. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Ramesh Patel replied on Wed, 2012/12/19 - 5:58am

Its really good one.

http://javakeexample.blogspot.in/


Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.