使用新的 Android Studio 项目替换 GitHub 存储库,同时保留旧的提交 [英] Replace GitHub repository with a new Android Studio project while preserving old commits

查看:24
本文介绍了使用新的 Android Studio 项目替换 GitHub 存储库,同时保留旧的提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个旧的 Android 项目,我想我是从 Eclipse 或一些旧版本的 Android Studio 开始的.无论如何,项目结构与 Android Studio 现在使用 Gradle 组织事物的方式完全不同.我没有尝试更新每个文件位置,而是使用相同名称的新项目重新开始.

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.

现在我想更新我的 GitHub 存储库,但我不想丢失我以前的提交,这显然会导致 git push --force origin master 之类的操作(请参阅 这里这里).

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

这个问题类似于替换GitHub repo,同时保留issues,wiki等,但我想知道Android Studio的具体情况.此外,该问题的当前答案不保留提交历史.

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.

推荐答案

  1. 将您当前的项目文件夹(您要放在 GitHub 上的新文件夹)重命名为类似 MyProjectBackup 的名称.

在 Android Studio 中,转到文件 > 新建 > 来自版本控制的项目 > Git.然后使用您的 GitHub 用户名和密码登录,并从 GitHub 存储库列表中选择旧项目的存储库名称.继续执行导入向导,您应该会在 Android Studio 中得到您的旧项目.(例如,现在您的旧项目在 MyProject 中,而您的新项目在 MyProjectBackup 中).

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

从您的 MyProject 项目文件夹中手动删除除 .git.gitignore(可能还有自述文件和许可证)之外的所有内容.(我试过 git rm -r * 但我是 出现错误.)

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

MyProject 文件夹中的命令行运行

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.

MyProjectBackup 复制所有新文件.然后运行

Copy in all your new files from MyProjectBackup. Then run

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

现在您的新项目将在 GitHub 中,旧项目的提交历史仍将可用.

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

有用的阅读

这篇关于使用新的 Android Studio 项目替换 GitHub 存储库,同时保留旧的提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆