Asad Moosvi
Asad Moosvi

Reputation: 490

When is the file removed from the index when you run git rm?

When you delete a file from your project by running the git rm command, when is the file actually removed from the index? Is it removed when you commit the deletion or is it removed when git rm is run?

From what I understand, a git rm involves the following steps:

  1. Remove file from index
  2. Remove file from working directory
  3. Commit file deletion

I'm just not sure whether the file is removed in the beginning from the index or when the deletion is committed. Also are these steps even correct?

Upvotes: 0

Views: 37

Answers (2)

torek
torek

Reputation: 487993

It happens immediately, but there is no commit at this point. That is, only steps 1 and 2 apply.

A good way to think of the index is that it's a third copy of everything—or really, a second copy, and the work-tree is the third. That is, at any time, you have:

  • A current commit, aka (also known as) HEAD or sometimes @. Since this is a commit, it cannot be changed at all.
  • A proposed next commit, aka the index, aka the staging area, aka the cache. (Why three names? Well, it's Git. :-) ) This is not—not yet—a commit, so you can change it. But it is in a special Git form, where it is really hard to view or work with any of the files stored in it.
  • The work-tree, which has the files in a form where you (and the rest of your computer) can work with them. The work-tree can also have untracked files in it (and some of those can be ignored as well as untracked)—the index cannot contain untracked files, because by definition, whatever is in the index, is tracked.

Note that you can copy files in both directions here: from HEAD to index, from index to work-tree, or from work-tree to index. But since commits can never be changed you can't copy from index back into HEAD. (What you do instead is make a new commit—copy from index to new commit. Of course, that new commit is permanent.)

Upvotes: 4

Ganesh
Ganesh

Reputation: 3428

When you run git rm it removes file from the index & working directory.

Reference - git-rm

Upvotes: 0

Related Questions