Eda Eren

July 3, 2023
  • Git

Excluding files (and folders) with `git add`

When you're using git add, there is a chance that you might be in a situation where you want to include a bunch of files (or folders) but exclude only some of them. Adding each file one by one manually is, of course, unthinkable, but thankfully there is a very short and sweet way to do it.

Here comes the magical command:

git add -A ':!<file_path>'
git add -A ':!<file_path>'

In my case, I had to rename a lot of folders, and wanted to git add them all at once, but exclude one of the untracked folders and a couple of other modified files.

Besides tons of other deleted files, git status was showing this:

Changes not staged for commit and untracked files

Since I wanted to exclude only the list-ops folder and the wordy files, I had to use the command:

git add -A ':!list-ops' ':!wordy/wordy.py' ':!wordy/wordy_test.py'
git add -A ':!list-ops' ':!wordy/wordy.py' ':!wordy/wordy_test.py'

And, it worked ๐ŸŽ‰:

Changes to be commited with those not staged for commit and untracked files

All thanks to Chirag Chhuchha's blog post.