Exclude files from git revision without adding to .gitignore
Have you ever wanted to create some file(s) in your repo but do not want it to commit or show in the git changes?
Your first thought would be to mention that file in .gitignore
and commit it, but what if I told you there's a way without even adding it to your repo's .gitignore
:
Per repository basis
Let's suppose you want to have a folder drafts
and ignore it and everything in it from git:
-
create a folder
drafts
in your repo -
add some files to it
-
those files will currently show as untracked files when running
git status
-
Now, create a
.gitignore
file insidedrafts
and write*
in it. That's it! Rungit status
and you wouldn't see any changes!- you can create as many files or folders inside
drafts
and it wouldn't show up in git changes. - next time, you can use this one-liner command to create a
drafts
folder and put a.gitignore
file:
mkdir drafts && echo '*' > ./drafts/.gitignore
- you can create as many files or folders inside
For all repositories
- We'll be using a global
.gitignore
for it that takes higher precedence over a repo's.gitignore
file. - let's suppose you want to hide
.DS_Store
,.env.production
files from all current and future repo's:-
changes directory to
/Users/{User}
cd ~
-
create an empty file with .gitignore
- Mac:
touch .gitignore
- Windows:
type nul > .gitignore
orecho.> .gitignore
- Mac:
-
Tell git to use that file as global ignore
git config --global core.excludesfile ~/.gitignore
-
open this
.gitignore
file:- Mac:
open -a TextEdit ~/.gitignore
- Windows:
notepad ~/.gitignore
- Mac:
-
and mention files (or patterns) to ignore:
.DS_Store .env.production
-