TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Git Add

Git Add with Git Tutorial, Git Introduction, Git, What is Git, GitHub, What is GitHub, Git vs GitHub, Git Mercurial, Installation of Git for Windows, Installation Git for Ubuntu, Git Environment Setup, Git Command Line Tools, Git Tools, etc.

<< Back to GIT

Git Add

The 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 files

Git 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:

$ 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:

Git Add

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 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:

$ 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:

Git Add

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:

$ 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:

Git Add

In the above output, all the files have been added. The status of all files is displaying as staged.

Removing Files from the Staging Area

The 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:

Git Add

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:

Git Add

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 for the text files.

The next step after adding files is committing to share it on Git.

Git Undo Add

We 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:

$ git reset <filename>

To learn more about git reset command, visit Git Reset.


Next TopicGit Commit




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf