Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ignore certain files in all folders using a single .gitignore file

Tags:

git

gitignore

After reading several questions about .gitignore I have found no one that could answer my question:

What do I have to add to .gitignore to ignore all files with a certain ending, say *.txt in all folders.

I'd prefer to only have one .gitignore file at top level.

I tried several things but none worked.

This is what I tested (.gitignore was added to the repository before, no other file was added):

  $ ls
  abc.txt  content

  $ ls content/
  abc.txt  def.other  def.txt

  $ echo "" > .gitignore


  $ git status --ignored --untracked-files=all
  # On branch master
  # Changes not staged for commit:
  #   (use "git add <file>..." to update what will be committed)
  #   (use "git checkout -- <file>..." to discard changes in working directory)
  #
  #       modified:   .gitignore
  #
  # Untracked files:
  #   (use "git add <file>..." to include in what will be committed)
  #
  #       abc.txt
  #       content/abc.txt
  #       content/def.other
  #       content/def.txt
  no changes added to commit (use "git add" and/or "git commit -a")

This is as expected: all files show up since .gitignore is empty.

  $ echo "*.txt" > .gitignore

  $ git status --ignored --untracked-files=all
  # On branch master
  # Changes not staged for commit:
  #   (use "git add <file>..." to update what will be committed)
  #   (use "git checkout -- <file>..." to discard changes in working directory)
  #
  #       modified:   .gitignore
  #
  # Untracked files:
  #   (use "git add <file>..." to include in what will be committed)
  #
  #       content/def.other
  # Ignored files:
  #   (use "git add -f <file>..." to include in what will be committed)
  #
  #       abc.txt
  no changes added to commit (use "git add" and/or "git commit -a")

Why don't the files content/abc.txt and content/def.txt show up in the list?

  $ git clean -n
  Would not remove content/

I thought they would show up here too.

  $ echo "" > .gitignore

  $ git clean -n
  Would remove abc.txt
  Would not remove content/

  $ cd content

  $ git clean -n -x
  Would remove def.other

  $ git clean -n -x
  Would remove abc.txt
  Would remove def.other
  Would remove def.txt

If the files content/abc.txt and content/def.txt are shown by clean -n -x but not by clean -n, I think they are ignored. But then why don't they show up in git status --ignored --untracked-files=all?

like image 674
Onur Avatar asked May 16 '12 11:05

Onur


1 Answers

Just adding *.txt works. Check gitignore(5) man page for the gitignore format explanation

If you try to add the content directory, all the *.txt file will be ignored.

$ echo "*.txt" > .gitignore 

$ git add content

$ git status --ignored --untracked-files=all
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#       new file:   content/def.other
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       .gitignore
# Ignored files:
#   (use "git add -f <file>..." to include in what will be committed)
#
#       abc.txt
#       content/abc.txt
#       content/def.txt
like image 55
KurzedMetal Avatar answered Oct 19 '22 19:10

KurzedMetal