stage files in git

To add all files & folders on git, use the following command: git add -A

This command will stage all the changes for the next commit. There are some other similar commands on git that I will discuss later in this post.

Staged all files using git add -A

Git is one of the most popular version control systems around the globe. It keeps track of the changes to your project. You can learn more about Git, why it is used, why people like it so much, etc in this post.

Troubleshoot — Commands before git add -A (all)

Fatal error, Not a git repository

If you see a fatal error such as “command not found” or “not a git repository”, there is a chance that you have not installed & configured Git on your machine. Let’s follow the following steps:

Check if Git is installed on your machine?

You can not just type git add -A to stage all the files. Before adding all the files, you have to make sure Git is installed on your machine.

To check whether it is installed or not, open your command line or terminal, type the following command, and hit enter:

git --version

This is how to check Git version in your command line

This command will show you the installed version. But if you see an error like a command not found or any other error, it means that Git is not installed on your machine.

In that case, you have to install Git first. I am using VS Code terminal but you can use Git Bash, Mac Terminal, or whatever you like.

Configure your global username & email

Git commands for setting up global username & email

Git also needs to check who is making the changes. So you have to configure your global username & password before you add all files to git.

If you have not configured it yet, see the first-time configuration in the video below:

Initialize a Git repository

git init, initiate empty Git repository

Before you add untracked files, you have to initialize a Git repository. Use the git init command to do that.

Now you’re ready to add all changes or stage the files. That means, after making the above changes, you’re able to use the git add -A command.

The next command after adding all files to the git will be git commit -m "a custom message" and from there you can push your local repository to a remote origin such as GitHub.

This is how you can troubleshoot if you find an error while staging all files.

Adding specific files to git

Sometimes you may also need to add a specific file. To add a specific file, use the following command:

git add file_name

Example: git add index.html

You can also add multiple files followed by a space.

Example: git add index.html sass/style.css

stage two specific files to git

In this example, the second file lives in a folder named “sass.” That means you also have to mention the file path while adding (as well as the extension).

Git add all files in a folder

To add all files that live in a folder, use the following command: git add folder_path_and_name/*

Example: in my project root, I have a folder called “assets” and this folder contains other folders & files. To stage or add all files & folders in this “asset” directory, I used the following command:

git add assets/*

git add all files in a folder

git add all files in a sub-directory

If I want to stage only the “scripts” folder that lives inside the “assets“, the command will be as follows: git add assets/scripts/*

git add all files in a sub-folder

It will stage all the files & other folders that live inside the “scripts” folder.

An alternative command for adding all files in a folder

In the last example, you saw that git add folder_name/* command adds all files & folders in the stage. However, there is an alternative to this command as you see below.

git add .

Please note that this command (git add .) works based on the current directory.

That means it adds all files from a folder where your cursor is currently pointing at.

Example: Let’s say, I want to add all files that live inside the “assets/scripts” folder. So my first step should be to change the directory in my terminal using the following command: cd assets/scripts

Change directory in terminal

Now I am ready to use the git add . (if I want to add all files that live inside the scripts folder)

git add all files in a folder using the git add . (period) command

If your terminal is currently pointing to the root directory and if you use the git add ., this will stage all the changes that you made. In a nutshell, git add . works very similar to the git add -A. The only exception is git add . works based on the directory where your terminal currently pointing at.

Learn more about Git

Conclusion

In this post, I explained all sorts of information that you need to know about adding files, folders, specific files, all files in a sub-directory, etc. Also, I gave you supportive documents and other helpful resources to troubleshoot along the way. Therefore if you still have any questions about adding all files in Git, let me know.