Search code examples
gitgit-rm

What is the difference between git rm and git rm -f?


I am reading book Git Pro and it says:

To remove a file from Git, you have to remove it from your tracked files (more accurately, remove it from your staging area) and then commit. The git rm command does that, and also removes the file from your working directory so you don’t see it as an untracked file the next time around.

And the next paragraph talks about git rm -f:

If you modified the file and added it to the staging area already, you must force the removal with the -f option. This is a safety feature to prevent accidental removal of data that hasn’t yet been recorded in a snapshot and that can’t be recovered from Git.

What I don't understand is in both paragraph, they are talking about removing file from the same area(staging I guess, place where git add screenshot after git add .).

If both command is used to remove file from staging area, then what is the difference?


Solution

  • git rm -f (or --force) overrides the up-to-date check. If no -f is supplied, git rm will refuse to remove a file that's modified since last commit, but git rm -f will proceed removal.

    $ git init
    Initialized empty Git repository in /home/ibug/test/.git/
    $ touch foo
    $ git add foo
    $ git rm foo
    error: the following file has changes staged in the index:
        foo
    (use --cached to keep the file, or -f to force removal)
    $ git commit --message "Test foo"
    [master (root-commit) 1234567] Test foo
     1 file changed, 0 insertions(+), 0 deletions(-)
     create mode 100644 foo
    $ git rm foo
    rm 'foo'
    $