Git

How to Add Multiple Files in Git for a Single Commit?

On a Git development project, developers work and make changes on multiple files simultaneously. After that, they add those changes to the Git repository. However, sometimes, it is required to add all the files’ changes in a single commit to ensure that the changes are grouped and can be tracked easily as a single unit. More specifically, it specifies the review process.

This write-up will illustrate the procedure to add multiple files in Git for a single commit.

How to Add Multiple/Various Files in Git for a Single Commit?

To add multiple files in the local Git repository in a single commit, try out the below-mentioned steps:

  • Switch to the local repository.
  • Create/make multiple files.
  • Add all files to the Git index.
  • Commit multiple desired files using the “git commit <file1-name> <file2-name> <file3-name> -m “<commit-message>”” command.
  • Verify changes.

Step 1: Navigate to Local Repository

First, redirect to the particular local directory:

$ cd "C:\Git\testRepo"

Step 2: Create Multiple Files

Then, create or make some files in the current repository using the “touch” command:

$ touch File1.txt File2.txt test.txt demo.txt

Here, “File1.txt”, “File2.txt”, “test.txt” and “demo.txt” are the names of the files that we want to create:

Step 3: Stage All Files

Next, type out the provided command to add all the files to the Git staging area:

$ git add .

Step 4: Check Git Status

After that, view the current status of the repository:

$ git status

The below output shows four files that have been staged:

Step 5: Commit Multiple Files

Now, utilize the “git commit” command along with the desired commit message and specify the file names that you want to commit. For instance, we want to commit three files that are “File1.txt”, “test.txt” and “demo.txt”:

$ git commit File1.txt demo.txt test.txt -m "Three files added"

Step 6: Verify Changes

To ensure that the files have been committed or not, run the given-provided command:

$ git show HEAD

In the below output the name of three committed files can be seen:

Step 7: View Current Status

Lastly, check the Git status to view the uncommitted file:

$ git status

The Git status shows the file that we did not commit:

That was all about adding multiple files in Git for a single commit.

Conclusion

To add multiple files in Git for a single commit, first, redirect to the local repository. Then, create multiple files and stage them. Next, utilize the “git commit <file1-name> <file2-name> <file3-name> -m “<commit-message>”” command to add multiple desired files to the local Git repository in a single commit. Lastly, verify changes. This write-up illustrated the method to add multiple files in Git for a single commit.

About the author

Laiba Younas

I have done bachelors in Computer Science. Being passionate about learning new technologies, I am interested in exploring different programming languages and sharing my experience with the world.