-4

lately, I've been learning git, then I came to the git rm that kind of confused me, I kept digging in and understood most of it, but there is a question still baffling me, what will happen if I deleted a file using the normal bash rm command without unstaging the removed file and then commit it? will the commit keep the file?

I've tried to do that myself but I couldn't see and understand the results.

1 Answers1

1

Git builds new commits from whatever is in Git's index (aka staging-area).

If you remove a file from your working tree, but leave the copy of the file in the index, and then make a new commit, the new commit contains the file, because the file is there in Git's index. The file continues not to exist in your working tree, because committing does not update your working tree.

Note that running git add -u or git add file will remove the file from Git's index. The next commit will now lack the file, i.e., the difference between the current commit—which has the file—and the next commit will include "delete the given file". So it's always a question of what's in Git's index, not what's in your working tree, and from there, a question of "what does this Git command do to Git's index". Using git rm removes the file from Git's index, but—rather peculiarly—so can git add!

torek
  • 448,244
  • 59
  • 642
  • 775
  • but `git add` is supposed to add files to the staging area not take them off of it, can you explain that more, please? – elakhdar_ayoub Aug 07 '22 at 14:24
  • 1
    @elakhdar_ayoub: See [the documentation](https://git-scm.com/docs/git-add) for the way *they* put it, but the way I like to describe it is that `git add ` means "make the index copy of match the working tree copy". That includes "working tree copy gone, so make index copy gone to match". It's weird and feels "wrong" to me so I use `git rm` instead, but it does actually *happen*. There's a `git add` flag to disable this. – torek Aug 07 '22 at 14:29
  • that is actually helpful and interesting, thank you so much for your time. – elakhdar_ayoub Aug 07 '22 at 15:16