9

I have a file mentioned in .gitignore, but I added it by git add -f because I wanted to add it only once and then ignore it as usual.

However, it is now tracked by default. How do I undo this?

Ryan Kohn
  • 13,079
  • 14
  • 56
  • 81
user1632167
  • 133
  • 1
  • 5

2 Answers2

11

Not completely sure what you're looking for but you can either remove the file to be ignored in future commits with

git rm --cached filename

Note that git will not ignore a file that was already tracked before a rule was added to this file to ignore it. In such a case the file must be un-tracked, usually with git rm --cached filename

Info: https://help.github.com/articles/ignoring-files

Or you can tell git that it ignores the changes of a file with

git update-index --assume-unchanged filename

and undo it with

git update-index --no-assume-unchanged filename

To list all assume-unchanged files run

git ls-files -v|grep '^h'

Info: How do I .gitignore and delete an already committed file without affecting other working copies?

However please keep in mind that assume-unchanged is (in my opinion) against the philosophy of git and this command will only ignore it the changes made on your machine. Everyone else will still be able to overwrite it.

In most cases you use this for configuration files. What you can do to avoid using this option is to keep it in the .gitignore and add a setup description to the README or create a little setup script which creates those files.

Community
  • 1
  • 1
Dominic
  • 3,353
  • 36
  • 47
  • I did `git rm --cached filename` now `git status` shows Changes to be committed: # (use "git reset HEAD ..." to unstage) # # deleted: main/filename – user1632167 Aug 30 '12 at 07:02
  • Yes from now on git will ignore main/filename – Dominic Aug 30 '12 at 09:15
  • 2
    `Not completely sure what you're looking for` - Just for some context for anyone else that might read this: this is useful if you want to have a configuration file with a blank template that users can download, but not commit sensitive information. At least, that's one reason why I came here looking to understand how to do this in git. – Joseph Mar 03 '17 at 12:01
  • @joseph4tw I always place a "sample-config" in the repo instead of using assume-unchanged. (e.g. .env_sample and .env, config_sample.php and config.php) It's a hassle if you want to add configuration parameters and always have to update this file. – Dominic Mar 27 '17 at 09:50
1

You can remove it with...

git rm --cached file

Then commit.

alex
  • 479,566
  • 201
  • 878
  • 984