209

Can I tell git to ignore files that are modified (deleted) but should not be committed?

The situation is that I have a subdirectory in the repo which contains stuff I'm not interested in at all, so I deleted it to prevent it showing up in auto-completions and the like (in the IDE).

But now, if I add that folder to .gitignore, simply nothing changes, all the stuff is shown as deleted by git status.

Is there a way to make git ignore it either way?

(Alternatively, as I'm using git-svn, could I commit the changes to the local git and ensure they are not passed on to the svn repo?)

Sam
  • 7,252
  • 16
  • 46
  • 65
  • instead of just marking them as unmodified in the repo index, you might want to remove them from the index completely. So while you will still have the file in your working directory, the repo index can be delted entirely, and git won't even see it as having existed at all. Please see the link in my answer below. – MaurerPower Jun 03 '12 at 02:15

6 Answers6

311

check out the git-update-index man page and the --assume-unchanged bit and related.

when I have your problem I do this

git update-index --assume-unchanged dir-im-removing/

or a specific file

git update-index --assume-unchanged config/database.yml
Carl Walsh
  • 6,100
  • 2
  • 46
  • 50
csmosx
  • 3,261
  • 1
  • 17
  • 10
  • This works great for modified files. However when I delete the dir (either with 'rm' or 'git rm') the files are listed as missing/deleted in 'git status'. I think I'll accept this as an answer and restate the question explicitly asking about deleted files. (At first, I didn't know that there was such a big difference ;) Thanks! –  Apr 24 '09 at 14:32
  • 4
    Can you list all your ignored files somehow ? – Zitrax Apr 07 '10 at 10:10
  • Anyone know a way to do this with the TortoiseGit GUI? – Simon East Apr 26 '12 at 12:04
  • 32
    You can get the files back with `git update-index --no-assume-unchanged config/database.yml` – Denys Kniazhev-Support Ukraine Dec 12 '12 at 12:25
  • 1
    See Dave L's answer below for a newer solution without some of the shortcomings of `--assume-unchanged`. – Ben Challenor Jan 09 '13 at 11:29
  • 2
    Is it possible to add this to the repository config file, instead of running this command manually every time? – Tim Boland Sep 30 '14 at 22:44
  • 1
    Will it appear as modified for next commit? I don't want it to be assumed unchanged forever. – Pranav Nandan Apr 27 '18 at 12:54
  • `--assume-unchanged` not working for me. I ended up with using `git update-index --skip-worktree ` – Huang C. Dec 18 '21 at 03:18
42

A newer and better option is git update-index --skip-worktree which won't be lost on a hard reset or a new change from a pull.

See the man page.

And a comparison at http://fallengamer.livejournal.com/93321.html

Carl Walsh
  • 6,100
  • 2
  • 46
  • 50
Dave L.
  • 43,907
  • 11
  • 63
  • 62
  • 9
    One disadvantag to note is that once a file's modifications have been hidden with `skip-worktree`, finding these files is a bit cumbersome. The only way I know of is `git ls-files -v |grep -v '^H'`. Also, many GUI tools do not know about this feature, and may produce funny errors if e.g. a `checkout` fails because of "hidden" modified files. – sleske Oct 04 '12 at 08:11
  • 2
    I have never been able to understand ANY of the git doc files. So this isn't much help. (Each doc file assumes that you understand the subtleties of every other doc file!) – SMBiggs Mar 13 '20 at 02:35
13

Use this code

git update-index --assume-unchanged file-name
Sujiraj R
  • 1,544
  • 1
  • 13
  • 4
  • 2
    Really interesting. I have to use a passord in my maven project but I don't want to publish it on a git repository, so I put placeholder for it without value in local,properties, I commit it, then I insert the password, but mark the file unchanged with your suggested command. So the password is not published. – Stefano Scarpanti Jul 13 '17 at 09:28
7

What I usually do is

git stash

git whatever-else

git stash apply

git stash clear
Tiago Martins Peres
  • 14,289
  • 18
  • 86
  • 145
Walter
  • 7,809
  • 1
  • 30
  • 30
  • 12
    fwiw you can also 'git stash pop' to do those last two at once (though 'git stash clear' will clear ALL stash entries, if you want that). – Groxx Feb 09 '13 at 03:59
6

Tracked files can't be ignored, so you'll have to remove them from your index first. Add a .gitignore that ignores the directories you don't want, then delete them, and remove any stragglers with git rm --cached.

John Feminella
  • 303,634
  • 46
  • 339
  • 357
  • 1
    Hm, I try this, but then i have all files listed as deleted. Should I commit that and the --cached will cause that it will not be pushed to remotes? Or did I get sth. wrong? The most important for me is not corrupting the remote (svn) repo. –  Mar 17 '09 at 17:44
  • 1
    You can't commit anything that doesn't start out in your index. git rm --cached everything that you don't want to commit, then add a .gitignore file locally that has "*" in it. Now, no matter how much you git add, you'll never see those files in your index again. – John Feminella Mar 17 '09 at 18:20
  • Ah, I think i understand --cached now.. It only removes the stuff from the index, and leaves the working tree alone.. I'm not sure whether I'm missing something, but AFAIS I'm looking for the opposite, removing it from the working tree without touching the index.. Or can I use it for that somehow? –  Mar 18 '09 at 14:43
0

What worked in my case:

git restore --staged filename

Example: git restore --staged index.js

Gabriel Arghire
  • 1,992
  • 1
  • 21
  • 34