标签:
File States Overview
Every file in your working directory is mainly in one of 2 states:
1. Tracked : All files that were committed to the last snapshot in Git repository are in state Tracked. They can be one of the following states:
1.1 Unmodified : Files you checkout from repository and haven‘t edited anything yet.
1.2 modified : Files you checkout from repository and have ever edited something since your last commit.
1.3 staged : Files you‘ve modified and added to staging area.
2. Untracked : All files that were not in the last snapshot and are not in the staging area.
Please take a look at the states diagram below
figure 7.1
There are couples of transfer operations that make files from one state to another. Today we‘re going to explain what that mean to each of them and how to do to make that happen.
Suppose this is my working directory as below screen shown. I have 3 files which are all in my last snapshot.
figure 7.2
Untracked
First of all, let me create a new file in my working directory..
echo ‘This is a html project‘ > README git status
Let‘s take file README as an example. The output is displayed as below
figure 7.3
File README is now in state untracked. See it is understand the list of ‘Untracked files:‘.
Untracked -> Staged
See the figure 7.1, as the it says we add the file README to staging area.
git add README
git status
That time after running the commands above, you can see the file README now is on staging area waiting for commit.
figure 7.4
Staged -> Unmodified
Obviously it is time to commit the file changes on stage
git commit -m "add file README"
The state of file README now is changed from staged to unmodified.
figure 7.5
Unmodified -> Untracked
As the figure 7.1 tells us, the change happens by ‘Remove the file‘. If the file is removed, is gone, I think it doesn‘t make any sense. No file no state, what do you mean to Untracked? So one hand we must remove the file from the last snapshot, have Git no track for it. On the other hand we want to keep that file on your working directory, on your hard drive.
That‘s the reason why the option --cached is used.
git rm --cached README
figure 7.6
You can see the file README is still in working directory (command ls displays this) and to be under the list of Untracked files.
Unmodified -> Modified
The common thing between these 2 states is that files were in your last snapshot. The different thing between them is that, if you edit files since your last commit Git sees them as modified, otherwise as unmodified. So the transfer from unmodified to modified is to change your files.
Let‘s suppose that file README is committed to my local repo and now I‘m adding one more line text appending to it.
git ls-files echo ‘hello world\r\n‘ >> README git status
Look at the output
figure 7.7
Modified -> Staged
That‘s similar to the state change "Untracked -> Staged".
git add README
The output also look the same as figure 7.4
Conclusion
untracked -> staged: git add <file>
staged -> unmodified: git commit
unmodified -> untracked: git rm --cached
unmodified -> modified: (You can do any change to files which Git is tracking.)
modified -> staged: git add <file>
标签:
原文地址:http://www.cnblogs.com/vjianwang/p/gittutorial7.html