Sakura Kinomoto
Sakura Kinomoto

Reputation: 1884

How to reset / clean a GitLab repository

I want to reset one of my repositories on gitlab, but without deleting the whole project.

I've read on Gitlab Forums (on the post https://forum.gitlab.com/t/how-can-i-delete-a-repository-and-push-a-new-one/690) two different options:

First: Deleting the physical repository from disk. I've tried renaming the folder (repo.git) but then, gitlab gives me a 404 error trying to access to it.

Second: Deleting all branches. This solution do not work too, because, I cannot delete the main branch (on my case master), and when I create a new one, the new branch requires to specify a "original" branch. I cannot create a clean branch without any data and change the main to it.

Someone know how to do it?

Thanks,

Upvotes: 17

Views: 45352

Answers (5)

Jack
Jack

Reputation: 131

There is a workaround, and it worked for me.

  • At first, on project's settings/repository page change the default branch to a non-master branch,
  • Then delete the master branch and push the new files to master.
  • At last change the default branch back to master and delete all the other branches you don't want.

Upvotes: 5

Simon Mattes
Simon Mattes

Reputation: 5234

Full 2023 solution to only reset the repository to a new/ clean state:

  1. In your repository, navigate to Code -> Branches and delete all branches except for master/ main
  2. In your repository settings, navigate to Repository -> Protected branches and toggle Allowed to force push for the master/ main branch to ON
  3. Locally, create a new and empty directory, and initialize a new repository: git init -b master or git init -b main
  4. Create some file (or your initial commit files) and add them: git add --all
  5. Create the initial commit: git commit -m 'Initial commit after reset'
  6. In your repository, navigate to Code -> Repository and copy the SSH url (use the clone button, including the username)
  7. Add the remote in the newly-created directory: git remote add origin <URL_FROM_PREVIOUS_STEP>
  8. Force-push to upstream: git push -f --set-upstream origin master or git push -f --set-upstream origin main
  9. In your repository settings, navigate to Repository -> Protected branches and toggle Allowed to force push for the master/ main branch to OFF

PS: also consider running Run Housekeeping and Prune unreachable objects from Settings -> General -> Advanced to reduce the size of your repository after cleanup :)

Upvotes: 9

stakahop
stakahop

Reputation: 951

Delete all branches except master, unprotect master in settings -> repository -> protected branches and execute:

git push -f master origin .

Upvotes: 10

Enrico Cupellini
Enrico Cupellini

Reputation: 445

If you want to delete the branch's history, Matt already suggested the same strategy I have done. The only think to say is to "unprotect" a protected branch, otherwise you may face with an error like this:

git push -f origin last_known_good_commit:branch_name

remote: GitLab: You are not allowed to force push code to a protected branch on this project.

Go to your project "repository settings/protected branches" and unprotect it. After you make the update operation you can protect your branch again. Be careful to rewrite the repository history if you are working with other people on the same project.

Upvotes: 2

Axllent
Axllent

Reputation: 83

I've been asking myself this same question, and running through the same issues you mentioned. I have repositories with a lot of crud in them that shouldn't have been there before which is why I wanted to do this. I can easily reduce my local repo to < 1MB and force push that to gitlab, however the hosted repo simply doesn't reduce in size (I presume artefacts remain in the repo). Moving all issues across to a new repo isn't an option either as the dates all get "today's" date and is cumbersome.

I think I've discovered a way though. First export your current repo and download the tar.gz file from gitlab (this contains the repo, issues etc). Then create / modify / whatever the new repo you wish to replace the existing one with. Once ready, create a bundle of that new repo, ie: the one you want (from within that repo):

git bundle create /tmp/project.bundle --all

Then replace the project.bundle in the downloaded tar.gz with the new project.bundle.

Now you can create a new project on gitlab by importing that tar.gz, and you should hopefully have all your issues, tags, etc from the original, but with the new repository.

Now you can rename your old and new repos and hopefully you should be good to go. It's not ideal I know, but in my case it was the issue history I wanted to keep, and a pruned version of the repo.

Hope this helps!

Upvotes: 6

Related Questions