Git AddThe git add 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 add 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 add command can be run many times before making a commit. These all add operations can be put under one commit. The add command adds the files that are specified on command line. The git add 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 filesGit add command is a straight forward 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: 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: And check the status whether it is untracked or not by git status command as follows: 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: 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 AllWe can add more than one files 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 -A option. We can use '.' Instead of -A option. This command will stage all the files at a time. It will run as follows: Or 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 displaying as untracked files by Git. To track all of these files at once, run the below command: 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 displaying as staged. Removing Files from the Staging AreaThe git add 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: 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: Add all Modified and Deleted FilesGit 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: Add Files by WildcardGit 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: The above command will stage all the Java files. The same pattern will be applied for the text files. The next step after adding files is committing to share it on Git. Git Undo AddWe can undo a git add operation. However, it is not a part of git add command, but we can do it through git reset command. To undo an add operation, run the below command: To learn more about git reset command, visit Git Reset. Next TopicGit Commit |