35

I have a NetBeans file "nbproject/project.properties" that always shows up in the "Changes not staged for commit" section (when I do git status). How could I move this to the Untracked files section (without adding it to .gitignore) ? I tried this command git rm --cached but what happened is the file shows as untracked in my local repo and got removed in the remote one but what I want is to keep it in remote and untrack only in local repo.

Mr_and_Mrs_D
  • 32,208
  • 39
  • 178
  • 361
Jimmy
  • 10,427
  • 18
  • 67
  • 122
  • 1
    Why would you not want to track the file if its in the remote repo? – yasouser Jul 22 '11 at 15:33
  • 1
    so the file is changed by other team member in the remote repo and i want to get their change's but i dont want to commit my change. – Jimmy Jul 22 '11 at 19:45

1 Answers1

92

You could update your index:

cd /root/folder/of/your/repo
git update-index --assume-unchanged nbproject/project.properties

and make sure it never shows as "updated" in your current repo.
That means it won't ever be pushed, but it is still present in the index.
(and it can be modified at will in your local working tree).


git update-index --no-assume-unchanged <file>

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

meetar
  • 7,443
  • 8
  • 42
  • 73
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 3
    I tried this but when I check out a commit or do a pull, the file still gets modified. – Cam Mar 02 '12 at 20:16
  • I don't know why on earth the ``git update-index --assume-index`` is not actually working for me! I tried ``git update-index --assume-unchanged `` Then check it with ``git ls-files -v | grep ^[a-z]`` First time, it shows that it worked, not showing it in the modified list. Then if again I hit ``git status``, it again shows that the file is modified! Can anyone tell me what am I doing wrong? – Tahsin Abrar Jul 22 '15 at 11:22
  • @TahsinAbrar could you ask a separate question describing your git version and the exact context of those commands? – VonC Jul 22 '15 at 13:49
  • this seemed to be working great until i tried to change branches. I couldn't because I had "unstaged changes" – max pleaner Nov 23 '16 at 00:55
  • @maxpleaner you could try and stash those changes (`git stash`) – VonC Nov 23 '16 at 05:45
  • @VonC I ended up using `checkout -f`to force undo the changes. But to be honest that was a pretty confusing situation and I'm not running to get there again. – max pleaner Nov 23 '16 at 05:49
  • I voted this answer while unfortunately the command doesn't work for a directory. I have to apply `git update-index --assume-unchanged ` for each file in the directory. – Weekend Nov 24 '16 at 05:04
  • @Weekend yes, Git works on content (files), not on containers (directories). You have alternatives listed at http://stackoverflow.com/a/12288918/6309. – VonC Nov 24 '16 at 05:14
  • @VonC Lucky, it is all tracked in Git that the directory which is to be assume unchanged and the files in it. So `find *` did the work. I find out that `git update-index --assume-unchanged ` will produce an error output just now, so `git ls-files` in your link is the common right solution, thank you! – Weekend Nov 24 '16 at 07:44
  • I had to execute the command from the repository root directory for it to work. – Mike W Mar 21 '17 at 14:18
  • @MikeW Good point. I have edited the answer to make that requirement clearer. – VonC Mar 21 '17 at 14:46