Open In App

How to Add All Files in Git ?

Last Updated : 14 May, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Adding all files in Git is a common task when you want to stage all changes for committing. Adding all files in Git involves staging all modifications, additions, and deletions in your working directory for the next commit. This process ensures that all changes are included in the commit history. In this article, we will see the detailed guide for adding all files in Git.

What is Git’s staging Area?

Before diving into adding files in Git, It’s important to know about the concept of the staging area. The staging area, also known as the index, acts as a middle ground between your working directory and the repository. It serves as a checkpoint where you can review and prepare changes before committing them to the repository.

Approach 1: Using the git add command with the dot(.) notation:

Step 1: Create a project or folder so all our files will be there. Put all your files there to add on git.

For creating a folder use command- mkdir folder-name
For creating a file use command- touch file-name

Folder Structure: Create a folder similar to that.

Screenshot-2024-04-19-150004

Step 2: Initialize git to that project root or folder.

git init 

Screenshot-2024-04-19-150336

Step 3: Add all files using dot(.) to git.

git add .

And make a commit immediately

git commit -m "added all files"

Screenshot-2024-04-19-150652

Step 4: To see the log of your added file, just run this command

git log

And log look likes this:

Screenshot-2024-04-19-150901

Approach 2: Using the git add command with the –all or -A flag:

Step 1: Make some changes to all files so that turned into modified state, if a file modified then ‘M’ will be shown right side.

Screenshot-2024-04-19-151258

Step 2: To know the status of all files, just run this command.

git status

And you will see this.

Screenshot-2024-04-19-151455

Step 3: To add all files using –all or -A flag, run following command on terminal.

git add --all  OR
git add -A

and then make a commit.

git commit -m "added all modified files"

Screenshot-2024-04-19-151832

Now we can check git log.

Screenshot-2024-04-19-152049

Approach 3: Adding all files by extension.

In some cases, you may be interested in adding all files that have a specific extension : *.txt or *.js for example.

To add all files having a specific extension, you have to use the “git add” command followed by a wilcard and the extension to add.

git add *.txt
git add *.js

Let’s say, we have two js files, and we want to add both files to git, then follow these command.

Screenshot-2024-04-19-153632

In this way, We can easily add all files to git.

FAQs

1. What if I want to add only modified files and not new files?

Git’s git add . command stages both modified and new (untracked) files. If you only want to add modified files, you can use git add -u.

2. How can I check which files are staged for the next commit?

You can use the command git status to see the status of files in your working directory. Staged files will be listed under “Changes to be committed.”

3. Is there a way to add all files but exclude certain ones?

Yes, you can use a .gitignore file to specify patterns of files that Git should ignore. Files matching these patterns will not be staged when using git add ..

4. Can I undo adding files before committing?

Yes, you can unstage files using git reset HEAD <file>. This command removes the specified file(s) from the staging area, but keeps the changes in your working directory.

5. Will git add . add files in subdirectories too?

Yes, git add . stages all modified and untracked files in the current directory and its subdirectories.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads