11

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,

Sakura Kinomoto
  • 1,784
  • 2
  • 21
  • 30
  • "reset one of my repositories on gitlab, but without deleting the whole project" What does that mean? – matt Dec 14 '18 at 02:11
  • I'm talking about cleaning the git repository only, without affect issues / comments / code snippets etc – Sakura Kinomoto Dec 14 '18 at 02:37
  • So couldn't you just delete all the files, turn that into a commit, and push it? Or do you mean you wish to delete the whole _history_? If the latter, this is well discussed already, e.g. https://stackoverflow.com/questions/9683279/make-the-current-commit-the-only-initial-commit-in-a-git-repository – matt Dec 14 '18 at 02:41

4 Answers4

9

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

git push -f master origin .

stakahop
  • 921
  • 9
  • 18
6

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!

Axllent
  • 83
  • 4
4

There is a workaround, and it works 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.

Jack
  • 111
  • 2
  • 4
2

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.

Enrico Cupellini
  • 447
  • 7
  • 14