0

So

  1. I created a repository in gitlab
  2. Pushed some changes to the master branch
  3. Created many other branches and pushed changes to each of them too

Now I want to go back to the step 1 using command line, so basically an empty and initial repository that I had at step one, right after the repository creation with no history, nothing

Any way to do it in command line?

I tried many ways but not got succeed. Checked few stackoverflow posts too like this one but didn't work for my case

How to reset a remote Git repository to remove all commits?

https://gist.github.com/stephenhardy/5470814

Pawan Nogariya
  • 8,330
  • 12
  • 52
  • 105
  • Remove the directory and create a new one...? – Paolo Aug 26 '20 at 18:14
  • @Paolo - That is the obvious way, but I do not want to do that because I have tons of repositories so I am looking for a way to reset existing repositories from command line – Pawan Nogariya Aug 26 '20 at 18:35
  • You can get fairly close with a very short script: list all references, then use `git push --delete` to delete them all. That's not guaranteed to be the same state as an initial empty repository, though. – torek Aug 26 '20 at 19:23

1 Answers1

1

rm -rf .git then git init.

The .git directory holds all the commits, commit history and other metadata that make a repo a repo. When you delete the .git directory in a repo, it turns back into a regular directory.

Then you can do git init again like the very first time. Use git add to add your files and then git commit for you initial commit.

You will have to force push (git push -f) to Gitlab since you have essentially rewritten history from the remote's perspective. Alternatively you can delete the repo on Gitlab and create a fresh one. Then follow GitLab's instructions for pushing an existing repo to it.

Inigo
  • 12,186
  • 5
  • 41
  • 70