Recursively Adding Files To Git Repository

Adding a file to Git Repository recursively. first, we need to create a deep nesting folder structure within a Git repository.

We know that Git adds all the files and directory to the staging area before doing commit, but what if you have subdirectory or files..?, the answer is yes, Git will add all the subdirectory or files to the staging area, we call this process as adding files recursively to Git Repository.

The Git Add command takes a path name for either a file or a directory. if it is a directory then the add command adds all the files in that directory recursively.

Now Let us understand this process through a demo as shown below:

  • First thing you need to do is, open a Git bash and move to your home directory, pass pwd command to make sure that you are on your project folder.

Do an lsyou can see there is starter-web-1/ folder, so this is our git repository. Now cd into it, it will be directed to a starter-web-1 folder and now give git status, Git returns a message that it's on the master branch and your working directory is clean.

Now create a deep nesting folder structure by using mkdir command with -p parameter as
mkdir -p level1/level2/level3/level5 , Thus do an ls you can see your first level folder directory.

  • Go to that level1 directory and do pwd to see your path.
  • Next, create .txt file over here using notepad++ the default editor page will open, copy some dummy text from and paste it here then save and exit.
  • If you do ls you can see level1-file.txt and level2 directory.
  • Do cd level2 directory and pwd.
  • let us create a .txt file inside this, us
    notepad++ level2-file.txt, copy some dummy text and paste it into the editor page, save and exist.
  • Now, again do ls, you can see level2-file.txt and level3 directory.move to the level3 directory
    cd level3 and pwd.
  • Repeat the same process as above, create a .txt file notepad++ level3-file.txt and add some dummy text into that and save then exit.
  • Do ls you will see level3-file.txt and level5 directory.
  • Now let us delete or remove leve5 directory by using rm -rf level5.

Now go back to your root directory by using command cd ../../.. . you returned to your starter-web-1/ folder and do an ls, you will see a level1 folder in that. Do,git status you can see only a first level directory as the untracked file.

However, am not able to see all the files recursively but I can add the files recursively to Git repository.

So to do that am going to use parameter git add . It will add all the files recursively from this point forward. If I do git status again it will show you all the new files as shown in the below image.

  • Now you can commit changes by using a default text editor, git commit press enter, editor page will occur here, enter your commit message adding several files recursively and save then exit.
  • Thus the commit command returns each of those files recursively have been added to the Git repository.

Editing files

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions