GIT ADD

Hemashree S

Git Add

  • The 'git add' command moves file changes to the staging area, updating the working directory's content and preparing it for the next commit. Every time files are added or modified in a project, they must be sent to the staging area using this command. 
  • The `git add` command is an essential part of Git, typically adding one file at a time, but it also offers options to add multiple files at once.
  • The "index" holds a snapshot of the working directory's data, which will be included in the next commit.
  • The 'git add' command can be executed multiple times before a commit, with all additions included in a single commit. It stages the files specified in the command line.
  • By default, the 'git add' command does not include the '.gitignore' file, which is used to specify files that should be ignored from being tracked.
Let’s learn how to add files in Git.

Git add files

The 'git add' command is simple and adds files to the staging area. You can stage either single or multiple files at once. The command is executed as follows:
$ git add <File name>
The above command stages the files in Git but does not yet share them in the version control system. A commit is required to share the changes. Let’s explore the scenario below.
We created a file for our new repository in the NewDirectory. To do this, use the `touch` command as shown below:
$ touch newfile.txt
Then, check if the file is untracked using the `git status` command, as shown below:
$ git status
The above command will list the untracked files in the repository. These files can be added to the repository. Since we've created a `newfile.txt`, use the command below to add it:
$ git add newfile.txt
Refer to the output shown below:

Git Add

From the output above, we can see that `newfile.txt` has been staged for our repository. Now, we need to commit it to share it on Git.

Git Add All

You can add multiple files in Git by running the 'add' command repeatedly, but Git offers a convenient option to add all files at once. To stage all files in the repository, use the 'git add -A 'command. Alternatively, you can use '.' to achieve the same result. The command will look like this:
$ git add -A
Or
$ git add .
The command above will add all the files in the repository. Let’s consider the following scenario:
You can either create four new files or copy them, and then add all of these files at once. 
Refer to the output below:
 
Git Add

In the output above, all the files are shown as untracked by Git. To track all of these files at once, use the command below:
$ git add -A
The command above will add all the files to the staging area. Note that the '-A' option is case-sensitive.
Refer to the output below:

Git Add

In the output above, all the files have been added, and their status is shown as staged.

Removing Files from the Staging Area

The 'git add' command can also be used to remove files from the staging area. If a file is deleted from the repository, it will appear as an untracked file. The 'add' command can then be used to remove it from the staging area. It might seem unusual, but Git can handle this. Consider the following scenario:
We have deleted 'newfile3.txt' from the repository. The repository's status after deleting the file is as follows:

Git Add

As shown in the output above, the deleted file remains in the staging area. To remove it from the index, use the command below:
$ git add newfile3.txt
Refer to the output below:

Git Add

The output above shows that the file has been 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 by using the ignore removal option. This can be done with the following command:
$ git add --ignore-removal .

Add all Modified and Deleted Files

Git's 'add' command offers various options, including one that stages only modified and deleted files, excluding newly created files. To stage all modified and deleted files only, use the command below:
$ git add -u

Add Files by Wildcard

Git allows us to add all files that match a specific pattern at once, providing another way to add multiple files together. For example, to add all Java or text files, you can use patterns like '.java' or '.txt'. To do this, run the command as follows:
$ git add *.java
The command above will stage all Java files. The same pattern can be applied to stage text files as well.
The next step after adding files is to commit them in order to share the changes on Git.

Git Undo Add

You can undo a 'git add' operation, though it's not done with the 'git add' command itself. Instead, you use the 'git reset' command to accomplish this.
To undo an 'add' operation, use the command below:
$ git reset<filename>









Tags
Our website uses cookies to enhance your experience. Learn More
Accept !

GocourseAI

close
send