Preparing Files for Commit in Git
Watching files is only half the battle. We also need to tell Git at what point to remember the state of a file for a later commit. This is also done with the versatile add command. We'll discuss this subtlety in more detail in a moment.
Let's say we created a file index.html. Initially it will be untracked:
Untracked files:
index.html
Let's make it trackable:
git add index.html
Now it gets put into the commit staged list as a new file:
Changes to be committed:
new file: index.html
Now let's change the text of the file. In this case, the file will be in the section not prepared for commit, but already as modified:
Changes not staged for commit:
modified: index.html
Moreover, this file will be in two sections at once:
Changes to be committed:
new file: index.html
Changes not staged for commit:
modified: index.html
How so? The thing is that Git saves (indexes) the current state of the file. The state is the current text of the file, as well as whether it was created or deleted. If the file is changed, its changes will not be saved (not indexed) in Git. To do this, you need to run the add command again. In fact, this command should be considered as adding the current state of the file to the new commit.
This is why we see our file in several lists - these are different versions of the file.
Let's make Git index the current state of our file index.html, which we're going to make changes to. Let's run the add command on it:
git add index.html
After this, the current version will be ready for commit and will be placed in the appropriate section:
Changes to be committed:
modified: index.html
And you have to do this every time. When you make changes to a file, you have to stage them for the next commit with the add command. Of course, if you want to pick up these changes in the next commit.
Create a new file. Index your file. View the status.
Make a change to your file. View the status. Index your file. View the status.
Make a change to your file again. Check the status. Index your file. Check the status.