0

I am trying to push changes to my repository:

-My Folder
  --Chapter 1
  --Chapter 2
  --Chapter 10
    --my_new_file.py

I added new folder Data a long with changing the code in my_new_file.py:

-My Folder
  --Chapter 1
  --Chapter 2
  --Chapter 10
    --my_new_file.py
  --Data

I tried this:

git rm -r --cached Data
git commit -m "Removed folder from repository"
git push origin master

But I get this:

fatal: pathspec 'Data' did not match any files

As I understand from this answer it's the correct way to do it, what am I doing wrong? I want the folder Data to stay in my local folder but not be pushed to my repository.

Jonas Palačionis
  • 2,081
  • 1
  • 10
  • 25
  • Are any files within `Data` tracked in git? If not, then git doesn't know/care about the folder. In essence git only cares about files. Folders only matter to it insofar as they contain tracked files. So you likely can't remove the folder from git because it's not tracked by git. – Joachim Sauer Nov 12 '20 at 19:52
  • Yes, there are 200MB of data which I do not want to be pushing in my repository. – Jonas Palačionis Nov 12 '20 at 19:54
  • 1
    Note that if the folder was already committed once, you will still push the data because it is contained in previous commits. – knittl Nov 12 '20 at 20:14

2 Answers2

1

In order for git to be aware that something lies in Data/, you would first need to stage the folder, and create a commit.
If you haven't done so (it looks like you didn't), no file in Data/ is tracked, and you can push straight away without running git rm --cached.


You can take one extra step to make sure that Data/ doesn't accidentally land in your repository : you can edit your .gitignore file and add /Data to it.

That way : git status will stop listing Data altogether, and you won't be able to add those files in future commits (not without an explicit action at least).

LeGEC
  • 29,595
  • 2
  • 37
  • 78
  • I've created `gitignore`, that worked out, thanks. Is it a good practice to be pushing `.gitignore` file to my repository or can I put `.gitignore` inside the `gitignore`? – Jonas Palačionis Nov 12 '20 at 20:16
  • 1
    `.gitgnore` *is* intended to be committed and shaared with your repository, so yes, push it to your repository. That way, anyone who clones your repo will also have their `Data/` directory ignored (if they have any). – LeGEC Nov 12 '20 at 20:18
  • 1
    That being said, there are other ways to ignore files in a git repo : see for example https://stackoverflow.com/q/22906851/86072 for more details – LeGEC Nov 12 '20 at 20:18
1

The way Git works is that you form a commit in two stages:

  1. First you tell the index (also called the cache or the staging area) that you want this file to be taken account of in the next commit. Do that with every such file.

  2. Second you say "form that commit" based on what the index was told to do.

And the only things that get pushed (or fetched or pulled or whatever) are commits.

So if you never added any of the files in Data to the index (with git add), then they won't go into the next commit, so they won't get pushed, so you're fine. Git may know (and tell you, when you say git status) that Data exists, but if it doesn't say (when you say git status) that files in Data are staged to be committed, then they are not.

Another way to know what's in the index is to say git ls-files --cache. If you don't see any Data files listed, then when you form the next commit, it won't contain any Data files.

matt
  • 447,615
  • 74
  • 748
  • 977