[git] 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?

This question is related to git github bitbucket delete-file git-bash

The answer is


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.


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.


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.


Go to your git Directory then type the following command: rm -rf <Directory Name>

After Deleting the directory commit the changes by: git commit -m "Your Commit Message"

Then Simply push the changes on remote GIT directory: git push origin <Branch name>


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.


You can delete the folder locally and then push, as follow:

git rm -r folder_name
git commit -m "your commit"
git push origin master

A combination of the 2 answers worked for me

git rm -r one-of-the-directories
git commit . -m "Remove duplicated directory"
git push

if it still shows some warning, remove the files manually

git filter-branch --tree-filter 'rm -rf path/to/your/file' HEAD
git push

I usually use git add --all to remove files / folders from remote repositories

rm -r folder_name
git add --all
git commit -m "some comment"
git push origin master

master can be replaced by any other branch of the repository.


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.


First git command need to know who you are before deleting anything

  1. git init
  2. git config user.name "someone"
  3. git config user.email "[email protected]"
  4. git rm -r
  5. git commit -m "deleting dir"
  6. git push origin master

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.


try rm -r -f <folder name> or rm -r --force <folder name> - forcefully delete a folder


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


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.


You can try this: git rm -rf <directory_name>

It will force delete the directory.


Examples related to git

Does the target directory for a git clone have to match the repo name? Git fatal: protocol 'https' is not supported Git is not working after macOS Update (xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools) git clone: Authentication failed for <URL> destination path already exists and is not an empty directory SSL_connect: SSL_ERROR_SYSCALL in connection to github.com:443 GitLab remote: HTTP Basic: Access denied and fatal Authentication How can I switch to another branch in git? VS 2017 Git Local Commit DB.lock error on every commit How to remove an unpushed outgoing commit in Visual Studio?

Examples related to github

Does the target directory for a git clone have to match the repo name? Issue in installing php7.2-mcrypt How can I switch to another branch in git? How to draw checkbox or tick mark in GitHub Markdown table? How to add a new project to Github using VS Code git clone error: RPC failed; curl 56 OpenSSL SSL_read: SSL_ERROR_SYSCALL, errno 10054 How to add empty spaces into MD markdown readme on GitHub? key_load_public: invalid format git - remote add origin vs remote set-url origin Cloning specific branch

Examples related to bitbucket

How to markdown nested list items in Bitbucket? Your configuration specifies to merge with the <branch name> from the remote, but no such ref was fetched.? Bitbucket git credentials if signed up with Google What I can do to resolve "1 commit behind master"? Bitbucket fails to authenticate on git pull Change remote repository credentials (authentication) on Intellij IDEA 14 git: updates were rejected because the remote contains work that you do not have locally How do I push a local repo to Bitbucket using SourceTree without creating a repo on bitbucket first? Clone private git repo with dockerfile How to move git repository with all branches from bitbucket to github?

Examples related to delete-file

Ansible: How to delete files and folders inside a directory? Can I delete data from the iOS DeviceSupport directory? Python3 project remove __pycache__ folders and .pyc files How do I delete files programmatically on Android? Delete files or folder recursively on Windows CMD How to delete a folder and all contents using a bat file in windows? How to delete a file or folder? How to remove a directory from git repository? Linux delete file with size 0 Java 'file.delete()' Is not Deleting Specified File

Examples related to git-bash

"Permission Denied" trying to run Python on Windows 10 Adding Git-Bash to the new Windows Terminal How do I use Bash on Windows from the Visual Studio Code integrated terminal? How can I change the user on Git Bash? Change drive in git bash for windows Running .sh scripts in Git Bash Set an environment variable in git bash Python not working in the command line of git bash Change the location of the ~ directory in a Windows install of Git Bash Username and password in command for git push