When should I use rm, git rm, git rm --cached, git add

前端 未结 1 1662
遥遥无期
遥遥无期 2020-12-30 06:59

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, s

相关标签:
1条回答
  • 2020-12-30 07:43

    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.

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