Let AI write emails and messages for you 🔥

Exclude files from git revision without adding to .gitignore

Gourav Goyal

Gourav Goyal

Sep 8, 2023

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 inside drafts and write * in it. That's it! Run git 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
    

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:
    1. changes directory to /Users/{User}

      • cd ~
    2. create an empty file with .gitignore

      • Mac: touch .gitignore
      • Windows: type nul > .gitignore or echo.> .gitignore
    3. Tell git to use that file as global ignore

      • git config --global core.excludesfile ~/.gitignore
    4. open this .gitignore file:

      • Mac: open -a TextEdit ~/.gitignore
      • Windows: notepad ~/.gitignore
    5. and mention files (or patterns) to ignore:

      .DS_Store
      .env.production
      
That's all, folks!

Gourav Goyal

Gourav Goyal