Why are there two ways to unstage a file in Git?

后端 未结 12 1841
刺人心
刺人心 2020-11-27 23:48

Sometimes git suggests git rm --cached to unstage a file, sometimes git reset HEAD file. When should I use which?

EDIT:

D:\         


        
12条回答
  •  一整个雨季
    2020-11-28 00:48

    These 2 commands have several subtle differences if the file in question is already in the repo and under version control (previously committed etc.):

    • git reset HEAD unstages the file in the current commit.
    • git rm --cached will unstage the file for future commits also. It's unstaged untill it gets added again with git add .

    And there's one more important difference:

    • After running git rm --cached and push your branch to the remote, anyone pulling your branch from the remote will get the file ACTUALLY deleted from their folder, even though in your local working set the file just becomes untracked (i.e. not physically deleted from the folder).

    This last difference is important for projects which include a config file where each developer on the team has a different config (i.e. different base url, ip or port setting) so if you're using git rm --cached anyone who pulls your branch will have to manually re-create the config, or you can send them yours and they can re-edit it back to their ip settings (etc.), because the delete only effects people pulling your branch from the remote.

提交回复
热议问题