Adding and Removing files with Git

Checking the Staging Area

To see what the staging area is currently looking like, you can type the command git status. This shows you what files have been added to the staging area and which have not yet been added.

Adding to the Staging Area

Adding one file to the staging area

You can add individual files to the staging area by using the command git add.

Example

git add random-file.txt

The above command adds the text file called random-file to the staging area.

git add script.js

The above command adds the script to the staging area

Adding multiple files to the staging area

If you have done multiple changes to multiple files, it can be way more useful to add all of the files at once. This can be done very easily when you use a dot in the command: git add ..

Removing from the Staging Area

Removing from your own device

If you want to remove a file from the folder on your device, you can just delete it.

But, it will still exist in your staging area. You'll have to use the git add . command again to remove the files from your staging area.

Removing a file from the staging area, but keeping it in your folder

Maybe you accidentally added a file with confidential information to your staging area. You would want to keep this file in your local repository, but not to be committed. Using just git rm [filename] might not be enough, since it will give an error because you have changed to the file staged in your staging area.

Luckily, you can force remove a file by using git rm -f [filename]. This flag tells you it is a force.

But, this will also remove the folder the file was places in, you'll have to use a different command, which is git rm --cached [filename].