How can I delete a file from a Git repository?

后端 未结 24 2257
無奈伤痛
無奈伤痛 2020-11-22 13:41

I have added a file named \"file1.txt\" to a Git repository. After that, I committed it, added a couple of directories called dir1 and dir2

24条回答
  •  不要未来只要你来
    2020-11-22 14:16

    If you want to delete the file from the repo, but leave it in the the file system (will be untracked):

    bykov@gitserver:~/temp> git rm --cached file1.txt
    bykov@gitserver:~/temp> git commit -m "remove file1.txt from the repo"
    

    If you want to delete the file from the repo and from the file system then there are two options:

    1. If the file has no changes staged in the index:

      bykov@gitserver:~/temp> git rm file1.txt
      bykov@gitserver:~/temp> git commit -m "remove file1.txt"
      
    2. If the file has changes staged in the index:

      bykov@gitserver:~/temp> git rm -f file1.txt
      bykov@gitserver:~/temp> git commit -m "remove file1.txt"
      

提交回复
热议问题