Git

How to Add Multiple Files in Git

Sometimes Git users face multiple troubles while working on projects; either they want to add untracked files to Git local repositories or make changes to their existing files. In such scenarios, you must precisely add the required file to the repository. However, it becomes a little bit difficult if you have multiple files. To handle these situations, Git provided an efficient solution: the “$ git add” command.

In this article, we will discuss the method of adding multiple files in Git and making changes to them.

How to Add Multiple Files in Git?

Git allows users to add single or multiple files to the repository. For this corresponding purpose, follow up on the below-mentioned instructions.

Step 1: Open Git Bash
Search “Git Bash” using the “Startup” menu and open it on your system:

Step 2: Change Directory
First, move to the specific directory where the untracked files are present:

$ cd "C:\Users\nazma\Linuxhint"

Here, “Linuxhint” is our targeted repository:

Step 3: List Files
Now, execute the “ls” command to check the list of files:

$ ls

Below output indicates that, our “Linuxhint” repository contains three files each of which have a different extension such as, “.txt”, “.html”, and “.php”:

Step 4: Check Current Status
Run the below-provided command to check the Git repository current status:

$ git status

According to our current status, we have two untracked files named “myfile2.html”, and “myfile3.php”:

Step 5: Add Files
To add untracked multiple files simultaneously in the Git repository, use the provided command:

$ git add myfile2.html myfile3.php

The mentioned files are added into the staging area:

Step 6: Check Status
Again, check the repository status by executing the below command:

$ git status

As you can see in the output, our “myfile2.html”, and “myfile3.php” files are successfully added and these changes need to committed:

Now, check out the below section for editing into the added files.

Can we Make Changes to Added Files Using Git Bash?

Yes, we can make changes to the added files using Git Bash.

How to Edit Added Files in Git?

Follow the below instructions to edit the added files in Git.

Step 1: Open Selected File
First, open the file in which you need to make changes using the “start” command:

$ start myfile2.html

For instance, we want to edit “myfile2.html”:

The specified file will be opened in the default text editor configured at the time of Git bash installation. In our case, we have selected “Notepad”. That’s why our “myfile2.txt” is opened in Notepad:

Step 2: Edit File
Edit the opened file according to your requirements. Here, we have added the “<h2>Welcome to the Linuxhint World!<\h2>” heading in our “myfile2.html” file:

Step 3: Save Changes
Click on the “File” option from the menu bar, hit “Save” and click on the “X” icon:

Step 4: Launch File
Open the second added file with the help of the “start” command:

$ start myfile3.php

Step 5: Edit File

Now, we will edit the other file:

Step 6: Save File

Save the added changes with the same mentioned method:

Step 7: Check Repository Status

Lastly, check the repository status for verifying the changes:

$ git status

The given output indicates that to verify “file2.html”, and “myfile3.php” files are modified successfully:

Other Options of git add Command

Git also provided multiple other options for adding files with “git add”, some of them are listed below:

Options Description
add *.exe Used to add all files having “.exe” extension.
add -A Used to stage all changes.
add . Used to stage new files and changes without deletions.
add <path>/. Used to add file contents to be indexed for commit.
add -u Use to stage only updated and deleted files.

That’s all! We have discussed the procedure of adding and editing multiple files in Git.

Conclusion

To add multiple files in Git, first, navigate to the directory where the untracked files are present and execute the “$ git add” command with the required files name. Then, use the “$ start” command to open added files one by one, make changes and save them. After that, execute the “$ gits status” command again to verify the changes. This article illustrated the method of adding multiple files in Git and making changes to them.

About the author

Maria Naz

I hold a master's degree in computer science. I am passionate about my work, exploring new technologies, learning programming languages, and I love to share my knowledge with the world.