paint-brush
Git Tutorial: How to Easily Exclude Files During Git Add without Manual Entryby@kodewithchirag
1,764 reads
1,764 reads

Git Tutorial: How to Easily Exclude Files During Git Add without Manual Entry

by Kode with ChiragNovember 18th, 2021
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

The git add command can be used to add changes to the index for new commits. If you have done changes to under 13 files and wanted to exclude only 3 files from those, that means 10 files need to be added with git add. Instead of passing 10 files paths, I could just pass 3 files path. It's like a **NOT (!) operator** under the git add path option. It can be possible to exclude specific files during the command execution of this command (not with.gitignore)

Company Mentioned

Mention Thumbnail
featured image - Git Tutorial: How to Easily Exclude Files During Git Add without Manual Entry
Kode with Chirag HackerNoon profile picture

Every day we use the "Git add" command a lot to add our changes to the index for new commits, but have you ever wondered how we can add all the changed files with some files excluded during the execution of this command (not with .gitignore)? If the answer is yes, then this article will help you to understand how certain files can be excluded during the git add command.

TLDR; command to exclude specific file

git add -A ':!<file_path>'

Problem

One day, I was put in a situation where I need to add some files for my new commit but I also needed to exclude a few files during that execution; those files would get added later on once my work was done on those.


One way to do this was to hit the below command:


git add <file_path> <file_path> ... <file_path>


Basically, if I have done changes to under 13 files and wanted to exclude only 3 files from those, that means 10 files need to be added with the Git add command. I then have to copy all those 10 files path and paste them to the terminal manually and it will have become a little bit of a tedious task.


Let’s look at the example.


git add Dockerfile \
README.md \
nest-cli.json \
package-lock.json \
package.json \
src/app.controller.spec.ts \
src/app.controller.ts \
src/app.module.ts \
src/app.service.ts \
src/main.ts


After this command, you can now check for the staged files by hitting:


git status

Solution

But what if this process can be done inversely? 🤔


What if instead of passing 10 files paths, I could just pass 3 files path? 👀


Yes you heard it right this can be possible with Git add with the below example.


git add -A ':!.eslintrc.js' ':!.gitignore' ':!.prettierrc'


And now when you hit Git status, it will show all 10 files added to the index apart from the 3 excluded files. It's like a NOT (!) operator under the Git add path option.


Hope you enjoyed this content, please share your thoughts under comment and also get in touch with me on Twitter.