When should I use rm, git rm, git rm

I am learning git, but I am confused by different ways of staging and committing files. To wrap my head around this I'm using a metaphor of directories: working directory, staging directory, commit directory.

  • If I rm a file from my working directory, it only removes it only from my working directory. It that correct?
  • If I git rm a file from my working directory, it removes it from all three directories. Correct?
  • If I git rm --cached a file, it is removes the file from my staged and commit directories, but leave it in my working directory?
  • If I have updated, added, and deleted files from my working directory, and do git add . , then git status shows staged files that have been added, deleted, and updated. What happens when I commit? Are the deleted files removed from the Commit directory? If I roll back to that commit later, will those deleted files reappear?
  • Any help to better understand these concepts would be appreciated -thanks!


    Tweak your understanding of the staging area (also known as the index or cache) and the --cached option. The documentation for git rm states

    --cached
    

    Use this option to unstage and remove paths only from the index. Working tree files, whether modified or not, will be left alone.

    Running down your list gives

  • rm file — remove file from the work directory only
  • git rm — remove file from the work directory and from the staging area, but not yet part of the history (repository, “commit directory”)
  • git rm --cached — remove from staging area but neither the work directory nor the history
  • git add . in the presence of modifications, new files, and deleted files — git will record the modifications and new unignored files in the cache. ( git add will behave differently with certain options.)
  • The --cached option to various git commands causes them to operate on the index or at least with respect to the index.

    git add and git rm take changes from the work directory to the index or cache. Think of these commands as building up your next commit a piece at a time.

    After you are happy with what's in the index, move changes from the index to the repository with git commit .

    Most of the time, what you want is the simple sequence git rm file followed by git commit to stop tracking file at the current point in your history.

    链接地址: http://www.djcxy.com/p/23388.html

    上一篇: 让.gitignore忽略除几个文件以外的所有内容

    下一篇: 什么时候应该使用rm,git rm,git rm