29

I'm trying to create a new git repository from existing folder. I've created a .gitignore file in the root of the folder. But if I say

git add *
git commit
git push

files that should be ignored still get committed to the remote repository. I'm on Windows. Also I've bought a license for SmartGIT. It also seems to ignore .gitignore. I have to manually select which new files to commit.

Cœur
  • 37,241
  • 25
  • 195
  • 267
John Grey
  • 356
  • 1
  • 3
  • 5
  • 3
    What version of Git are you using? Also, you mention that you are using Windows, so pathname separators are always trouble. Could you post an example of your `.gitignore` file that doesn't work as you expect? – Greg Hewgill May 25 '10 at 04:01
  • 2
    Could you edit your question and post the content of your `.gitignore` file? `.gitignore` will use the underlying OS-specific `fnname()` method: see for instance: http://stackoverflow.com/questions/2330471 and http://stackoverflow.com/questions/1470572 – VonC May 25 '10 at 04:02
  • I haven't heard of SmartGIT - I use MSysGit and TortoiseGit and they seem to work with .gitignore files well. If the issue is with SmartGIT then I would try the alternative of MSysGit and TortoiseGit. – Geoff Adams May 25 '10 at 10:23
  • Do publish the result of `git status` and the content of `.gitignore`. On msysgit, directory separator are forward slashes: `/`, how does it look like for you? – Gauthier May 25 '10 at 11:18
  • Check out [this question](http://stackoverflow.com/questions/767147/how-do-i-tell-git-to-ignore-gitignore), which covers this ground in more detail... – buildsucceeded Jun 01 '11 at 11:36

10 Answers10

29

Your file must still be tracked, you can see by doing git status that will show you that your file is modified even if it's in .gitignore
You need to do this:

git update-index --assume-unchanged [path_to_file]
Shadowbob
  • 1,402
  • 2
  • 16
  • 24
  • That should be the accepted answer, so after adding file(s) in .gitignore it still shows up in git status. After above command, I got the desired result – Adeel Shekhani Jun 12 '23 at 05:18
27

Try "git add ." instead.

Also, it works for me (on Linux):

$ git init
$ echo foo > .gitignore
$ echo foo > foo
$ echo bar > bar
$ git add -v *
The following paths are ignored by one of your .gitignore files:
foo
Use -f if you really want to add them.
fatal: no files added
Jakub Narębski
  • 309,089
  • 65
  • 217
  • 230
24

Are your files already tracked? .gitignore only silences comments about untracked files, but won't stop a tracked file from being tracked.

Randal Schwartz
  • 39,428
  • 4
  • 43
  • 70
12

I've had issues with .gitignore also. I checked out the linked answers listed about, which fixed half the issue.

What really got gitignore working full for me was adding a comment on the first line of the file. Git wasn't parsing the exclude situated on the first line.

Cheers

  • 2
    Adding the comment on windows worked for me as well. +1 for you! – Jay D. Mar 29 '12 at 01:06
  • 1
    Sadly I figured this out for myself first... then found your comment. Now on to seeing if this is a bug that ought to be fixed! – altendky Oct 04 '12 at 14:34
6

You could have created a UTF-8 encoded text file. Try saving it as ANSI encoded. In git bash, you can verify by using vi -b.

Shen Wang
  • 61
  • 1
  • 1
  • Seems to be the cause. I didn't use `vi -b` but it worked after rewriting .gitignore from scratch – Javier May 12 '22 at 09:01
4

A trick I faced on windows is that using echo (as per Jakub Narębski answer) you have to be careful with spaces.

As you can see below any space before the redirection operator does have an effect on the actual ignore.

C:\test>dir /B
TOBEIGNORED

C:\test>echo TOBEIGNORED > .gitignore
C:\test>git status
[...]
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       .gitignore
#       TOBEIGNORED
C:\test>echo TOBEIGNORED> .gitignore
C:\test>git status
[...]
# 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)
Valentin Jacquemin
  • 2,215
  • 1
  • 19
  • 33
  • 1
    YES DONT USE ECHO ON WINDOWS OR YOU WILL WASTE 3 HOURS FIGURING OUT WHY THE HELL THE .gitignore JUST DOES NOT WORK – Mmmh mmh Aug 22 '14 at 12:58
0

I'm using git version 1.7.12.3 on MacOSX and the first line of .gitignore is also not taken into account. Just add a comment as first line.

0

Probably your exclude file mask is inacurate.

takeshin
  • 49,108
  • 32
  • 120
  • 164
0

Comment line as the first line of the file is critical! I spent considerable time trying to exclude files only to find that GIT was ignoring the first line in the ignore file.

Scott A
  • 21
  • 2
  • This is definitively not the issue. Almost none of my gitignore files (~50) has a comment in the first line, and they all work flawlessly. – mhutter Aug 19 '15 at 17:24
0

I lastly did echo node_modules >> .gitignore, and it wasn't working.

The windows terminal saves the file in UCS-2 LE BOM and git doesn't accept that.

It latter worked when I opened the file with Notepad and saved it with UTF-8 encoding

notepad save utf-8 encoding

It Worked fine then.

I think they need to fix this since echo "filetoignore" >> .gitignore actually seems a handy thing to do

Abraham
  • 12,140
  • 4
  • 56
  • 92