Easy way to exclude files during git add

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

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

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.

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

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.

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.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Kode with Chirag

Kode with Chirag

10 Followers

Helping others simplify the web development journey by keeping things short