How to Recursively Add Files and Folders in Git
Sometimes, we encounter scenarios where we have to adjust some files, folders, and subfolders already present in Git. An accomplished portion of the nested folder system has to be added to Git remotely.
This article will discuss how to recursively add all the folders, subfolders, and files of our project to the staging area using commands. Finally, we will commit them to the repository.
Also, we will talk about some issues where we will have to recursively add all the subfolders and files of the main folder to the staging area and then finally commit them to the remote repository.
Recursively Add Files and Folders in Git
The command git add
can add files and folders in the working tree to the staging area. It also takes the pathname (extension) for the file or a directory.
The git add
command will recursively add all the files in that particular directory.
With the help of this command, we won’t be able to add ignored files in the working tree, as this option is used as default. If we execute the command git add
, it will give out a list of ignored files.
We’ll work with the shorthand -f (force)
option for adding these ignored files in our command.
The steps through which we can add files recursively are as follows.
Determine the Version of Git
The usage of the command git add
can differ depending on the current Git version that we are using on our computer.
For determining our current Git version, we will use the following Git command.
Syntax:
$ git --version
Add All Files Using the git add
Command
We will use the command git add
followed by the shorthand -A
to add all files and folders that we want.
Syntax:
git add -A
Instead of -A
, the option all
can also be used.
git add --all
The output of both will be the same. The untracked, modified, and deleted files will be added to the staging area of Git irrespective of the location we are accomplishing from this command.
For adding the files only from the current directory, we can use the git add
command followed by the shorthand .
, which indicates that we are by default on the top of our project folder ranking.
Syntax:
git add .
Run the git commit
Command
Recall that the command git add
doesn’t act on the repository. Changes will not be recorded and reflected until we run the git commit
command.
Syntax:
git commit -am "<commit message>"
Run the git push
Command
After committing, we will push these changes for the final result. It will push all the folders and files that we have added through the git add
to the project’s remote repository.
Syntax:
git push
Abdul is a software engineer with an architect background and a passion for full-stack web development with eight years of professional experience in analysis, design, development, implementation, performance tuning, and implementation of business applications.
LinkedIn