Add or Remove files to / from Git Staging area using ‘git add’ command

Before we start learning how to add files into Staging area in Git, It is required to understand what the Staging area means with respective to the Git – a distributed version control system.

Staging area in Git is something as in E-commerce website, You are using “Add to cart” which means you want that item but you are not sure which choice is better and you are either still looking for better product for the same item which you have added to cart or you are adding other products. So it’s an area where you are putting all things together which you would like to checkout or buy in future.

Same goes with Git, Where you can add all your items into Staging area and once you decide it’s all changes which you want to commit to local repository followed by pushing the same to remote repository.

For example, I have created a file named “file1.txt” and I would like to add this file to staging area.

git add "file1.txt"

Now, Suppose I have created multiple files “file1.txt”, “file2.txt” and “file3.txt”. Next question is how to add all these files,

git add file1.txt file2.txt file3.txt

Yes, as you can see in the example you need to mention all file names separating by space. Now, You have 100 files to add into staging area, So what you will do. for that git provides below command.

git add .

Yes, that’s it you just need to add period only. a single dot can add all your files into current directory to staging area. Now, What if I also want to add all files which are there into some folders inside current directory. Above command will only include files which are in current directory while it will exclude files which are in some folders in the current directory. You can try below command for the same.

git add --all

All these commands will only help when you want to add something to Git staging area. What if there are some files which you want to remove afterwards. Just like you add so many items to shopping cart and then remove some items just to get all the best items.

You can try below command for it:

git rm --cached file1.txt

in above command –cached command  tells that file1.txt is in staging area. Another option to remove file from staging area is:

git reset file1.txt

This git “reset” is totally opposite to git “add” command in Git.

 

Join Discussion

This site uses Akismet to reduce spam. Learn how your comment data is processed.