Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove a directory from git repository?

I have 2 directories on my GitHub repository. I'd like to delete one of them. How could I do that without deleting and re-creating entire repository?

like image 573
Sahat Yalkabov Avatar asked Jun 10 '11 23:06

Sahat Yalkabov


People also ask

How do I remove a folder from my git repository?

Browse to the directory in the repository and branch that you want to delete. In the top-right corner, click "…", and then Delete directory. Review the list of files. Depending on your permissions and the branch protection rules, choose to either commit the change directly or propose the change using a pull request.

How do I delete a directory in git but not locally?

Execute the following command: git rm --cached path/to/file . Git will list the files it has deleted. The --cached flag should be used if you want to keep the local copy but remove it from the repository.

How do I remove a directory from GIT stage?

You may use the –r option in the Git rm command for removing a folder. Any files contained in the folder are also removed. The –r option in rm command allows recursive removal if you provide a leading directory name.


2 Answers

Remove directory from git and local

You could checkout 'master' with both directories;

git rm -r one-of-the-directories // This deletes from filesystem git commit . -m "Remove duplicated directory" git push origin <your-git-branch> (typically 'master', but not always) 

Remove directory from git but NOT local

As mentioned in the comments, what you usually want to do is remove this directory from git but not delete it entirely from the filesystem (local)

In that case use:

git rm -r --cached myFolder 
like image 196
karmakaze Avatar answered Oct 13 '22 20:10

karmakaze


To remove folder/directory only from git repository and not from the local try 3 simple commands.


Steps to remove directory

git rm -r --cached FolderName git commit -m "Removed folder from repository" git push origin master 

Steps to ignore that folder in next commits

To ignore that folder from next commits make one file in root folder (main project directory where the git is initialized) named .gitignore and put that folder name into it. You can ignore as many files/folders as you want

.gitignore file will look like this

/FolderName 

remove directory

like image 41
Suresh Karia Avatar answered Oct 13 '22 19:10

Suresh Karia