0

I have a dir /data and my file main.py. I use this as a beta file and change this first, rather than deploying in production directly. Doing this manipulates the files in /data, and when I later commit it, it commits all the data/ files too, which of course I don't want to.

I tried adding the dir data/ in .gitignore, and then executing

git rm -rf --cached .
git add .

but when I try to commit, it shows I removed all the files in data/, which is exactly not I want.

What I wanted was: It just stops tracking the files, i.e. it will neither remove them, nor commit the changed files. How can I do this?

AwesomeSam
  • 153
  • 1
  • 16
  • 1
    Use .gitignore to stop certain files or folders from being commited – Alpha Wolf Gamer Jun 10 '21 at 03:40
  • @AlphaWolfGamer I already said when i add dir to `.gitignore`, it removes it entirely.. rather than stop tracking it – AwesomeSam Jun 10 '21 at 03:44
  • You normally only need to do `git rm -rf --cached .` *once*, when it's already tracked and you want to untrack it. After that, you just need to put it into .gitignore and git will stop tracking it. Putting it in .gitignore does not delete anything, so it's probably some other command/step you are doing that deletes it. – Gino Mempin Jun 10 '21 at 04:03
  • If you are using an IDE, then it probably just *hides* gitignored files from the UI, but doesn't actually delete them. – Gino Mempin Jun 10 '21 at 04:04
  • @GinoMempin Thanks, but i found what I needed- `--skip-worktree` – AwesomeSam Jun 10 '21 at 04:09

1 Answers1

1

I found what I needed: --skip-worktree

You will have to apply skip-worktree to all elements inside that folder though.
See "Git Skip Worktree on All Tracked Files inside Directory and its Subdirectories"

As I explain in "Git - Difference Between 'assume-unchanged' and 'skip-worktree'"

Users often try to use the assume-unchanged and skip-worktree bits to tell Git to ignore changes to files that are tracked.
This does not work as expected, since Git may still check working tree files against the index when performing certain operations.

In general, Git does not provide a way to ignore changes to tracked files, so alternate solutions are recommended.

In your case though, that might work as a temporary workaround.
Don't forget a --no-skip-worktree to restore the folder index.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250