The git adds command is used to add file contents to the Index (Staging Area). This command updates the current content of the working tree to the staging area. It also prepares the staged content for the next commit. Every time we add or update any file in our project, it is required to forward updates to the staging area.
The git adds command is a core part of Git technology. It typically adds one file at a time, but there some options are available that can add more than one file at once.
The “index” contains a snapshot of the working tree data. This snapshot will be forwarded for the next commit.
The git adds command can be run many times before making a commitment. These all add operations can be put under one commit. The add command adds the files that are specified on the command line.
The git adds command does not add the .gitignore file by default. In fact, we can ignore the files by this command.
Let’s understand how to add files on Git?
Git add files
Git add command is a straightforward command. It adds files to the staging area. We can add single or multiple files at once in the staging area. It will be run as:
$ git add <File name>
The above command is added to the git staging area, but yet it cannot be shared on the version control system. A commit operation is needed to share it. Let’s understand the below scenario.
We have created a file for our newly created repository in NewDirectory. To create a file, use the touch command as follows:
$ touch newfile.txt
And check the status whether it is untracked or not by git status command as follows:
$ git status
The above command will display the untracked files from the repository. These files can be added to our repository. As we know we have created a newfile.txt, so to add this file, run the below command:
$ git add newfile.txt
Consider the below output:
From the above output, we can see newfile.txt has been added to our repository. Now, we have to commit it to share on Git.
Git Add All
We can add more than one file in Git, but we have to run the add command repeatedly. Git facilitates us with a unique option of the add command by which we can add all the available files at once. To add all the files from the repository, run the add command with the -A option. We can use ‘.’ Instead of -A option. This command will stage all the files at a time. It will run as follows:
$ git add -A
Or
$ git add .
The above command will add all the files available in the repository. Consider the below scenario:
We can either create four new files, or we can copy it, and then we add all these files at once. Consider the below output:
In the above output, all the files are displayed as untracked files by Git. To track all of these files at once, run the below command:
$ git add -A
The above command will add all the files to the staging area. Remember, the -A option is case-sensitive. Consider the below output:
In the above output, all the files have been added. The status of all files is displayed as staged.
Removing Files from the Staging Area
The git adds command is also used to remove files from the staging area. If we delete a file from the repository, then it is available to our repository as an untracked file. The add command is used to remove it from the staging area. It sounds strange, but Git can do it. Consider the below scenario:
We have deleted the newfile3.txt from the repository. The status of the repository after deleting the file is as follows:
As we can see from the above output, the deleted file is still available in the staging area. To remove it from the index, run the below command as follows:
$ git add newfile3.txt
Consider the below output:
From the above output, we can see that the file is removed from the staging area.
Add all New and Updated Files Only:
Git allows us to stage only updated and newly created files at once. We will use the ignore removal option to do so. It will be used as follows:
$ git add --ignore-removal .
Add all Modified and Deleted Files
Git add facilitates us with a variety of options. There is another option that is available in Git, which allows us to stage only the modified and deleted files. It will not stage the newly created file. To stage all modified and deleted files only, run the below command:
$ git add -u
Add Files by Wildcard
Git allows us to add all the same pattern files at once. It is another way to add multiple files together. Suppose I want to add all java files or text files, then we can use pattern .java or .txt. To do so, we will run the command as follows:
$ git add *.java
The above command will stage all the Java files. The same pattern will be applied to the text files.
The next step after adding files is committing to share them on Git.
Git Undo Add
We can undo a git add operation. However, it is not a part of the git add command, but we can do it through the git reset command.
To undo an add operation, run the below command:
$ git reset <filename>
Next Topic: Click Here
Great, thanks for sharing this blog.Thanks Again. Want more.