lakshmipathi lakshmipathi - 3 months ago 9
Git Question

How can I delete a file from git repo?

I have added a file named

"file1.txt"
to git repo. After that I committed it, added a couple directories called
dir1
and
dir2
, and committed them to git repo.

Now the current repo has
"file1.txt"
,
dir1
and
dir2
.

How can I delete
"file1.txt"
without affecting others like
dir1
and
dir2
?

Answer

Use git rm:

git rm file1.txt
git commit -m "remove file1.txt"

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