elakhdar_ayoub
elakhdar_ayoub

Reputation: 99

what happened if i rm a file without unstaging it?

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.

Upvotes: -4

Views: 70

Answers (1)

torek
torek

Reputation: 490058

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!

Upvotes: 1

Related Questions