213

Due to external weird constraints I cannot modify the .gitignore of my repository. Is there a way to ignore files and directories other than modifying a .gitignore? Even if it is a global solution like a global configuration that will be applied to all my repositories.

Pablo Fernandez
  • 279,434
  • 135
  • 377
  • 622

9 Answers9

242

If you can modify .git/info/exclude you can put the same rules there. But that file is within your local repo only.

Ólafur Waage
  • 68,817
  • 22
  • 142
  • 198
  • 1
    How does this work with submodules? There's no `.git` directory in a submodule... – zakdances May 29 '13 at 00:55
  • 17
    @yourfriendzak a submodule's `.git` folder is actually at `parent_repo/.git/modules/submodule_name`. So you'd edit `parent_repo/.git/modules/submodule_name/info/exclude` – Kara Brightwell Jan 06 '15 at 14:23
  • 1
    This approach, though, doesn’t work if you want to ignore files that are already being tracked by Git [(source article)](https://luisdalmolin.dev/blog/ignoring-files-in-git-without-gitignore/). – Desko27 Feb 28 '23 at 19:12
238

Do not forget, according to gitignore, that there is an order of precedence in the different "ignore pattern sources" that Git consider:

  • Patterns read from the command line for those commands that support them.
  • Patterns read from a .gitignore file in the same directory as the path, or in any parent directory, with patterns in the higher level files (up to the root) being overridden by those in lower level files down to the directory containing the file.
  • Patterns read from $GIT_DIR/info/exclude.
  • Patterns read from the file specified by the configuration variable core.excludesfile.

The last two can be a solution for your problem but:

  • they are not replicated for a distant repository
  • they can have their patterns overridden by the other sources

(See also this SO question)


The other two solutions involve updating the index (git update-index):

However, when you checkout another branch or when you git pull, that "ignore" status might be reset. Hence the other option:

The difference between the two is explained in "Git - Difference Between 'assume-unchanged' and 'skip-worktree'".

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • You can use `update-index --assume-unchanged` @see http://stackoverflow.com/a/25253144/292408 – Elijah Lynn Aug 11 '14 at 22:06
  • 1
    @ElijahLynn sure. You also have `update-index --skip-work-tree`. I jave edited the answer to add some links to old answers of mine illustrating those two `update-index` commands. – VonC Aug 12 '14 at 05:30
51

There are three ways to tell GIT which files to ignore:

  • .gitignore files
  • $GIT_DIR/.git/info/exclude
  • Files pointed to via the core.excludesfile setting

The latter two points could solve your problem.

For further information, see gitignore(5).

acdcjunior
  • 132,397
  • 37
  • 331
  • 304
Ferdinand Beyer
  • 64,979
  • 15
  • 154
  • 145
12

I have been in similar situations, so I'm adding my preferred solution that I don't see mentioned. The problem with git update-index --assume-unchanged in this case is that you cannot do that for an untracked file. You said

I cannot modify the .gitignore of my repository.

I'm going to assume what you mean is that you can't push any changes to .gitignore to origin. If that is the case what you can do is add the untracked file to your local .gitignore, then do git update-index --assume-unchanged .gitignore so that your change to .gitignore is never pushed. Now you are ignoring the (possibly) untracked file, and not affecting the remote .gitignore file.

Tony
  • 953
  • 1
  • 10
  • 22
10

Another way:

  • edit local .gitignore
  • then git update-index --assume-unchanged .gitignore
langpavel
  • 1,270
  • 12
  • 16
8

Ignore local changes to tracked files: git update-index --assume-unchanged my-file.php

Unignore local changes to tracked files: git update-index --no-assume-unchanged my-file.php

source: git help update-index

--[no-]assume-unchanged
           ...
           This option can be also used as a coarse file-level mechanism to ignore uncommitted changes in tracked
           files (akin to what .gitignore does for untracked files). Git will fail (gracefully) in case it needs to
           modify this file in the index e.g. when merging in a commit; thus, in case the assumed-untracked file is
           changed upstream, you will need to handle the situation manually.
Elijah Lynn
  • 12,272
  • 10
  • 61
  • 91
7

If you just want to have some local files in the repository and the subdirectory location is flexible, you can put your files in tracked_dir1/tracked_dir2/untracked_dir/ and then add a tracked_dir1/tracked_dir2/untracked_dir/.gitignore with contents like:

*

I.e.

$ cat > tracked_dir1/tracked_dir2/untracked_dir/.gitignore
*
<Ctrl+D>
David Winiecki
  • 4,093
  • 2
  • 37
  • 39
1

You can use the global gitignore method instead of modifying the one in the project https://help.github.com/articles/ignoring-files/#create-a-global-gitignore

frazras
  • 5,928
  • 4
  • 30
  • 40
0

Modifying .git/info/exclude using vim or emacs works fine for me.

Taking note of @Desko27 comment, this approach, though, doesn’t work if you want to ignore files that are already being tracked by Git.

So ignore files early before tracking it.

Tosin Onikute
  • 3,883
  • 6
  • 38
  • 61