August 21, 2016
Have you been wanting a way to create and keep new files in a git repo without checking them into git, and
also
not adding them to your .gitignore? Maybe you're working on a large team and want to impose on them neither your config files nor another line in the project's
.gitignore
?
Well, you're in luck.
It's the git exclude!
Git's exclude file functions just like
.gitignore
,
but it isn't checked into git, so committing and pushing your project will allow you to keep your excluded files locally, wihle not making any changes to the remote repository.
In your project's
.git
directory,
cd
into the
info
directory. There, you'll find a file called
exclude
.
Just open it with your favorite text editor and start adding filenames. Alternatively, if you'd like to exclude a file, say Procfile.dev, from your project's root directory with just one command, you can run:
echo "Procfile.dev" >> .git/info/exclude
You can also throw the following function into your
.bashrc
or
.bash_profile
and run
git_exclude "filename"
on the command line:
git_exclude() { echo $1 >> .git/info/exclude }
Similarly with
.gitignore
,
if you exclude a file after it's already been checked-in and committed, you'll have to forcibly remove the file from git like so:
git rm --cached
Then, the excluded file will register as deleted from your git repository.