81

I would like to know how to gitignore files but only on local, without pushing it (because the other people working on the project should not get this update of gitignore of files I added there.

To be simple, after a git status I only have :

modified:   .gitignore

because I added my files to ignore to my gitignore but well now I want the line above gone without pushing it.

SwissCodeMen
  • 4,222
  • 8
  • 24
  • 34
Max
  • 1,103
  • 1
  • 10
  • 20
  • 3
    Possible duplicate of [How do you make Git ignore files without using .gitignore?](https://stackoverflow.com/questions/653454/how-do-you-make-git-ignore-files-without-using-gitignore) – phd Mar 15 '18 at 18:59

5 Answers5

178

For local ignores you should use the .git/info/exclude file, not .gitignore:

Patterns which are specific to a particular repository but which do not need to be shared with other related repositories (e.g., auxiliary files that live inside the repository but are specific to one user’s workflow) should go into the $GIT_DIR/info/exclude file.

The two files accept the same format.

ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257
  • 6
    I often then symlink `.git/info/exclude` to `.gitignore_local` in the project root for visibility (and add that to `.gitignore`) – flurdy Mar 15 '22 at 17:51
  • 6
    You can self-reference `.gitignore_local` within itself if you prefer not polluting the shared `.gitignore` – Frédéric Bolduc Jul 05 '22 at 17:56
16

Gitignore file should be committed because in most cases what you ignore would be ignored by other developers in the team too.

But if you absolutely need to exclude it from being tracked for local changes, you could do the following:

git update-index --assume-unchanged .gitignore

This will make it "disappear" from the modified list. Though if someone else changes it, you will not get the changes on pulling. You'll then need to do the below to bring it back to tracked list and do a pull again:

git update-index --no-assume-unchanged .gitignore

Vasan
  • 4,810
  • 4
  • 20
  • 39
  • 8
    `git update-index --assume-unchanged` is a foot gun that is best avoided. The reason is in the name -- even though you did change something, ignore that fact that you did. `git status` will say that everything is committed and working fine, even though your view of reality will be different that that of another person having the "same" (but really not) version of your code. You would be better of using `.git/info/exclude` in this case. – jsageryd Mar 17 '18 at 17:58
  • 2
    @jsageryd This is only for .gitignore, and _"will say that everything is committed and working fine, even though your view of reality will be different that that of another person"_ is exactly what OP wanted. – Vasan Mar 17 '18 at 21:21
  • 3
    I'm using it in a configuration file that behaves differently in windows than linux, and since I'm the only dev using windows in the team it doesn't make sense to mess with their configurations, so I just excluded the file from git using this. – Carl Kroeger Ihl Jun 25 '19 at 04:30
4

Another option in VSCode is to use Git Exclude extension which basically add any file/folder using right click to your .git/info/exclude file

Tamir Gilany
  • 441
  • 7
  • 12
0

.gitignore file contains the list of files & folders that are to be prevented from getting committed/pushed to the remote repo.

But, it is always acceptable to commit & push .gitignore file. And it is a very common practise. So, commit the .gitignore file and move ahead with your code.

Read this thread for reference/good practises about committing .gitignore file.

Pankaj Singhal
  • 15,283
  • 9
  • 47
  • 86
-3

To exclude files without .gitignore

git update-index --assume-unchanged project/file.php

to remove file from exclude

git update-index --no-assume-unchanged project/file.php

this will help to ignore git file locally without adding it into .gitignore file

Mohamed Raza
  • 818
  • 7
  • 24