How do you make Git ignore files without using .gitignore?
Asked Answered
D

10

222

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.

Daves answered 17/3, 2009 at 9:0 Comment(0)
J
255

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'".

Jammiejammin answered 17/3, 2009 at 9:21 Comment(3)
You can use update-index --assume-unchanged @see https://mcmap.net/q/54457/-how-do-you-make-git-ignore-files-without-using-gitignorePropertius
@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.Jammiejammin
The "other two" options still require adding the pattern to the ignore file and then running --assume-unchanged?Aalborg
I
261

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

Indene answered 17/3, 2009 at 9:3 Comment(3)
How does this work with submodules? There's no .git directory in a submodule...Christology
@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/excludeEngleman
This approach, though, doesn’t work if you want to ignore files that are already being tracked by Git (source article).Lorrettalorri
J
255

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'".

Jammiejammin answered 17/3, 2009 at 9:21 Comment(3)
You can use update-index --assume-unchanged @see https://mcmap.net/q/54457/-how-do-you-make-git-ignore-files-without-using-gitignorePropertius
@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.Jammiejammin
The "other two" options still require adding the pattern to the ignore file and then running --assume-unchanged?Aalborg
M
53

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

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

The latter two points could solve your problem.

For further information, see gitignore(5).

Mixup answered 17/3, 2009 at 9:19 Comment(0)
S
13

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.

Selenium answered 26/4, 2018 at 17:22 Comment(4)
This was the exact situation I was in and the perfect solution for me. Thanks!Mariellemariellen
If this solution is used, how will you later pull others' changes to the .gitignore? Why is this better than directly using --assume-unchanged file-to-ignore?Elissa
@kotool " The problem with git update-index --assume-unchanged in this case is that you cannot do that for an untracked file."Selenium
@kotool a .gitignore file is best customized to each local setup. There's no reason why you have have to pull one, as you would with a code file or build file. Usually they are check in as a convenience. If you want to pull it later, you would need to stop assuming the file is unchanged, save off or stash your local changes to the file, pull, and merge you changes back in. Something like git update-index --no-assume-unchanged .gitignore; git stash push -- .gitignore; git pull; git stash pop #may need to resolve conflict; git update-index --assume-unchanged .gitignore;Selenium
S
10

Another way:

  • edit local .gitignore
  • then git update-index --assume-unchanged .gitignore
Sokil answered 20/6, 2019 at 23:33 Comment(0)
P
9

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.
Propertius answered 11/8, 2014 at 22:2 Comment(0)
L
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>
Labium answered 5/3, 2015 at 20:43 Comment(0)
H
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

Higbee answered 2/4, 2015 at 22:2 Comment(0)
B
1

I use VS Code. I found this discussion and now I use the extension GitSweep

The extension is specific to VS Code but it uses Git techniques, which was helpful to learn. As their site says:

GitSweep uses git's --skip-worktree and .git/info/exclude to exclude changed files that you don't want to accidentally commit.

So these techniques are not new, nor specific to VS Code; the accepted answer already describes how to use git update-index --skip-worktree {file} and/or $GIT_DIR/info/exclude.

However, the extension is handy because it gives a visual reminder. Also, the GIF demo of the extension (seen below) points out that a new file can't use --skip-worktree, which implies that an old/already-tracked file might not be able to use .git/info/exclude. The inability to use .git/info/exclude was my problem -- so GitSweep taught me something new about how Git works!

A gif showing the GitSweep extension for VS Code working

The screenshot below demonstrates that files that I was already tracking in my repo could not be ignored with .git/info/exclude. When I listed them in my .git/info/exclude file , they were marked "Excluded" by GitSweep, but not removed from the Git/Source Control "Changes" list in VS Code (unexpected results, left of screenshot, red arrows). This was true before the GitSweep extension, GitSweep just reminds me what's happening.

However, when I used GitSweep to "sweep" the files (which uses the --skip-worktree technique , apparently), the files were no longer listed under my Git/Source Control "Changes" list! (expected results, right of screenshot, green arrows). This technique would also work independently of the GitSweep extension, GitSweep just reminds me what's happening.

screenshot showing the diff between git ".git/info/exclude" technique vs git "--skip-worktree" technique (works as expected)

Indeed, I could achieve the same effect without GitSweep VS Code extension, using the plain git commands $ git update-index --skip-worktree index.html, GitSweep just helps me remember what's happening. And for me, the --skip-worktree technique still works even if I checkout other branches (i.e. I don't get errors like described here), nor do I have to repeat the command (like when changing branches and using the --assume-unchanged technique)

Bezique answered 4/12, 2023 at 23:58 Comment(0)
O
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.

Outright answered 2/3, 2023 at 3:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.