Git add all files in folder-Recursively add the entire folder to a repository

3.4K    Asked by Yashraj in Salesforce , Asked on Jul 6, 2021

I am trying to add a branch to the master branch on GitHub and push a folder onto that branch.

The folder structure of the branch looks like - SocialApp/SourceCode/DevTrunk/SocialApp and all the source code files are in the last folder.

I am using the following Git commands:

git add *
git commit -m with the message
git push

This is pushing only the first folder "SocialApp" onto GitHub and ignoring the folder SourceCode that is inside the folder. How git add recursive?

Answered by debbie Jha

For this case:

Go to .gitignore file and check whether the subdirectory is ignored or not ignored.

And then you could do:

git add --all

git commit -am ""

git push

This will add the entire folder to a repository.

Note: To recursively add all files or folders and also sub folders to the staging area of git, we can either call “git add -A” or “git add –all”, it will add all files in the project workspace to the staging area, irrespective of location from where this command is executing.



Your Answer

Answers (2)

If you want to add all files in a folder recursively to a Git repository, here is how you can do it

1 Basic Command to Add All Files

To add all files inside a folder and its subfolders, use

  git add foldername

  • This will recursively add all files and subfolders inside the specified folder
  • If you want to add everything in the current directory, use

  git add .

This will include all new, modified, and deleted files

2 Checking What Will Be Added

Before committing, you can check what is being staged by running

  git status

This will show a list of tracked and untracked files

3 Committing the Changes

Once all files are added, commit them using

  git commit -m "Added all files in the folder"

Replace the message with something meaningful

4 Ignoring Unwanted Files

If you do not want to add certain files, create a .gitignore file and list the files or folders you want to ignore

 Example

node_modules
*.log
*.tmp

Then run

git add .
git commit -m "Added all required files"

5 Final Thoughts

  • Use git add foldername to add a specific folder
  • Use git add . to add everything in the current directory
  • Always check git status before committing to avoid adding unwanted files
  • Use .gitignore to exclude unnecessary files


1 Week

To add all files in a folder, including subfolders, to a Git repository, you can use the following commands:


git add .
git commit -m "Add all files recursively"

This assumes that you are already in the root directory of your Git repository.

Explanation:

git add .: This command adds all changes in the current directory and its subdirectories to the staging area. The . represents the current directory. By default, this command will recursively add all files and folders.

git commit -m "Add all files recursively": This command commits the changes added to the staging area with a commit message "Add all files recursively". You can replace the commit message with any message that describes the changes you're committing.

After running these commands, all files in the folder, including those in subfolders, will be staged and committed to the repository.

10 Months

Interviews

Parent Categories