Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove files from remote branch in git

Tags:

git

I accidentally pushed some files to my remote Git branch. What is the best way to remove those particular files from the remote branch?

like image 254
Shaolin Avatar asked Nov 30 '22 00:11

Shaolin


2 Answers

Since you do not want to push those files to the remote server, but want to keep the copies locally, your best bet is to do git rm with a --cached flag.

Basically, do this:

git rm --cached some/filename.ext
git rm --cached -r some/directory/

and then commit and push your changes back using

git commit -m "removing redundant files"

From the manpage for git rm:

--cached

Use this option to unstage and remove paths only from the index. Working tree files, whether modified or not, will be left alone.

like image 63
Anshul Goyal Avatar answered Dec 03 '22 22:12

Anshul Goyal


Pull the changes from remote, do a git rm on your local repository, commit the changes, and again push to the remote. The files will be deleted.

You can check this earlier question on Stack Overflow: How can I delete files in the remote repository?

like image 34
Diptendu Avatar answered Dec 03 '22 23:12

Diptendu