26

I have a repo on GitHub with a lot of issues there. I want to replace the entire code base of that repo with a new one but I want to keep all the previous information.

What's the best way of doing that without messing up the code nor GitHub?

Andrew Marshall
  • 95,083
  • 20
  • 220
  • 214
donald
  • 23,587
  • 42
  • 142
  • 223

4 Answers4

32
  • cd into "new" repository
  • git remote add origin git@github.com:myusername/myrepository (replacing myusername & myrepository accordingly)
  • git push --force origin master
  • Possibly delete other remote branches and push new ones.

This will forcefully replace everything in the remote repository with what you have locally. Be very careful, there is no going back.

Andrew Marshall
  • 95,083
  • 20
  • 220
  • 214
  • 4
    Keeping in mind that until something runs `git gc` the old orphaned blobs will still all be there using up space. – Chris Eberle Dec 11 '11 at 02:36
  • @Chris of course, but obviously those shouldn't be depended on for rolling-back, and there's not much that can be done about them when you don't have control of the server. – Andrew Marshall Dec 11 '11 at 02:39
  • The point being that on a server like github which has storage limits, just because you think you got rid of your old code doesn't mean it's not still there eating up space. I'm not disagreeing, your instructions are close to what I'd do, I'm just pointing out the downside of doing it this way. – Chris Eberle Dec 11 '11 at 02:42
  • @Chris Ah, I see, didn't think about the quota implications. – Andrew Marshall Dec 11 '11 at 03:29
  • @Chris why don't you say what you would do ? – donald Dec 11 '11 at 03:38
  • @donald essentially the same thing, just with a few different commands. Personally I'd rename `master` on github to something else (`tmp` maybe), push something brand new to `master`, and then remove the `tmp` branch. It would accomplish the exact same thing. – Chris Eberle Dec 11 '11 at 04:00
  • This solution doesn't preserve the commit history, right? – Suragch Dec 22 '16 at 02:53
  • 1
    @Suragch It intentionally does not. – Andrew Marshall Dec 22 '16 at 08:16
19

Since git push --force origin master does not preserve the code commits from the old project, I think this question needs another answer. I already made an answer for an Android Studio project, but it is not much different to use Git exclusively.

I'll call the old project on GitHub that you want to replace MyProject.

Backup your new project

Rename your current project (or copy it to a new location and delete the folder with the original project name. We will clone the old project version from GitHub to this location.)

Clone the old project from GitHub

Go to the directory that you want your project in and run

git clone https://github.com/username/repo.git

Replace username with your GitHub user name and repo with your repository name.

Remove all the old files

You can try

git rm -r *

But if that doesn't work (as it didn't work for me), then manually remove them with rm or a file manager. However, don't delete the .git folder and .gitignore.

Then if you deleted them manually, you need to update git.

git add -u
git commit -m "deleting old files before adding updated project"

Add the new files

Copy in all the files from your new project that you previously made a backup of. Now let git know that you want to add them to the repository.

git add .
git commit -m "new updated project"

Push your changes to GitHub

git push

Now your new project will be in GitHub, but the old project's commit history will still be available.

Community
  • 1
  • 1
Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
  • The reason `*` doesn't work is because git doesn't understand `*`, but your shell does, depending on how it is treating `globbing`. You could try to use `git rm -r ./*` instead. (But I have not tried it!) For more details on these subtleties look at [this](http://stackoverflow.com/a/26042555/1147688) and [this](http://stackoverflow.com/a/39349920/1147688) SO answers. – not2qubit Apr 14 '17 at 18:07
  • Also, please confirm in your answer if this method still preserve the `wiki` and `issues` entries as requested in the OP. – not2qubit Apr 14 '17 at 18:09
  • @not2qubit, I actually haven't tested the wiki and issues, but I assume it would preserve them since you are not deleting those things. However, if someone else tests this, I would appreciate a comment confirming or denying it. – Suragch Apr 15 '17 at 01:27
  • 1
    @Suragch I just did the same thing and my old commits and Issues on gitlab are still there. – John Nov 01 '18 at 18:33
  • 2
    Assuming the new project is also a git repository, this does not keep the commit history for the new project, does it? – Alexandre Feb 12 '20 at 20:10
  • @lalilulelost Good point. This method wouldn't keep any git history from your new project. – Suragch Feb 13 '20 at 00:38
3

The similar problem I have solved in the way described below.

Assumptions:

  • "old project" is on GitHub repo
  • "new project" is created localy on your machine

Expectations:

  • new project must be pushed to the old repo on github
  • old commit history has to remain and be visible in github

Solution:

1) go to the "old project" folder on your local machine

2) find the hidden ".git" folder there and copy it

3) go to the new project folder on your machine

4) check if there is a .git folder (it is hidden so you need to show hidden files) - if there is a .git folder rename it, you can either delete it but its better to rename now and delete if all will go according to plan - if there is not .git folder go to the point 5 below. 5) paste previously copied .git folder (from old project) and paste it in the "new project" folder

Now the new project has a .git folder with all previous changes and history and includes the reference to the URL of you old repo on GitHub.

6) If you are using for example VS, you can check changes in code. There will be lots of them, or check them in terminal. You can check that old files have been deleted and new files added.

7)Push actual new project. This new project will be pushed to your old repo on GitHub. Check your git repo on web to be sure that all went well.

8) Now you can delete old project from your local machine and delete this renamed new git hidden folder (it was renamed in point 4).

Now you can develop your new project, keeping all old history with you.

Hope it helps.

Ja Rek
  • 116
  • 1
  • 5
0

Copy the old .git folder to the new ones. And then do push flow as usual (git add, commit, push).

Keenan Gebze
  • 1,366
  • 1
  • 20
  • 26