2

The .gitignore of a project (maintained by someone else) is like this:

.idea/*
!.idea/watcherTasks.xml

I want to add private watcher tasks, so my watcherTasks.xml is changed. But I don't want to commit it.

I don't want to type git reset .idea/watcherTasks.xml (or specify the paths) every time I git add/git commit. So I tried to use .git/info/exclude to ignore the file locally:

.idea/watcherTasks.xml

However, the .git/info/exclude line cannot override the .gitignore line (probably because ! has higher priority).

Is there something I can put in .git/info/exclude to cancel the effect of the !.idea/watcherTasks.xml line in .gitignore? I don't want to modify the .gitignore since I do not maintain the project.

This question does not ask for approaches like git commit $path, because I do not want to change future commit commands to use.

SOFe
  • 7,867
  • 4
  • 33
  • 61
  • 1
    .gitignore has no effect if the file is already committed / added to the repo. – melpomene Jun 06 '19 at 04:21
  • @SOFe The current selected answer, while technically correct, does not address your problem: I have proposed an actual solution. – VonC Jun 06 '19 at 04:57
  • 1
    Possible duplicate of [Can I 'git commit' a file and ignore its content changes?](https://stackoverflow.com/questions/3319479/can-i-git-commit-a-file-and-ignore-its-content-changes) – 1615903 Jun 06 '19 at 07:06
  • @1615903 this question specifically asks *without specifying options in each add/commit command*. While the linked question also has relevant answers, this question has a more specific requirement such that most answers there would not suffice. – SOFe Jun 06 '19 at 07:10

3 Answers3

2

If I understand what you are asking (summarized: to ignore an ignore rule), the answer is No. In the .gitignore documentation: https://git-scm.com/docs/gitignore it clearly states the following:

A gitignore file specifies intentionally untracked files that Git should ignore. Files already tracked by Git are not affected;

Now, as you note, you do not want to affect the overall repository. So, I think you are out of luck on this. Sorry.

1

If your .idea/watcherTasks.xml is already tracked, you can:

  • modify it locally
  • make sure it remains not added/committed with

    cd /root/folder/of/your/local/repository
    git update-index --skip-worktree -- .idea/watcherTasks.xml
    

That means:

  • no need to override an ignore rule
  • no need to modify a .gitignore

Your tracked file remains tracked, but none of your local modification will be added or committed.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
1

I tested this using # and it worked.

Before...

public_html/includes/local.php

local.php is ignored, but when I changed it to...

#public_html/includes/local.php

local.php is there.

I can see no error warnings on my github desktop, nor on the site itself.