Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
650 views
in Technique[技术] by (71.8m points)

git - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
  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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...