If you want to ignore a file that you've committed in the past, you'll need to delete the file from your repository and then add a . gitignore rule for it. Using the --cached option with git rm means that the file will be deleted from your repository, but will remain in your working directory as an ignored file.
. gitignore is a file in your git root directory. Add the name patterns for the files that you want to ignore, and the files will be ignored automatically. It is the directory where you used git init .
If you want to maintain a folder and not the files inside it, just put a ". gitignore" file in the folder with "*" as the content. This file will make Git ignore all content from the repository.
Do not forget, according to gitignore, that there is an order of precedence in the different "ignore pattern sources" that Git consider:
$GIT_DIR/info/exclude
.core.excludesfile
.The last two can be a solution for your problem but:
(See also this SO question)
The other two solutions involve updating the index (git update-index
):
git update-index --assume-unchanged
: see "Git: untrack a file in local repo only and keep it in the remote repo".git update-index --assume-unchanged
on directory". --no-assume-unchange
to reverse the effect: See "Is it possible to git add
a file currently protected by assume-unchanged
?".However, when you checkout another branch or when you git pull
, that "ignore" status might be reset. Hence the other option:
git update-index --skip-worktree
; see:
The difference between the two is explained in "Git - Difference Between 'assume-unchanged
' and 'skip-worktree
'".
If you can modify .git/info/exclude
you can put the same rules there. But that file is within your local repo only.
There are three ways to tell GIT which files to ignore:
.gitignore
files$GIT_DIR/.git/info/exclude
core.excludesfile
settingThe latter two points could solve your problem.
For further information, see gitignore(5).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With