git add
In the previous lesson, we used the following command to track the state of our project:
git status
>> On branch master
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
project-1.txt
git warned us that our project project-1.txt
is not being tracked, and gave us a hint to use git add
to initiate the tracking process. Let's do just that:
git add project-1.txt
Nothing seems to have happened. Let's try printing the state of our project again:
git status
>> On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: project-1.txt
The message seems to have changed. Notice how git is showing us that the changed that need to be committed are in the new file: project-1.txt. The file changes are now in the staging area and are ready to be committed.
Staging multiple files
let's create two empty files:
touch file-2.txt
touch file-3.txt
ls
>> file-2.txt file-3.txt project-1.txt
Then let's check the status of our project:
git status
>> On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: project-1.txt
Untracked files:
(use "git add <file>..." to include in what will be committed)
file-2.txt
file-3.txt
Based on the output above, git is not tracking file-2.txt and file-3.txt.
let's stage them, now we could use the following command:
git add file-2.txt file-2.txt
but that's a lot of writing, and can become annoying once you have a lot of changed files. We could use the prefix .
to select all of the files at once:
git add .
git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: file-2.txt
new file: file-3.txt
new file: project-1.txt
all of our files have been successfully staged.
Removing a file from the staging area
Let's assume that one of our files is not ready to be staged. We could remove that file from the staging area using the little helper git has provided us:
(use "git rm --cached <file>..." to unstage)
git rm --cached file-2.txt
git status
>> On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: file-3.txt
new file: project-1.txt
Untracked files:
(use "git add <file>..." to include in what will be committed)
file-2.txt
The file file-2.txt has successfully been removed from the staging area.
git rm --cached
does not behave like your typicalrm
, it won't destroy your files. It will only remove them from the staging area.