2

If I am understanding the answers here correctly, git should not track an empty directory, but should not delete it locally. This is not the behavior I am experiencing.

Directory dir_foo/ exists on two computers, A and B. It is full of tracked files. On computer A, my program deletes all of the files in dir_foo/, but leaves the directory intact. I then add/commit/push the changes. On computer B, after pulling, I see that not only are all the files deleted, but dir_foo/ has been deleted locally as well on computer B! (Note that it still exists on computer A.) I think this behavior is incredibly counterintuitive. I do not want this behavior, and, for my use case, cannot specify every directory that git should not delete.

Why is this happening? How can I stop this behavior (without having to specify every empty directory that git should not delete)? Why does the behavior I am experiencing contradict the info in the link above? Thank you.

Reading past this point is not required to answer the question. The info below was requested in the comments.


Screenshots (identifying info removed):

The troublesome directory is results/. The first two screenshots show computers A and B, with the directory and a test file inside:

enter image description here

enter image description here

Next, we remove the file on computer A, leaving the directory intact, commit/push, and observe that the directory is still locally intact on A:

enter image description here

Finally, we pull on computer B, and observe that the directory has (again) been deleted locally:

enter image description here

Also, computer B is a cluster (I have little control/no sudo access). Perhaps they have some weird non-standard version of git or git settings on the cluster?

.gitignore contents:

$ cat .gitignore 
results.jld2
safe_to_graph
[redacted]_exit_results/
Environments/Recommender_Env/MSLR-WEB10K/*
!Environments/Recommender_Env/MSLR-WEB10K/csv_converter.py
!Environments/Recommender_Env/MSLR-WEB10K/pickle_file_checker.py
!Environments/Recommender_Env/MSLR-WEB10K/Fold1/
Environments/Recommender_Env/MSLR-WEB10K/Fold1/*
!Environments/Recommender_Env/MSLR-WEB10K/Fold1/vali_pickle
# Environments/Recommender_Env/Microsoft_Data_Set/.idea/
IJCAI_Async_Rec_results/
async_results/*
swarm_logs/*
zipresults/*
Environments/Recommender_Env/MQ2008/*
!Environments/Recommender_Env/MQ2008/csv_converter_MQ.py
!Environments/Recommender_Env/MQ2008/pickle_file_checker_MQ.py
!Environments/Recommender_Env/MQ2008/Fold1/
Environments/Recommender_Env/MQ2008/Fold1/*
!Environments/Recommender_Env/MQ2008/Fold1/vali_pickle
MUJOCO_LOG.TXT

Mureinik
  • 3,974
  • 11
  • 28
  • 32
MindSeeker
  • 123
  • 4
  • 3
    perhaps some pictures would help describe the issue. have you looked at the state of the repo on the server? is everything there and accounted for? – Frank Thomas Apr 12 '22 at 19:18
  • Your scenario is not what the original question asks about. I would fully expect Git to delete a dir that is empty after it *no longer* has tracked files in it. – Daniel B Apr 12 '22 at 19:34
  • @FrankThomas I uploaded screenshots; thank you. – MindSeeker Apr 12 '22 at 19:39
  • @DanielB Hm, the top comment and the top answer on that question seem to say that git should not be deleting empty directories locally. The comment says "Git does not delete empty folder" and the answer says "If you can see the directory locally in your file browser, but it disappeared from GitHub..." Am I misunderstanding? – MindSeeker Apr 12 '22 at 19:49
  • @DanielB Also, is it really expected behavior to empty a directory on computer A (empty but intact), commit/push, pull on computer B, and have git delete the directory on computer B? But when pulling on A, the directory remains intact (still empty). If that's really intended behavior, I guess that I'll have to figure out a way to deal with it, but it seems like exceptionally counterintuitive behavior to me... – MindSeeker Apr 12 '22 at 19:51
  • 1
    do you have a .gitignore file that is doing anything with 'results/'? you may want to try re-cloning the repo on B. there isn't a chance you are on two different branches are you? Git won't "delete" an empty directory, it will just sit there untracked until you either delete it from the workspace, or reset the workspace. if that directory was in multiple systems workspaces, then it will remain in all of them, but a new clone of the repo won't create that directory either. is the directory and its files on the server? – Frank Thomas Apr 12 '22 at 19:54
  • @FrankThomas Thank you for your help! I've posted the .gitignore above. A few things have the term `results` in them, but there is no `results/` anywhere (except in `zipresults/*`). Nothing in .gitignore is causing the problem, is it? I'll try re-cloning the repo and report back. Regarding your last question: The directory and its files were on both computers (see the first two screenshots above). – MindSeeker Apr 12 '22 at 20:04
  • 1
    Whats up with `zipresults`? it only appears to exist on B. you don't have a CICD pipeline that is zipping up that directory and removing it server side, do you? I'm not seeing anything standing out in your .gitignore – Frank Thomas Apr 12 '22 at 20:04
  • @FrankThomas No, no pipeline like that. It was just a directory that I was writing into using Computer B that I never needed on Computer A. – MindSeeker Apr 12 '22 at 20:05

1 Answers1

2

Git doesn't delete local directories when you delete a file. When you apply a change (e.g., by doing git pull, git rebase or even git am), and that change removes all the files from the directory, it will also remove the directory.

I'd argue that if your program deletes/modifies files in a directory, this directory probably shouldn't be tracked at all, as it's ephemeral data, not a real part of your source code. I.e., here, it looks like the entire results directory should be under .gitignore.

Mureinik
  • 3,974
  • 11
  • 28
  • 32