Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Mattiman's avatar

Git repository start over

I've got a project which I've been working on for a while, with the repository being on Github, build in a different framework. But now I'm working with Laravel, I have started over with the project. I just created a new Laravel project in a different directory, starting from scratch again. This new project is not in git/github yet.

So now I want to "replace" the Github repository (with the old code) with my new code. I could completely delete the Github project, create a new one and start over. But it would be nice if I could use the same Github repo and "push" my new local code into that one, completely replacing the old code. So then I'd still be able to see the old code and all of its commits.

Have been searching for a while, but most threads I find talk about merging branches. I don't have a local branch.

My guess is it's something like this:

cd my/local/dir/with/project
git init
git add --all
git commit -m "first commit with new code base"
git remote add origin https://github.com/myname/myproject.git
git push --force origin master

But I'm not sure about it. Will that last line do exactly what I want?

0 likes
6 replies
Mattiman's avatar

Ok I tried that code. Didn't work as I wanted: it pushed all code to the repo, but I lost all old commit history in the Github repo.

michaeldyrynda's avatar

Well, yes, you just forced a whole new git history into that repo, which had no knowledge of the other history.

Better option would have been to git remove -r everything in your original working copy, copy your Laravel codebase into it, then add and commit the new files.

I'm not sure if there's any way to recover the old codebase's history now, unless you have it checked out somewhere (git push --force it back to origin.)

Mattiman's avatar

Hi deringer, I did not know that. Should have waited running that command... it seemed like it as what I needed to do :(

As far as the old codebase: I think if I go into the directory on my machine in which the old code is, that should contain the old history inside the .git files right? Sorry I don't know enough about Git yet, but I thought that was the whole idea/advantage of git, having multiple copies on each machine?

michaeldyrynda's avatar

Any old working copy should be able to do that, yes.

Once you get back to square one, do the migration of your Laravel code into that working copy in a feature branch to make sure you get it working the way you want before merging back to develop or master.

Also, try and avoid the --force flag in future unless you are fully aware of the consequences :)

Mattiman's avatar

Well the problem is I wouldn't now how to put back the old code in the repo now. Don't want to mess up even more, so guess I'll leave it like it is now. Thanks for the help!

Please or to participate in this conversation.