Excluding Files in 'git add' Using Pathspec
Tags: git, productivity
Published: 2020-05-30 23:02:43

---
title: Excluding Files in 'git add' Using Pathspec
published: true
description: Easily exclude files when staging your changes in git
tags: git,productivity
//cover_image: https://direct_url_to_image.jpg
---

_This post was originally published on [my blog](https://www.loumarven.dev/2020/05/31/excluding-files-in-git-add-using-pathspec/)._

When I use git to do version control in my projects, my typical flow would be the following:
1. Initialize git repository (git init).
2. Make changes to the code.
3. Add the changes to the staging area (git add).
4. Commit the changes to the local repository (git commit).
5. Push changes to the remote repository (git push).

I made changes to my project and so had to commit them. Typically, I would stage these changes with `git add -A` which adds all new and modified files to the staging area. There are some cases though that I wanted to exclude some files from being added to the staging area. One particular case is when I tested some modifications to a gem I am using. In my Gemfile, instead of using the default gem's repository, I pointed the said gem to my local copy which I modified:   

```ruby
gem 'some-arbitrary-gem', path: '~/Documents/code/oss/some-arbitrary-gem-master/'
```

Running `bundle install` would also modify `Gemfile.lock`, so git would know that both the Gemfile and Gemfile.lock have been modified. But I didn't want to add and commit these changes yet until I'm satisfied with my modifications.

I could manually add each of the files I wanted to add. Say if I have ten files I wanted to add, I could manually enter `git add file1 file2 fileN`. This can get inefficient though, especially when you have a lot of files you wanted to add and perhaps just one file to exclude. So I [searched online](https://duckduckgo.com/?t=ffab&q=git+add+all+except&ia=web) for an efficient way to get this done and came across [this answer](https://stackoverflow.com/a/51914162) that worked for me.

Applying the answer in my context, I added my files and excluded Gemfile and Gemfile.lock with the following:
```shell
git add --all -- ':!Gemfile*'
```

I looked at git add's documentation to see what those additional options for `git add` meant. The `--` option separates command-line options from the list of files. In the case above, it separates the `--all` option from the list of specified files. The `':!Gemfile*'` from the command above is an example of specifying a <em>pathspec</em>. A pathspec, based on the [online documentation](https://git-scm.com/docs/gitglossary#Documentation/gitglossary.txt-aiddefpathspecapathspec), is a pattern used to limit paths in Git commands. A pathspec with a colon (:), in a nutshell, lets you exclude certain paths and files. When using `git add` to exclude files or directories using pathspec <em>magic</em>, you may use its long form:
```shell
git add --all -- ':exclude(Gemfile*)'
```
or its short form:
```shell
git add --all -- ':!Gemfile*'
```

It can be used with [other git commands](https://git-scm.com/docs/gitglossary#Documentation/gitglossary.txt-aiddefpathspecapathspec) aside from `git add`. But knowing I can easily exclude files and directories when adding/staging changes is enough for me now.