0

In my branch A, I created a new directory myDir. I commited all changes on that branch by git add . and commit. I don't see any untracked file or directory.

Then I switch to branch B, I see untracked file myDir.

How can I remove myDir from branch B but still have it when switch to branch A ? I mean I don't want to really delete myDir from hard disk and I don't want to see untracked file myDir either on branches (other than branch A) .

I know there is git clean command, but it deletes files from hard disk, right?

Chris Maes
  • 35,025
  • 12
  • 111
  • 136
Leem.fin
  • 40,781
  • 83
  • 202
  • 354

1 Answers1

1

There is no real problem removing myDir from your disk, when you will switch back to branch A, myDir will appear again.

So you can safely use git clean (in this case, since you already committed myDir on another branch).

Chris Maes
  • 35,025
  • 12
  • 111
  • 136
  • Thanks, could you please clarify to me that does `git clean` delete file from hard disk or not? If it does deletion from hard disk how could git keep it when switching back to branch A? – Leem.fin Sep 25 '19 at 08:11
  • @Leem.fin Each commit is a hash and contains info. It describes the tree of your project with its files and their content. Even when your directory `myDir` will be deleted, git has written everything it needs to recreate it. – Romain Valeri Sep 25 '19 at 08:15
  • To simplify a little: when you `git commit`, a copy of each file is put under your `.git` directory, so after that you cannot lose the file anymore (unless you remove your `.git` directory) – Chris Maes Sep 25 '19 at 08:15
  • @RomainValeri I understand that, each commit is a hash containing info, but if the hash referenced/described file is permanentely deleted from hard disk, how can git recover it? That's my main question here. I don't understand if `git clean` delete file from hard disk, how can switching to branch A still have the file. Unless `git clean` doesn't really delete file from hard disk. – Leem.fin Sep 25 '19 at 08:17
  • @Leem.fin Why would the hash itself be deleted? We're talking about deleting the directory. The hash is inside `.git`, your directory isn't. – Romain Valeri Sep 25 '19 at 08:18
  • @ChrisMaes so, in short, you mean git copy real physical files to `.git`. Right? If so, it makes sense to me now. That's the answer I need. Thanks. – Leem.fin Sep 25 '19 at 08:18
  • @RomainValeri I was saying the hash referenced file, not the hash. My understanding is the hash is referencing a file in hard disk, like pointer. If I understand wrongly, let me know. – Leem.fin Sep 25 '19 at 08:20
  • @Leem.fin "so, in short, you mean git copy real physical files to `.git`. Right?" No, it creates a hash from the whole tree plus metadata, and stores that hash inside `.git`. But the files themselves are not stored as such in `.git`. Chris answer is fine :-) – Romain Valeri Sep 25 '19 at 08:21
  • @RomainValeri If I ask in another way: Does the hash act like a pointer/reference pointing/refering to the the physical file on hard disk? – Leem.fin Sep 25 '19 at 08:22
  • @Leem.fin No. It's an encrypted version of the data itself, it doesn't point to the real file, it incorporates its contents in a short form, which will need to be unhashed to be used. – Romain Valeri Sep 25 '19 at 08:24
  • @RomainValeri so, it is a "copy" because you said it is an encrypted version of the data under `.git`. Otherwise, how can a file permanately deleted from hard disk be available again in another branch. There must be a sort of "copy", doesn't matter encrypted version or whatever version "copy" is happening. – Leem.fin Sep 25 '19 at 08:28
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/199945/discussion-between-romainvaleri-and-leem-fin). – Romain Valeri Sep 25 '19 at 08:28