Open In App

How to Delete a Git Repository

Last Updated : 31 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

GAs developers and software teams are collaborating on projects, the usability of version control systems like Git to track changes and manage codebases has increased a lot. Using Git, developers can create and maintain repositories, that store the complete history and versions of projects. However, there are situations when deleting a git repository become necessary.

In this article, we will learn the steps to delete both local and Git repositories. We will ensure that the project is completely removed from the local system’s environment. Also, we will explain the process of deleting a remote repository hosted on platforms like GitHub, GitLab.

Method-1: Deleting the Repository from the Command line

When a user clones a Git repository from Github using the command git clone <url>, they get a copy of the remote repo on their local computer so that they can work on it on their current working directory where the repo got cloned without directly making changes on the remote repository.

If you want to delete a local Github Repository that was cloned from to local computer without touching or making any changes to the Remote GitHub repository then follow the commands below:

Step 1: Go into your project file

The deletion of the ‘.git’ file, will delete the .git file that contains a log of the commit history, its information, and also the remote repository address from the working directory. We can think of this deletion as when we do git init to initialize the current working directory as a Git directory, with the above command we are just reverting it back to not being a Git directory. 

And what about the files and folders in the present working directory?
This has to be deleted using the following set of commands.

cd <project_name>

rm -rf <repository_folder>.git 

Go-into-your-Project-file

What are rm and rf commands?

  • In Linux, the user can delete/remove directories using rmdir or rm, in the above case we have made use of rm which is used to remove non-empty directories, unlike rmdir which is used to remove empty directories.
  • This command is used to remove non-empty directories and all the files in the directory without being prompted if a directory or a file in the current working directory is write-protected (this case is very common when working on a forked repository from GitHub) and the user is prompted to provide Y (for yes) to confirm the deletion of the write-protected file. Now using -rf with rm is effective as it can skip part of the user being prompted every time.

Step 2: Verifying Git Repository is Deleted

To verify whether the targeted repository is deleted or not, check its status using the below command in the current directory and if the error is showing after running of command it means, we have successfully deleted a Git Repository.

git status




Step 3(Optional): ( if you want to initialize the working directory to another GitHub Repository See Additional steps below):-

Go to the directory where the project is present (Note: Don’t go inside the project file).

rm -rf <folder_name>

Verifying-Git-Repository-is-Deleted

Additional Steps:

Suppose you want to initialize (git init) a new GitHub repository, and then add it to a new remote repository and then start adding and committing to the files being added from the current working directory.

  1. Make sure that the current working directory is the directory that needs to be pushed to the open-source platform (GitHub).
  2. git init
  3. git remote add origin https://github.com/user/repo.git
  4. git push -u origin master

Method-2: Deleting the Repository from GitHub Website

Step 1: Navigate to your project

https://github.com/your-github-username/Project-Name

Tips-to-Stay-Motivated-As-a-Web-Developer

Step 2: Go to the settings option on the top right corner like the image above, and navigate down to the danger zone 

Go-to-the-settings-option

Step 3: Go and click on the delete this repository button and after which you will be prompted to make sure you are deleting and all these actions will permanently affect the repository, in the box type your-username/project-name (replace it with yours). 

You may also be prompted to type in the GitHub password.

click-on-the-delete-this-repository

Following these steps will make sure you have completely deleted the repository from the local PC as well as the GitHub site.

Deleting the File from GitHub Website

Here we are going to delete a particular file from our GitHub using Github. This feature can be useful when we have by, mistake added a few files in our project but we no longer need that.

Step 1: Navigate to your project

https://github.com/your-github-username/Project-Name

Step 2: Browse to the file in your repository that you want to delete.

At the top of the file, click on the Delete Icon.

Browse-to-the-file-in-your-repository

Step 3: At the bottom of the page, type a short, meaningful commit message that describes the change you made to the file

type-a-short-meaningful-commit-message

Then it will show an alert like this that the File was successfully deleted.

Showing-an-Alert

Conclusion

There may be several reasons to delete a Git Repository, whether your work is completed in a particular project or you want to create space in your computer by deleting files. And, deleting a git repository is very simple as well. You just need to manually delete the files in a project till all the files will be deleted. But git repositories have a hidden folder named .git and you have to delete that also to fully delete a git repository. Whether you want to delete a git repository locally or want to delete a git repository on GitHub, both methods are very simple and explained in the article above.

FAQs on How to Delete a Git Repository

1. What are the commands to delete a Git Repository?

You can use these Git commands to delete a Git repository after moving to the targeted directory.

rm -fr .git

2. Can I recover a deleted Git Repository?

No, once you delete a Git repository, it is not possible to recover it (especially when using rm -rf or the equivalent command). Hence, use this command carefully and backup the data incase, it will be used in the future.

3. How to add change files in git?

To add file changes to a git repository using the git add –all command all your changes will be saved to the staging area.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads