Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace GitHub repository with a new Android Studio project while preserving old commits

I had an old Android project that I think I had started in Eclipse or some old version of Android Studio. Anyway the project structure was completely different from how Android Studio organizes things now with Gradle. Rather than try to update every file location I just started over with a new project using the same name.

Now I would like to update my GitHub repository but I don't want to lose my previous commits, which doing something like git push --force origin master would apparently cause (see here and here).

This question is similar to Replace GitHub repo while preserving issues, wiki, etc, but I would like to know for the specific case of Android Studio. Also the current answer to that question does not preserve the commit history.

I am going to try to figure out a way to do this based on hints from here and here. If I can solve it, I will post my answer below.

like image 873
Suragch Avatar asked Dec 22 '16 05:12

Suragch


People also ask

How do I transfer a repository to another repo?

You first have to get the original Git repository on your machine. Then, go into the repository. Finally, use the --mirror flag to copy everything in your local Git repository into the new repo.


1 Answers

  1. Rename your current project folder (the new one you want to put on GitHub) to something like MyProjectBackup.

  2. In Android Studio, go to File > New > Project from Version Control > Git. Then log in with your GitHub username and password and select your old project's repository name from the list of your GitHub repos. Continue through the import wizard and you should end up with your old project in Android Studio. (Now, for example, your old project is in MyProject and your new project is in MyProjectBackup).

  3. Manually delete everything except for .git and .gitignore (and maybe the readme and license) from your MyProject project folder. (I had tried git rm -r * but I was getting errors.)

  4. From the command line in your MyProject folder run

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

    This will update the tracked files in the working tree for all the manual deletions you just made.

  5. Copy in all your new files from MyProjectBackup. Then run

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

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

Helpful reading

  • Git Basics - Recording Changes to the Repository
like image 164
Suragch Avatar answered Sep 23 '22 00:09

Suragch