How can I delete a single directory containing files from a Git repository?
16 Answers
Remove directory from Git and local
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
To remove this directory from Git, but not delete it entirely from the filesystem (local):
git rm -r --cached myFolder
17 Comments
git rm -r myFolder it worked, but also deleted everything from "MyFolder" directory. Had to revert everything in the "MyFolder" directory and then commit.git rm -r --cached myFoldergit rm -r --cached directory/only removes from git tracked, but the repository structure remains.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

6 Comments
rm -rf FolderName; git add -A; git commit -m 'removed FolderName ?If, for some reason, what karmakaze said doesn't work, you could try deleting the directory you want using or with your file system browser (ex. In Windows File Explorer). After deleting the directory, issuing the command:
git add -A
and then
git commit -m 'deleting directory'
and then
git push origin master.
3 Comments
I already had committed the folder before and want to remove the directory in the history as well.
I did the following:
Add folder to .gitignore:
echo Folder_Name/ >> .gitignore
Remove from all commits:
git filter-branch --index-filter 'git rm -rf --cached --ignore-unmatch Folder_Name/' --prune-empty --tag-name-filter cat -- --all
remove the refs from the old commits:
git for-each-ref --format="%(refname)" refs/original/ | xargs -n 1 git update-ref -d
Ensure all old refs are fully removed
rm -Rf .git/logs .git/refs/original
Perform a garbage collection
git gc --prune=all --aggressive
push you changes to the online repository:
git push
You are done here.
But you can to the following to push all the changes to all branches with: But be careful with this command!
git push origin --all --force
git push origin --tags --force
After that the folder was removed from git, but was not deleted from local disk.
1 Comment
You can try this:
git rm -rf <directory_name>
It will force delete the directory.
1 Comment
If you remove the files in the directory (with git rm as the other answers explain), then the directory no longer exists as far as git is concerned. You cannot commit an empty directory, nor can you remove one.
This is unlike subversion where you have to explicitly svn rm emptyfolder/, and is incidentally why the man page for git describes itself as "the stupid content tracker"
An answer on "How do I add an empty directory to a git repository" links to the FAQ on this subject:
Currently the design of the git index (staging area) only permits files to be listed, and nobody competent enough to make the change to allow empty directories has cared enough about this situation to remedy it.
Directories are added automatically when adding files inside them. That is, directories never have to be added to the repository, and are not tracked on their own.
You can say "
git add <dir>" and it will add files in there.If you really need a directory to exist in checkouts you should create a file in it. .gitignore works well for this purpose; you can leave it empty, or fill in the names of files you expect to show up in the directory.
Comments
for deleting Empty folders
i wanted to delete an empty directory(folder) i created, git can not delete it, after some research i learned Git doesn't track empty directories. If you have an empty directory in your working tree you should simply removed it with
rm -r folderName
There is no need to involve Git.
Comments
You can use Attlasian Source Tree (Windows) (https://www.atlassian.com/software/sourcetree/overview). Just select files from tree and push button "Remove" at the top. Files will be deleted from local repository and local git database. Then Commit, then push.
Comments
One of my colleague suggested BFG Repo-Cleaner which I think powerful. It is not only delete unwanted data but also clean your repository from any related commit information.
Comments
First git command need to know who you are before deleting anything
- git init
- git config user.name "someone"
- git config user.email "[email protected]"
- git rm -r
- git commit -m "deleting dir"
- git push origin master
Comments
To add new directory:
mkdir <YOUR-DIRECTORY>
But now Git is not aware by this new directory, because Git keep tracks of file not directories DIRECTORY
git status
Git won't be aware with the change we've made, so we add hidden .keep file to make Git aware by this new change.
touch /YOUR-directory/.keep
Now, if you hit git status Git will be aware with the changes.
And If you want to delete the directory, you should use this command.
rm -r <YOUR-DIRECTORY>
And If you checked by using git status, you will see the directory has been removed.
Comments
If you are deleting directory from GitHub web application, you will find a delete option. However, after that it will give you list of files being deleted, with commit option at the bottom. Do remember to commit it. Otherwise, directory will not be deleted.
Reference - https://docs.github.com/en/repositories/working-with-files/managing-files/deleting-files-in-a-repository
.gitignore. It has returnedfatal: pathspec 'venv/' did not match any files. I had to take the folder out of.gitignorefirst, thengit add <folder>, then runit rm -r --cached <folder>and then add it back to.gitignore. It took me a some time to find out why it did not work at first place.