GitHub合并2个项目并保留commit记录

GitHub有两个项目Pro1和Pro2,现打算将Pro2整个项目移动到Pro1,并保留2个项目的commit记录。
移动前Pro1目录结构为:文件Pro1.txt、文件Pro123、文件README.md、文件夹pro1dir/,项目 Pro2类似。
移动后目录结构为Pro/Pro1/*;pro/Pro2/*(Pro1重命名为pro)。

建议新建测试项目Pro1、Pro2熟悉一下流程,或者备份原有项目,避免误操作产生意料之外的效果。

1、clone项目Pro1到本地
cd Pro1

2、在Pro1项目根目录新建文件夹Pro1,并将原Pro1项目所有文件移动至Pro1/下(如果不需要完全区别两个项目可忽略)
mkdir Pro1
cd Pro1
vi pro1.txt 输入描述信息(move Pro1 to new dir)
如果时github网页创建新文件,则create new file》文件名框中输入文件夹名Pro1/(有个斜杠),在输入文件名即可。
# 移动所有文件(夹)到新文件夹Pro1
cd ..
mv Pro123 README.md pro1dir/ Pro1

3提交所有变更到github
# 将文件的修改、删除、新建添加到暂存区
(注意区别:git add -u:将文件的修改、文件的删除,添加到暂存区;git add .:将文件的修改,文件的新建,添加到暂存区)
git add -A
git status
git commit -m "move Pro1 all files to new dir"
git push (可能会输入用户名密码)

4、移动项目Pro2的所有内容到Pro1
# 将新项目添加到旧项目中
# 校验是否添加成功
$ git remote -v
Pro2 https://github.com/zxiaofan/Pro2.git (fetch)
Pro2 https://github.com/zxiaofan/Pro2.git (push)
origin https://github.com/zxiaofan/Pro1.git (fetch)
origin https://github.com/zxiaofan/Pro1.git (push)
# 获取新项目内容
git fetch Pro2
# 合并新项目Pro2到本地项目Pro1
git merge Pro2/master
Note 如果报错:fatal: refusing to merge unrelated histories
此错误出现在2.9.0版本后,在命令后添加执行参数--allow-unrelated-histories即可
git merge Pro2/master --allow-unrelated-histories
会提示你输入merge信息:比如输入:mergee Pro2 to Pro1

参考操作2,将Pro2的所有内容移动到新建文件夹Pro2中。

5、参考操作3,提交所有变更到github

6、项目重命名
前往github官网,在项目Pro1的Settings中重命名Pro1为Pro。
Note:Pro1重命名为Pro后,原Pro1的地址将重定向到新的Pro项目的地址。

至此2个github项目合并完毕。
可参考本人合并后的项目:https://github.com/zxiaofan/springStudy,该项目是有SpringBootStudy、SpringDataStudy两个项目合并到chapter(合并后更名为SpringStudy)项目,保留了原有的所有commit记录。

欢迎个人转载,但须在文章页面明显位置给出原文连接;
未经作者同意必须保留此段声明、不得随意修改原文、不得用于商业用途,否则保留追究法律责任的权利。

【 CSDN 】:csdn.zxiaofan.com
【GitHub】:github.zxiaofan.com

如有任何问题,欢迎留言。祝君好运!
Life is all about choices! 
将来的你一定会感激现在拼命的自己!

已标记关键词 清除标记
<div class="post-text" itemprop="text"> <p>I'm developing some web application (php, codeigniter). Today a time to push it to github has come, but I've encountered an issue I can't manage.</p> <p>I've got 2 branches:</p> <ul> <li>master - this branch should be used for github pushing, does not contain credentials or any other sensitive data (just public code)</li> <li>dev - this is master's child, used for development, does contain credentials (of my private app instantion)</li> </ul> <p>By credentials I mean: database login, password, host, user; oauth services id / secrets; encryption_key. Credentials are stored in 2 separated files within /application/config folder.</p> <p>The idea is that I'll make all changes on dev, than merge dev into master, and pull master content to github (but master can't contain my private credentials).</p> <p><strong>This is what I've done till now:</strong></p> <ol> <li>Removed all credentials from /application/config/config.php and /applications/config/database.php (as I don't want them to be public on github) they've become an empty strings like: $db['default']['hostname'] = '';</li> <li>Started git repository and made an initial commit - the default branch that was created is named 'master'</li> <li>Pulled master into github</li> </ol> <p>And it's pretty cool till now - I've got my project code on github with no credentials within. Next I need to make some changes in my project, so I:</p> <ol> <li>Created new branch 'dev'</li> <li>Stored my credentials into dev, and make a commit - I need dev instantion to have credentials as I'm uploading this to ftp server every time I change something in the code (I don't use local LAMP or anything like this in this project)</li> </ol> <p>So now I need to make some change in dev, upload it and pull to github so I:</p> <ol> <li>Make an change on dev (let's say add new feature)</li> <li>Checkout to master</li> <li>Merge dev into master.</li> </ol> <p><strong>What I expected to happen:</strong> Git will ask me which version of configs should be used in 'master' as it is different in both branches. I will use diff to chose a 'master' verion of files (which does not contain credentials). Git will remember my choice, and in the future I will not have to chose again when I'll merge dev into master. Git works like this on one of my other projects, where dev is a master branch and instantions are it's children (In this case dev is a child and I merge child into parent).</p> <p><strong>What happened:</strong> Git just copied credentials from dev to master, did not ask me about that. So now my master branch is 'tainted' by credentials and I can't push it to github anymore.</p> <p><strong>My questions are:</strong></p> <ol> <li>Why is that happening, how can I avoid this?</li> <li>Is there any other (better?) way to handle that kind of issue.</li> </ol> <p>Please let me know if anything is unclear, any help will be appreciated.</p> </div>
©️2020 CSDN 皮肤主题: 程序猿惹谁了 设计师:上身试试 返回首页