How to Add Files to a Git Repository

John Wachira Mar 13, 2025 Git Git Add
  1. Adding a Single File
  2. Adding Multiple Files
  3. Adding All Changes at Once
  4. Adding Changes Interactively
  5. Conclusion
  6. FAQ
How to Add Files to a Git Repository

When working with Git, understanding how to add files to a repository is crucial. Whether you’re a beginner or an experienced developer, knowing the various methods to add files can streamline your workflow and enhance collaboration. In this article, we will explore the different ways to add files to a Git repository, ensuring you have the tools you need to manage your projects efficiently.

From adding single files to staging multiple files at once, Git provides various commands tailored to your needs. We’ll delve into each method, providing clear examples and explanations that will help you grasp the concepts easily. By the end of this article, you’ll be well-equipped to add files to your Git repository like a pro.

Adding a Single File

The simplest way to add files to your Git repository is by using the git add command followed by the file name. This method is particularly useful when you want to stage a specific file for your next commit.

git add filename.txt

After executing this command, the specified file will be staged, meaning it’s ready to be committed. You can verify the status of your repository by using the git status command. This will show you which files are staged for commit and which are not.

git status

Output:

On branch main
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   filename.txt

In this output, you can see that filename.txt is staged and ready for the next commit. This method is straightforward and ideal for when you are working on a small number of changes and want to commit them individually.

Adding Multiple Files

If you have several files to add to your Git repository, you can stage them all at once using the git add command with a wildcard or by specifying multiple file names. This approach saves time and effort, especially when dealing with larger projects.

Using Wildcards

To add all files of a specific type, such as all .txt files, you can use a wildcard:

git add *.txt

This command stages all text files in your current directory. It’s a quick way to gather multiple files without having to list each one individually.

Specifying Multiple Files

Alternatively, you can specify multiple files by listing them:

git add file1.txt file2.txt file3.txt

In both scenarios, you can again check the status of your repository with git status to confirm that the files have been staged.

git status

Output:

On branch main
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   file1.txt
        new file:   file2.txt
        new file:   file3.txt

This output confirms that all specified files are ready for the next commit. Using these methods is efficient when you have multiple changes to commit at once, helping you maintain a clean and organized repository.

Adding All Changes at Once

If you want to stage all changes in your repository, including new, modified, and deleted files, you can use the git add . command. This is particularly useful when you’ve made significant changes across multiple files and want to commit everything in one go.

git add .

This command stages all changes in the current directory and its subdirectories. After running this command, you can check the status again with git status.

git status

Output:

On branch main
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   file1.txt
        modified:   file2.txt
        deleted:    file3.txt

This output shows that all changes have been staged, including new files, modifications, and deletions. It’s a powerful command that can help you quickly prepare your changes for a commit, but be cautious when using it. Ensure that you really want to stage everything, as it can lead to unintended commits if you’re not careful.

Adding Changes Interactively

Sometimes, you might want to add changes selectively, especially when working on a large file with multiple modifications. The git add -p command allows you to stage changes interactively. This method is excellent for reviewing changes before committing.

git add -p

When you run this command, Git will present you with each change in the file, allowing you to decide whether to stage it or not. You’ll see prompts like y to stage the change, n to skip it, and s to split the change into smaller parts.

Here’s an example of how it might look:

Stage this hunk [y,n,q,a,d,/,e,?]? 

You can choose the appropriate option based on your needs. This interactive approach gives you granular control over what gets committed, helping you maintain a clean commit history.

Conclusion

Adding files to a Git repository is a fundamental skill every developer should master. Whether you’re adding a single file, multiple files, or even all changes at once, Git provides a range of commands to suit your workflow. By understanding these methods, you can manage your projects more effectively and collaborate seamlessly with others.

As you continue to work with Git, remember to experiment with these commands and find the approaches that work best for you. With practice, you’ll become more comfortable using Git and its powerful features.

FAQ

  1. How do I check if my files are staged?
    You can check if your files are staged by using the command git status. This will show you the current status of your files, including which are staged for commit.

  2. Can I unstage a file after adding it?
    Yes, you can unstage a file by using the command git restore --staged filename.txt. This command will remove the file from the staging area.

  3. What happens if I forget to add a file before committing?
    If you forget to add a file before committing, it won’t be included in that commit. You can always add the file later and create a new commit.

  4. Is there a way to add only specific changes from a file?
    Yes, you can use git add -p to stage specific changes interactively. This allows you to review and select which changes to stage.

  5. What is the difference between git add and git commit?
    git add is used to stage changes, while git commit is used to save those staged changes to the repository history. You must add files before you can commit them.

Enjoying our tutorials? Subscribe to DelftStack on YouTube to support us in creating more high-quality video guides. Subscribe
Author: John Wachira
John Wachira avatar John Wachira avatar

John is a Git and PowerShell geek. He uses his expertise in the version control system to help businesses manage their source code. According to him, Shell scripting is the number one choice for automating the management of systems.

LinkedIn

Related Article - Git Add