Git: untrack a file in local repo only and keep it in the remote repo
Asked Answered
G

1

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.

Gory answered 22/7, 2011 at 14:58 Comment(2)
Why would you not want to track the file if its in the remote repo?Coles
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.Gory
L
93

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 '

Liebman answered 22/7, 2011 at 17:25 Comment(11)
I tried this but when I check out a commit or do a pull, the file still gets modified.Apologetic
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 <file-path> 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?Etheline
@TahsinAbrar could you ask a separate question describing your git version and the exact context of those commands?Liebman
this seemed to be working great until i tried to change branches. I couldn't because I had "unstaged changes"Pug
@maxpleaner you could try and stash those changes (git stash)Liebman
@Liebman I ended up using checkout -fto force undo the changes. But to be honest that was a pretty confusing situation and I'm not running to get there again.Pug
I voted this answer while unfortunately the command doesn't work for a directory. I have to apply git update-index --assume-unchanged <filename> for each file in the directory.Mazarin
@Mazarin yes, Git works on content (files), not on containers (directories). You have alternatives listed at https://mcmap.net/q/12018/-git-update-index-assume-unchanged-on-directory.Liebman
@Liebman 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 <an_untracked_file> will produce an error output just now, so git ls-files in your link is the common right solution, thank you!Mazarin
I had to execute the command from the repository root directory for it to work.Kimberykimble
@MikeW Good point. I have edited the answer to make that requirement clearer.Liebman

© 2022 - 2024 — McMap. All rights reserved.