1653

I created a git repository with git init. I'd like to delete it entirely and init a new one.

jww
  • 97,681
  • 90
  • 411
  • 885
user105813
  • 17,059
  • 4
  • 21
  • 17

15 Answers15

2063

Git keeps all of its files in the .git directory. Just remove that one and init again.

If you can't find it, it's because it is hidden.

  • In Windows 7, you need to go to your folder, click on Organize on the top left, then click on Folder and search options, then click on the View tab and click on the Show hidden files, folders and drives radio button.

  • On a Mac OS:

    • Open a Terminal (via Spotlight: press CMD + SPACE, type terminal and press Enter) and run:

      defaults write com.apple.finder AppleShowAllFiles 1 && killall Finder
      

      Note: The keyboard shortcut to show hidden files in Finder is CMD + SHIFT + . so it is no longer necessary to modify the finder config this way

    • You could also type cd (the space is important), drag and drop your git repo folder from Finder to the terminal window, press return, then type rm -fr .git, then return again.

  • On Ubuntu, use shortcut Ctrl + H.

double-beep
  • 5,031
  • 17
  • 33
  • 41
Kristof Provost
  • 26,018
  • 2
  • 26
  • 28
  • 25
    Note: After you have removed the hidden .git file, you should enter `defaults write com.apple.finder AppleShowAllFiles NO && killall Finder` to hide the hidden files once again. – CodeBiker May 29 '16 at 01:12
  • 22
    To show/hide files on macOS (in Finder): `cmd + shift + .` This was a top result on Google for me so people will probably find it a lot even though it's old. – ludvigeriksson May 08 '17 at 12:54
  • 12
    @Van those are bash flags (options), and they can go in whatever order you'd like :) – De Novo Mar 04 '18 at 20:00
  • That solution was not enough when I added an iOS project as submodule. It appeared as a red folder with "iOSapp @ 1d12345" that couldn't be accesses nor could I push new changes (it said always it matches master already). I used the answer with "find . -type f | grep -i "\.git" | xargs rm" that removed whatever created the problem and now it works. – André Dec 10 '20 at 07:32
  • Hi I just do it accidentally and all my work vanishes. Is there a way to undo this? – Abdullah Saud Apr 27 '21 at 11:27
  • Cant find this mentioned but if you want to remove the git repository from in windows 10 cmd then use ```rmdir /s .git``` when inside the directory that contains the git repository. – eHarazi Jun 14 '22 at 16:25
1022

If you really want to remove all of the repository, leaving only the working directory then it should be as simple as this.

rm -rf .git

The usual provisos about rm -rf apply. Make sure you have an up to date backup and are absolutely sure that you're in the right place before running the command. etc., etc.

CB Bailey
  • 755,051
  • 104
  • 632
  • 656
  • 15
    And you can use this on Windows too, in git shell – TheTechGuy Nov 14 '18 at 15:37
  • 1
    It repeatedly says it can't: rm: `cannot remove 'Rift/.git/objects/pack': Directory not empty`, and yes I ran rm -rf – Vendetta Nov 16 '20 at 03:23
  • 1
    Side note, -rf is two options joined together: -r for recursive removal, -f to force the action – nCardot Apr 21 '21 at 15:39
  • Hi I just do it accidentally and all my work vanishes. Is there a way to undo this? – Abdullah Saud Apr 27 '21 at 11:27
  • @Abdullah Saud No way you could undo it when running that command. The warning should be put earlier. – MaXi32 Jan 14 '23 at 04:49
  • For Windows PowerShell users, run `Remove-Item ".git" -Recurse -Force` [doc](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/remove-item?view=powershell-7.3) – Max Ma Jun 14 '23 at 21:34
70

If you want to delete all .git folders in a project use the following command:

find . -type f | grep -i "\.git" | xargs rm

This will also delete all the .git folders and .gitignore files from all subfolders

ejazz
  • 2,458
  • 1
  • 19
  • 29
31

after cloning the repo

cd /repo folder/

to go to the file directory then

ls -a

to see all files hidden and unhidden

.git .. .gitignore .etc

if you like you can check the repo origin

git remote -v

now delete .git which contains everything about git

rm -rf .git

after deleting, you would discover that there is no git linked check remote again

git remote -v

now you can init your repository with

git init
git add README.md
git commit -m "first commit"
git remote add origin https://github.com/Leonuch/flex.git
git push -u origin main
Anatoly
  • 20,799
  • 3
  • 28
  • 42
leouonline
  • 328
  • 3
  • 6
  • 2
    This is a duplicate of [an existing answer](https://stackoverflow.com/a/1213449/68587). All the other steps add nothing to the answer. – John Kugelman Apr 04 '20 at 00:49
16

Alternative to killing TortoiseGit:

  • Open the TortoiseGit-Settings (right click to any folder, TortoiseGit → Settings)
  • Go to the Icon Overlays option.
  • Change the Status Cache from Default to None
  • Now you can delete the directory (either with Windows Explorer or rmdir /S /Q)
  • Set back the Status Cache from None to Default and you should be fine again...
11684
  • 7,356
  • 12
  • 48
  • 71
eckes
  • 64,417
  • 29
  • 168
  • 201
11

Where $GIT_DIR is the path to the folder to be searched (the git repo path), execute the following in terminal.

find $GIT_DIR -name *.git* -ok rm -Rf {} \;

This will recursively search for any directories or files containing ".git" in the file/directory name within the specified Git directory. This will include .git/ and .gitignore files and any other .git-like assets. The command is interactive and will ask before removing. To proceed with the deletion, simply enter y, then Enter.

Scorpius
  • 999
  • 1
  • 10
  • 22
5

To fully delete the .git repository in your computer (in Windows 8 and above):

  1. The .git repository is normally hidden in windows
  2. So you need to mark the "hidden items" to show the hidden folders
  3. At the top site of you directory you find "view" option
  4. Inside "view" option you find "hidden items" and mark it
  5. Then you see the .git repository then you can delete it
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
squal
  • 185
  • 3
  • 14
4

I tried:

rm -rf .git and also

Git keeps all of its files in the .git directory. Just remove that one and init again.

Neither worked for me. Here's what did:

  • Delete all files except for .git
  • git add . -A
  • git commit -m "deleted entire project"
  • git push

Then create / restore the project from backup:

  • Create new project files (or copy paste a backup)
  • git add . -A
  • git commit -m "recreated project"
  • git push
P.Brian.Mackey
  • 43,228
  • 68
  • 238
  • 348
4

you can use :

git remote remove origin

to remove a linked repo then:

git remote add origin

to add new one

MonirRouissi
  • 549
  • 8
  • 7
3

You can create an alias for it. I am using ZSH shell with Oh-my-Zsh and here is an handy alias:

# delete and re-init git
# usage: just type 'gdelinit' in a local repository
alias gdelinit="trash .git && git init"

I am using Trash to trash the .git folder since using rm is really dangerous:

trash .git

Then I am re-initializing the git repo:

git init
spongebob
  • 8,370
  • 15
  • 50
  • 83
Ahmad Awais
  • 33,440
  • 5
  • 74
  • 56
2

No worries, Agreed with the above answers:

But for Private project, please follow the steps for Gitlab:

  1. Login to your account
  2. Click on Settings -> General
  3. Select your Repository (that you wants to delete)
  4. Click on 'Advanced' on the bottom-most
  5. Click on 'Remove Project'
  6. You will be asked to type your project name

    This action can lead to data loss. To prevent accidental actions we ask you to confirm your intention. Please type 'sample_project' to proceed or close this modal to cancel.

  7. Now your project is deleted successfully.

Deepak Keynes
  • 2,291
  • 5
  • 27
  • 56
0

In windows:

  1. Press Start Button
  2. Search Resource Monitor
  3. Under CPU Tab -> type .git -> right click rundll32 and end process

Now you can delete .git folder

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
0

Windows cmd prompt: (You could try the below command directly in windows cmd if you are not comfortable with grep, rm -rf, find, xargs etc., commands in git bash )

Delete .git recursively inside the project folder by the following command in cmd:

FOR /F "tokens=*" %G IN ('DIR /B /AD /S .git') DO RMDIR /S /Q "%G"

SridharKritha
  • 8,481
  • 2
  • 52
  • 43
0

rm -rf .git worked for me I did a ls -a to see all hidden files/folders first.

-2

true,like mine was stored in USERS,so had to open USERS go to View on you upper left find Options,open it and edit folders'view options in view still to display hidden files/folders,all your folders will be displayed and you can deleted the repo manually,remember to hide the files/folders once done with the delete.