1382

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?

Suresh Karia
  • 14,742
  • 18
  • 63
  • 83
Sahat Yalkabov
  • 29,198
  • 40
  • 103
  • 171

16 Answers16

2607

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
Reed
  • 13,861
  • 6
  • 61
  • 102
karmakaze
  • 30,051
  • 1
  • 28
  • 30
  • 10
    I had same exact situation. When I tried `git rm -r myFolder` it worked, but also deleted everything from "MyFolder" directory. Had to revert everything in the "MyFolder" directory and then commit. – Justin Oct 25 '12 at 13:12
  • 255
    @Justin To only remove from git and leave filesystem as-is, use `git rm -r --cached myFolder` – karmakaze Nov 16 '12 at 06:28
  • how do you remove it from the github for web? – a coder Apr 04 '16 at 16:28
  • 1
    @acoder don't know of a direct web way, there might be an API, otherwise clone the repo `git rm -r myFolder` and push – karmakaze Apr 05 '16 at 00:22
  • 1
    figured it out, click the trashcan – a coder Apr 05 '16 at 04:32
  • 1
    Be aware that if you are working on a shared repo by others, despite you run the "--cached" version, they will get that folder removed from their local copy at the moment they pull new changes. Refer to this explanation about that https://gist.github.com/scy/6636390 – Leonardo Brambilla Apr 29 '16 at 12:28
  • And use `git rm -r --cached *` to delete everything – H A Jun 11 '16 at 05:43
  • how can chose from witch branch? – Mahdi Jun 21 '16 at 05:55
  • `git rm -r --cached myFolder` will stage the removal on the current branch which then still needs to be committed & pushed – karmakaze Jun 22 '16 at 17:51
  • 1
    @karmakaze fatal: pathspec 'myFolder' did not match any files.But I can see 'myFolder' in repo with 'git ls-tree --full-tree -r HEAD' command. – Dr.jacky Jul 11 '16 at 10:30
  • 38
    Error message I get is: fatal: pathspec 'directory' did not match any files. – user2441441 Sep 08 '16 at 21:14
  • 5
    @user2441441 I had this error poping up when I was trying to remove a folder/file that was not added to the repo at all – lukeatdesignworks Oct 13 '16 at 13:36
  • @lukeatdesignworks I had the same error. Apparently removing all files from the directory removed the directory itself from version control, so trying to remove that directory gave the "fatal: pathspec 'directory' did not match any files" error. – esdot Jan 05 '17 at 17:24
  • .gitignore has no effect when I add exclusions after running `git rm -r --cached to_be_excluded_dir`. Everything is added again. – codezombie Feb 04 '18 at 10:34
  • 4
    `git rm -r --cached directory/`only removes from git tracked, but the repository structure remains. – Néstor Aug 29 '18 at 16:57
  • I think the answer is very good, however it would be more intuitive if youd first mention how to remove only from the git. I ended up deleting my folder because I thought the question is clear that this is only concerning git – dieHellste Oct 23 '19 at 10:34
  • This helped me a lot, great answer! – Kumar Abhinav Dec 18 '19 at 19:14
  • 1
    `fatal: pathspec 'web/assets/2d40d8f2/' did not match any files` will be thrown when you use the `git rm --cached web/assets/2d40d8f2/` – Reborn Mar 30 '20 at 17:32
  • What does the `.` signifiy in `git commit . -m "Remove duplicated directory"`? – vineeshvs Apr 20 '20 at 07:34
  • `.` (aka dot/period) signifies the current directory. I believe it works on all platforms (but I'm less certain of Windows versions). – karmakaze Apr 23 '20 at 11:20
  • @lukeatdesignworks You would get this error if the directory is empty as well. Because, by default git ignores empty directories. – Faizi Dec 27 '20 at 10:30
341

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

Suresh Karia
  • 14,742
  • 18
  • 63
  • 83
  • 1
    You will also need to do the git rm -r --cached FolderName git commit -m "Removed folder from repository" on your other computer and then do a pull to keep it in synch. – Simon Oct 06 '15 at 17:07
  • 1
    Will this delete the directory from the point it was added to repository till now? Or is it just to remove the directory and recommit it to the .git repo? – alpha_989 Jan 21 '18 at 18:15
  • Why not just do a `rm -rf FolderName; git add -A; git commit -m 'removed FolderName` ? – alpha_989 Jan 21 '18 at 18:18
  • @Matt West Glad it helped – Suresh Karia Aug 02 '18 at 08:03
  • 1
    Great! Working! –  Nov 20 '18 at 17:42
  • 2
    Well explained, nice answer – Rakshit Shah Nov 25 '18 at 18:55
  • 1
    well done. On my machine, the third step doesnt work. But it works if it is changed to $git push – Hong Aug 01 '19 at 01:57
  • Unlucky for me: robdavisprojects@Robs-MacBook-Air default % ls default.services.yml drushrc.php settings.ddev.php default.settings.php files settings.php robdavisprojects@Robs-MacBook-Air default % git rm -r --cached files fatal: pathspec 'files' did not match any files robdavisprojects@Robs-MacBook-Air default % – therobyouknow Dec 15 '19 at 13:54
  • Answer for me: folder "files" was not controlled by git, so that's all good. I can just rmdir files or rm -Rf files – therobyouknow Dec 15 '19 at 13:58
87

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.

sophros
  • 8,714
  • 5
  • 30
  • 57
cmcculloh
  • 43,791
  • 36
  • 94
  • 126
23

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

It will force delete the directory.

Breen ho
  • 1,312
  • 10
  • 21
18

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.

Community
  • 1
  • 1
dbr
  • 153,498
  • 65
  • 266
  • 333
12

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.

marc_s
  • 675,133
  • 158
  • 1,253
  • 1,388
Skyborg
  • 584
  • 9
  • 11
6

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>

anky
  • 61
  • 1
  • 5
6

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.

Ridha Rezzag
  • 2,254
  • 22
  • 26
4

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
shrikant
  • 333
  • 4
  • 7
4

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.

ettanany
  • 15,827
  • 5
  • 37
  • 56
2

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.

1

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.

thanh.h.le
  • 51
  • 3
1

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 "someone@someplace.com"
  4. git rm -r
  5. git commit -m "deleting dir"
  6. git push origin master
Zeus
  • 29
  • 2
1

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.

Martin Zeitler
  • 49,224
  • 12
  • 97
  • 156
Abo3atef
  • 2,497
  • 1
  • 31
  • 29
1

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
user3821178
  • 155
  • 1
  • 11
0

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

Arshad
  • 106
  • 2
  • 8