如何将本地Git仓库推送到远程仓库的一个文件中并保留Commit记录
将本地Git仓库推送到远程仓库的一个文件中并保留Commit记录
当我们在本地用Git管理了一段代码,过了一段时间后想把它推送到远程仓库,但是又不想放到根目录下,因为我还需要在远程仓库中加入其它内容,比如硬件,文档等,这时怎么操作呢?这时我们就要用到Git的Subtree功能
# /path/to/your/local/repo: path to your local repo root, could be absolute path or relative path to current path
$ git remote add localrepo /path/to/your/local/repo# fetch
$ git fetch localrepo# new_folder/under/remote/root: a new folder under your remote repo, which should not exist yet, to contain the local repo, beware that the parent folder of the local repo is not copied, like if you local repo is under foler <local_repo>, the folder <local_repo> won't be copied, only the contents under <local_repo> will be copied.
# for example, if you would like to merge you <local_repo> to <code/motor_control> under remote root, the command should be : git subtree add --prefix=code/motor_control localrepo master
# Make sure the branch name of the local repo is 'master', otherwise change accordingly, if you are not sure, use command: 'git remote show localrepo' to get the branch name
$ git subtree add --prefix=new_folder/under/remote/root localrepo master# Finally push the commit to remote:
$ git push origin master