DEV Community

Cover image for Easy way to exclude files during git add
Chirag
Chirag

Posted on β€’ Originally published at kodewithchirag.com

13 5

Easy way to exclude files during git add

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>'
Enter fullscreen mode Exit fullscreen mode

Problem

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

One way to do this is to hit the below command

git add <file_path> <file_path> ... <file_path>
Enter fullscreen mode Exit fullscreen mode

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, then I have to copy all those 10 files path and paste it to the terminal manually and it will have become little bit 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

Enter fullscreen mode Exit fullscreen mode

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

git status
Enter fullscreen mode Exit fullscreen mode

Solution

But what if this process can be done inverselyπŸ€”? Like 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'
Enter fullscreen mode Exit fullscreen mode

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.

Image of AssemblyAI tool

Transforming Interviews into Publishable Stories with AssemblyAI

Insightview is a modern web application that streamlines the interview workflow for journalists. By leveraging AssemblyAI's LeMUR and Universal-2 technology, it transforms raw interview recordings into structured, actionable content, dramatically reducing the time from recording to publication.

Key Features:
πŸŽ₯ Audio/video file upload with real-time preview
πŸ—£οΈ Advanced transcription with speaker identification
⭐ Automatic highlight extraction of key moments
✍️ AI-powered article draft generation
πŸ“€ Export interview's subtitles in VTT format

Read full post

Top comments (2)

 
kodewithchirag profile image
Chirag β€’

Right, with git stash we can perform the same thing but it includes multiple commands to remember, but if you are aware of what files need to exclude then the syntax I have shared is much more easier one, I guess. πŸ˜€

Collapse
 
kodewithchirag profile image
Chirag β€’

Using GUI is the best thing but if GUI is not present then knowing few commands will help us to ease our work. πŸ˜‰

Eliminate Context Switching and Maximize Productivity

Pieces.app

Pieces Copilot is your personalized workflow assistant, working alongside your favorite apps. Ask questions about entire repositories, generate contextualized code, save and reuse useful snippets, and streamline your development process.

Learn more

πŸ‘‹ Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay