Home > Software engineering >  Stop tracking a file in git from a timepoint on while keeping old tracked commits
Stop tracking a file in git from a timepoint on while keeping old tracked commits

Time:12-02

A friend asked me for a way to untrack a file from a time point on for the future - but being able to git checkup to a state where the file was still tracked by git. Aim is to keep the file locally but prevent for future commits

I googled in the past for this - and read several stackoverflow answers to similar problems.

But it seems that there is no good way to achieve this.

It seems that the tracking status of files is globally controlled. Is there a way to keep the tracking status locally controlled (meaning can be changed from commit to commit)?

CodePudding user response:

It seems that the tracking status of files is globally controlled

No, it doesn't.

Is there a way to keep the tracking status locally controlled (meaning can be changed from commit to commit)?

No, because that is not what tracking is. None of what you've said is what tracking is.

A file is tracked because it is present in the index. That's basically all there is to it. It is present in the index because either (1) it is present in the currently checked out commit (aka HEAD) or (2) you have created the file and added it to the index (with git add). This definition is completely automatic and autonomous.

So how can you make a tracked file untracked? On a rather obvious and crude level, since the whole definition of "tracked" depends on the index, you can simply remove the file from the index (with git rm).

There is also an exclusion mechanism: you can use a .gitignore file. But this has no effect whatever on what I've already said. It merely lists exceptions to broad actions, saying that if a new file of a certain type should appear, it should not be added to the index when you give a global command such as git add ., and it should not be listed among the new untracked files when you say git status.

CodePudding user response:

To stop tracking the file git rm file_name

To keep the file from being tracked, add the filename to .gitignore

  • Related