Git

How to Add All Files to a Commit Except a Single File?

Developers can create multiple source code files in the local repository. After working on it, they need to push these files from the staging index to the working directory for updating purposes. However, sometimes they realize a pushed file contains unnecessary content or need to modify files and don’t want to push these files for a while. They can use the β€œgit reset — <file-path>” command in this situation.

This post will discuss adding files to a commit except for a particular file.

How to Add All Files to a Commit Except a Particular File?

Follow the provided steps to add all files to a commit excerpt a single file:

  • Redirect to the required repository.
  • Check the current status.
  • Run the β€œgit reset — <file-path>” command.
  • Show the current repository status.
  • Commit all modifications.

Step 1: Navigate to Git Repository

At first, redirect to the Git particular repository by utilizing the β€œcd” command:

$ cd "C:\Users\nazma\Git\cherry"

Step 2: Check Status

Then, run the β€œgit status .” command to check the current working branch status:

$ git status .

Below-highlighted staged file from the given output we don’t want to commit:

Step 3: Reset Changes

Now, execute the β€œgit reset” command to reset the Changes:

$ git reset -- cherry/file.py

Step 4: View Git Status

Lastly, use the provided command to display the current status of the working branch:

$ git status .

As you can see, the desired file changes are moved to the working index:

Step 5: Commit Changes

Now, commit all the added changes to the Git repository excerpt a single file by running the β€œgit commit” command:

$ git commit -m "new file added"

Step 6: Verification

Lastly, to ensure all changes are moved to the Git repository excerpt single file, run the β€œgit status .” command:

$ git status .

It can be seen that the previously reset file is placed in the working area:

That’s all! We have explained the method of adding all files to a commit excerpt single file.

Conclusion

To add all files to a commit, excerpt a single file, first, move to the required repository and check the current status. Then, execute the β€œgit reset — <file-path>” command, check the repository’s current status and commit changes. This post illustrated the method of adding files to the Git repository by committing except a particular file.

About the author

Maria Naz

I hold a master's degree in computer science. I am passionate about my work, exploring new technologies, learning programming languages, and I love to share my knowledge with the world.