How to Add All files in Git explained

Posted in Git on January 25, 2023 by Jessica Rose ‐ 7 min read

How to Add All files in Git explained

10xer.co Ad Sponsored

β“˜ Sponsored by 10xer.co

When working on a software development project, it is essential to have the ability to incorporate new files.

One way to manage and track these changes is through version control using Git.

The git add command allows you to include newly added files, modifications, and other updates in the versioning process.

To add all the untracked files and changes in Git, use the git add . command.

$ git add .

However, when working within a subdirectory of the project, this command will not include any changes made in the parent directories.

$ git add -A

By the end of this article, you will be well-versed in adding files to your project, including those in the current directory, those in the entire project, and those with specific extensions.

Additionally, you will have learned how to add only modified and deleted files while leaving new files untouched.

Adding All Files in Git

When it comes to adding files and changes to a Git repository, there are two main methods to choose from.

  1. The first is to use the “git add .” command, which will add all the files and changes in the current folder and its subfolders. This is useful when you want to add specific files or changes in a specific location within your repository.

  2. The second method is to use the “git add -A” command. This command will add all files and changes everywhere in your repository, regardless of their location. This is useful when you want to add everything in your repository at once, without having to navigate through different folders.

It’s important to note that these commands have different use cases and it’s up to you to decide which one will better suit your needs.

In summary, β€œgit add .” is used to add files and changes in the current folder and its subfolders, while β€œgit add -A” is used to add files and changes everywhere in the repo.

git add . Command explained:

The “git add .” command is used to stage changes in the current working directory and its subdirectories for a commit in Git.

It is used to prepare the changes you’ve made to your files for a commit. So that they can be included in the next version of your repository.

When you run the “git add .” command, Git will look for any new, modified, or deleted files in the current working directory and its subdirectories.

The dot at the end of the command indicates to Git that you wish to commit the changes in the current directory. This is similar to using the “.” in file paths, which refers to the current working directory. It is a way to specify that the changes should be made in the current folder, rather than specifying a specific path.

It will then stage these changes, which means that they will be added to the next commit.

This command is especially useful when you want to add specific changes or files in a certain location within your repository.

For example, if you only made changes to certain files in a specific subdirectory, you can navigate to that subdirectory and use the “git add .” command to only stage those changes.

It’s worth noting that this command only stages the changes and it doesn’t commit them yet, so you will still need to use the “git commit” command to finalize the changes.

All in all, “git add .” command is used to stage changes in the current working directory and its subdirectories for a commit in Git, this command is especially useful when you want to add specific changes or files in a certain location within your repository.

Example Let’s take a look at a concrete example.

In this example, I have a Git project in which I’ve added a file, deleted a file, and modified a file.

Here’s what the git status looks like:

$ git status 

10xer.co Ad Sponsored

β“˜ Sponsored by 10xer.co


On branch main
Your branch is up to date with 'origin/main'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	modified:   content/en/blog/.DS_Store

Untracked files:
  (use "git add <file>..." to include in what will be committed)
	content/en/blog//
	resources/_gen/images/blog/add-all-files-in-git-explained/
	resources/_gen/images/default-image_hu52edeca5cae62b99dc9f1d2237aed73e_4694_018a930c0e69bc6ec0fc37a676a7a73a.png
	resources/_gen/images/default-image_hu52edeca5cae62b99dc9f1d2237aed73e_4694_0665862f9409547fec46c8269ee65453.png
	resources/_gen/images/default-image_hu52edeca5cae62b99dc9f1d2237aed73e_4694_1270x715_fill_box_center_3.png
	resources/_gen/images/default-image_hu52edeca5cae62b99dc9f1d2237aed73e_4694_1920x1080_resize_q75_h2_box_3.webp
	resources/_gen/images/default-image_hu52edeca5cae62b99dc9f1d2237aed73e_4694_60050dd86c09508e07ff109641ac1e38.png
	resources/_gen/images/default-image_hu52edeca5cae62b99dc9f1d2237aed73e_4694_72b8612f764f3532997453ea41e3c8e9.webp
	resources/_gen/images/default-image_hu52edeca5cae62b99dc9f1d2237aed73e_4694_ac7f746701266a7dd0a07ef956d39e5d.png
	resources/_gen/images/default-image_hu52edeca5cae62b99dc9f1d2237aed73e_4694_ce553ce26735e50137f03f3f658db042.png
	resources/_gen/images/default-image_hu52edeca5cae62b99dc9f1d2237aed73e_4694_df0e6c5e71f7961a9ca7cbc27ea0c405.png

Git Status

After running the command “git status” again, it becomes clear that all the changes made to the files, such as additions, modifications, and deletions, have been successfully added to the staging area.

It is important to note that when using the command “git add .”, it is crucial to be in the root directory of the Git project, as this command might not function as intended if executed from a different location.

git add -A command explained:

The command “git add -A” is used to add all changes made to the files in a Git repository to the staging area. This includes new, modified, and deleted files.

The “-A” flag stands for “all” and tells Git to stage all changes made to the files in the repository, including those that have been deleted.

This command is useful for adding all changes made to the repository in one go, without having to manually stage each file or change individually.

For example, if you have made changes to multiple files in your repository and want to add all of them to the staging area, you can use the command

$ git add -A

instead of adding each file individually use the below command :

$ git add <file name>

It is worth noting that git add -A is an equivalent of git add . and git add -u together. The git add . stages new and modified files and git add -u stages deleted files.

In summary, “git add -A” is a convenient command that allows you to stage all changes made to the files in a Git repository in one go, including new, modified, and deleted files.

Add All Files by File Extension

Git allows you to add all files of a specific type using the git add command.

To accomplish this, you can specify the file extension after git add, using the format “*.extension”.

This is an efficient way to add multiple files of a particular type, without having to manually add each one individually.

 $ git add *.extension

Let’s consider an example wherein we want to add “.xml” to GitHub.

 $ git add *.xml

Add All Deleted and Modified Files Only

The git add -u command is used to stage changes for files that have already been tracked by Git. This command stage changes for all files that have been modified or deleted but do not stage new files that have been added to the repository.

The -u flag stands for “update” and it causes Git to only stage changes for files that are already being tracked. This means that if you’ve made changes to a file that’s already been committed, or if you’ve deleted a file, those changes will be staged for the next commit.

However, if you’ve added a new file to the repository, it will not be staged using this command.

It’s important to note that this command only stages changes for files that have been modified or deleted, it does not stage new files. If you want to stage new files as well, you can use the git add . command or git add –all command.

An example of using this command is when you are working on a feature and you have several files which have been modified and you want to commit them all together, instead of adding each file individually, you can just use git add -u command to add all the modified files at once.

In summary, git add -u command stages changes for all files that have been modified or deleted, but does not stage new files that have been added to the repository, it is a handy command when you want to stage changes for multiple files at once.

$ git add -u

Conclusion

As a quick summary:

  • git add . —> adds all the files in the current directory (and its subdirectories)
  • git add -A—> adds all the files on the entire project, even in parent directories.
  • git add *.extension —> adds all files that end with .extension.
  • git add -u . —> adds all the modified and deleted files but not new ones.

10xer.co Ad Sponsored

β“˜ Sponsored by 10xer.co