.gitignore is whitespace sensitive?

My .gitignore file seems to be working unpredictably. Here is an example:

I make a new repo foo with a file bar.txt, which I want to ignore:

  • Git: move changes off of master branch
  • Should website image files be included in GIT repository?
  • Iterative database migration in with git/gerrit
  • Git porcelain commands via pygit2?
  • Fixing broken git repository. No default revision 'HEAD'
  • With git, how can I perform a git add and still have it ignore the patterns in .gitignore?
  • pon2@kann4:~$ mkdir foo
    pon2@kann4:~$ cd foo/
    pon2@kann4:~/foo$ touch bar.txt
    pon2@kann4:~/foo$ git init
    Initialized empty Git repository in /home/pon2/foo/.git/
    pon2@kann4:~/foo$ git status
    # On branch master
    #
    # Initial commit
    #
    # Untracked files:
    #   (use "git add <file>..." to include in what will be committed)
    #
    #       bar.txt
    nothing added to commit but untracked files present (use "git add" to track)
    

    As expected, bar.txt shows up as untracked. So I tell git to ignore .txts, but I accidentally add some trailing whitespace:

    pon2@kann4:~/foo$ echo "*.txt " > .gitignore
    

    Now when I check the repo status, git doesn’t ignore bar.txt:

    pon2@kann4:~/foo$ git status
    # On branch master
    #
    # Initial commit
    #
    # Untracked files:
    #   (use "git add <file>..." to include in what will be committed)
    #
    #       .gitignore
    #       bar.txt
    nothing added to commit but untracked files present (use "git add" to track)
    

    What’s going on?

  • How do I do a 'git status' so it doesn't display untracked files without using .gitignore?
  • How do I use git without having a proper git server?
  • git shows random files changed on Mac NFS filesystem
  • Notepad++ error when doing git pull
  • Xcode not allows me to create local git
  • How to make git log output that just shows date and hash on one line?
  • 2 Solutions collect form web for “.gitignore is whitespace sensitive?”

    .gitignore is whitespace sensitive. If you include trailing whitespace, git won’t recognize your files.

    In this line there’s a trailing space:

    pon2@kann4:~/foo$ echo "*.txt " > .gitignore
    

    Once we fix that:

    pon2@kann4:~/foo$ echo "*.txt" > .gitignore
    

    The issue resolves:

    pon2@kann4:~/foo$ git status
    # On branch master
    #
    # Initial commit
    #
    # Untracked files:
    #   (use "git add <file>..." to include in what will be committed)
    #
    #       .gitignore
    nothing added to commit but untracked files present (use "git add" to track)
    pon2@kann4:~/foo$ 
    

    This changed in git 2.0. From the release notes:

    Trailing whitespaces in .gitignore files, unless they are quoted
    for fnmatch(3), e.g. "path\ ", are warned and ignored. Strictly
    speaking, this is a backward-incompatible change, but very unlikely
    to bite any sane user and adjusting should be obvious and easy.

    Git Baby is a git and github fan, let's start git clone.