Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I delete a file from a Git repository?

Tags:

git

git-rm

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.

Now the current repository has "file1.txt", dir1, and dir2. How can I delete "file1.txt" without affecting others, like dir1 and dir2?

like image 603
webminal.org Avatar asked Jan 12 '10 07:01

webminal.org


People also ask

Which command is used to remove a file from a git repository?

git rm is used to remove a file from a Git repository. It is a convenience method that combines the effect of the default shell rm command with git add .

Can we directly delete a file from GitHub?

You can delete an individual file or an entire directory in your repository on GitHub. People with write permissions can delete files or directories in a repository.

How do I delete a file from a remote branch?

Using git rm <deleted-filename> and git add <deleted-filename>. Your branch is up-to-date with 'origin/master'. It will stage the deleted file, and followed by git commit and git push will remove the file from the repository.


1 Answers

Use git rm.

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" 

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 
like image 111
Greg Hewgill Avatar answered Oct 12 '22 13:10

Greg Hewgill