Occasionally, you may want to exclude a specific path when staging files in Git. This can be useful when you have a directory with many files, but you only want to stage some of them. Today I will show how to do it.
Preparation
First, let’s clone a git repository and create some files to demonstrate the process.
The repository used here is one of my own, which contains some bioinformatics tools. You can use any repository you like, or create a new one.
|
|
Let’s create some new files
|
|
Exclude a folder
First, let’s check the status of the repository to see what files are untracked.
|
|
And you will see something like this, depending on your repository:
|
|
With the following command, you can add all files except the excluded
directory.
|
|
Note that the excluded path must following the following format:
- in single quotes, double quotes would trigger shell errors
- starts with ‘:!’
With the following command to check what have been staged:
|
|
And you can see something like:
|
|
Exclude more than one path at a time
Actually, one can exclude multiple paths at a time, and the below commands show this.
First, let’s roll back the staged files.
|
|
And then, let’s exclude two files in the excluded
folder (essentially the
same effect as excluding the whole folder, but just for demonstration purpose):
|
|
And you can check the status again.
I hope that this tip is helpful for you. Happy coding! 😄
Last modified on 2025-04-19