如何从 Git 存储库中删除文件? [英] How can I delete a file from a Git repository?
问题描述
我已将名为 "file1.txt"
的文件添加到 Git 存储库.之后,我提交了它,添加了几个名为 dir1
和 dir2
的目录,并将它们提交到 Git 存储库.
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
, and committed them to the Git repository.
现在当前存储库有 "file1.txt"
、dir1
和 dir2
.如何删除 "file1.txt"
而不影响其他人,例如 dir1
和 dir2
?
Now the current repository has "file1.txt"
, dir1
, and dir2
. How can I delete "file1.txt"
without affecting others, like dir1
and dir2
?
推荐答案
使用 git rm
.
如果您想从 Git 存储库和文件系统中删除文件,请使用:
If you want to remove the file from the Git repository and the filesystem, use:
git rm file1.txt
git commit -m "remove file1.txt"
但是如果您只想从 Git 存储库中删除文件而不是从文件系统中删除它,请使用:
But if you want to remove the file only from the Git repository and not remove it from the filesystem, use:
git rm --cached file1.txt
git commit -m "remove file1.txt"
并将更改推送到远程仓库
And to push changes to remote repo
git push origin branch_name
这篇关于如何从 Git 存储库中删除文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!