0

How can I untrack a file but not remove it from upstream?

When I tried to do: git rm --cached "file" then it says that its deleted.

then I add and commit and push it. Now the file has been removed from upstream.

This is not what I want. I want it to still be in the upstream, but not longer tracked for changes on my local. Can this be done?

mrfr
  • 1,724
  • 2
  • 23
  • 44
  • No. You will have to add back the file after pulling (and the file is deleted). If you want to keep tracking it, but disregard local modifications, that's different and there are some tricks for that, such as assume unchanged (plenty of other questions here about this). – Lasse V. Karlsen May 04 '18 at 12:17
  • If you could link one of those questions I would be forever grateful. I've been searching forever! – mrfr May 04 '18 at 12:40
  • Check the answer on this question, which shows both what you don't want as well as the assume-unchanged trick: https://stackoverflow.com/questions/936249/how-to-stop-tracking-and-ignore-changes-to-a-file-in-git/936290#936290 – Lasse V. Karlsen May 04 '18 at 13:24
  • 1
    Possible duplicate of [How to stop tracking and ignore changes to a file in Git?](https://stackoverflow.com/questions/936249/how-to-stop-tracking-and-ignore-changes-to-a-file-in-git) – phd May 04 '18 at 15:36

1 Answers1

1

You want:

git update-index --skip-worktree <path-name>

This will ignore all local changes, but will still track the file in other remotes. This also has the added benefit of showing if changes happen to that file on a remote, and managing it correctly - showing conflicts with your local, etc.

Do not use --assume-unchanged - this does not have those safeguards in place, and will do things like overwrite your local changes without warning if a remote changes the file, and start retracking it again.

See this and the linked blog post for more information on the difference.

eedrah
  • 2,245
  • 18
  • 32
  • Thanks! This looks like it! What of we have like 100+ files? Can we add this recursivly to our repo? And how does this work with gitignore? – mrfr May 04 '18 at 16:00
  • If they are in a common directory you can use the directory name rather than the individual files. Otherwise you could use list the files individually. – eedrah May 04 '18 at 16:12
  • Damn. There not unfortunately.. Is it possible to do the opposit? Since we want to apply this to almost all files except a few directories – mrfr May 04 '18 at 16:14
  • .gitignore is for ignoring/excluding files and sharing the list of these files across every remote of the repo/every person. This solution is for ignoring on a local basis. – eedrah May 04 '18 at 16:31
  • `git ls-files` is your friend - exclude the ones you want to keep and apply that list to the command. – eedrah May 04 '18 at 16:32
  • So This command logs all files currently tracked,right? So how can this assist me? Thanks!! – mrfr May 04 '18 at 16:41
  • Yes, take the list of files given by `ls-files` and use this list in the command given in the answer – eedrah May 07 '18 at 15:58