Reputation: 1884
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
Reputation: 131
There is a workaround, and it worked for me.
Upvotes: 5
Reputation: 5234
Full 2023 solution to only reset the repository to a new/ clean state:
Code -> Branches
and delete all branches except for master
/ main
Repository -> Protected branches
and toggle Allowed to force push
for the master/ main branch to ON
git init -b master
or git init -b main
git add --all
git commit -m 'Initial commit after reset'
Code -> Repository
and copy the SSH url (use the clone button, including the username)git remote add origin <URL_FROM_PREVIOUS_STEP>
git push -f --set-upstream origin master
or git push -f --set-upstream origin main
Repository -> Protected branches
and toggle Allowed to force push
for the master/ main branch to OFF
PS: also consider running
Run Housekeeping
andPrune unreachable objects
fromSettings -> General -> Advanced
to reduce the size of your repository after cleanup :)
Upvotes: 9
Reputation: 951
Delete all branches except master
, unprotect master
in settings -> repository -> protected branches
and execute:
git push -f master origin
.
Upvotes: 10
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
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