Why use 'git rm' to remove a file instead of 'rm'?

前端 未结 7 1093
不思量自难忘°
不思量自难忘° 2020-12-02 04:39

On SVN, removing something from the filesystem directly (rather than using svn) created a load of headaches.

I haven\'t found this to be an issue when using gi

相关标签:
7条回答
  • 2020-12-02 04:57

    Adding to Andy's answer, there is additional utility to git rm:

    1. Safety: When doing git rm instead of rm, Git will block the removal if there is a discrepancy between the HEAD version of a file and the staging index or working tree version. This block is a safety mechanism to prevent removal of in-progress changes.

    2. Safeguarding: git rm --dry-run. This option is a safeguard that will execute the git rm command but not actually delete the files. Instead it will output which files it would have removed.

    0 讨论(0)
  • 2020-12-02 05:00

    Removing files using rm is not a problem per se, but if you then want to commit that the file was removed, you will have to do a git rm anyway, so you might as well do it that way right off the bat.

    Also, depending on your shell, doing git rm after having deleted the file, you will not get tab-completion so you'll have to spell out the path yourself, whereas if you git rm while the file still exists, tab completion will work as normal.

    0 讨论(0)
  • 2020-12-02 05:01

    git rm will remove the file from the index and working directory ( only index if you used --cached ) so that the deletion is staged for next commit.

    0 讨论(0)
  • 2020-12-02 05:03

    If you just use rm, you will need to follow it up with git add <fileRemoved>. git rm does this in one step.

    You can also use git rm --cached which will remove the file from the index (staging it for deletion on the next commit), but keep your copy in the local file system.

    0 讨论(0)
  • 2020-12-02 05:08

    Remove files from the index, or from the working tree and the index. git rm will not remove a file from just your working directory.

    Here's how you might delete a file using rm -f and then remove it from your index with git rm

    $ rm -f index.html
    $ git status -s
     D index.html
    $ git rm index.html
    rm 'index.html'
    $ git status -s
    D  index.html
    

    However you can do this all in one go with just git rm

    $ git status -s
    $ git rm index.html
    rm 'index.html'
    $ ls
    lib vendor
    $ git status -s
    D  index.html
    
    0 讨论(0)
  • 2020-12-02 05:12

    However, if you do end up using rm instead of git rm. You can skip the git add and directly commit the changes using:

    git commit -a

    0 讨论(0)
提交回复
热议问题