Open uniquejava opened 6 years ago
详见: https://help.github.com/en/articles/splitting-a-subfolder-out-into-a-new-repository
假设子目录的名称为xxx
使用的命令如下
git check master
git filter-branch --prune-empty --subdirectory-filter xxx master
git remote set-url origin git@github.ibm.com:org_name/xxx.git
git push -u origin master
git check 1.x
git filter-branch --prune-empty --subdirectory-filter xxx 1.x # (如果有错误提示加 -f参数)
git push -u origin 1.x
git check 2.x
git filter-branch --prune-empty --subdirectory-filter xxx 2.x # (如果有错误提示加 -f参数)
git push -u origin 2.x
linux redhat 用的如下命令,window10 sed "s, ,&'"$PREFIX"'/," | 这里也过不去 git filter-branch --index-filter ' git ls-files -s | sed "s,\t,&'"$PREFIX"'/," | GIT_INDEX_FILE=$GIT_INDEX_FILE.new git update-index --index-info && mv $GIT_INDEX_FILE.new $GIT_INDEX_FILE ' HEAD
linux redhat 用的如下命令,window10 sed "s, ,&'"$PREFIX"'/," | 这里也过不去 git filter-branch --index-filter ' git ls-files -s | sed "s,\t,&'"$PREFIX"'/," | GIT_INDEX_FILE=$GIT_INDEX_FILE.new git update-index --index-info && mv $GIT_INDEX_FILE.new $GIT_INDEX_FILE ' HEAD
看来mac和linux的sed命令略有区别. windows不认识sed 😃
合并: 将若干个不相干的项目合并到一个项目
将git升级到最新版 macOS下:
brew upgrade git --verbose
当前的我的git版本: 2.18.0
合并过程
TLDR:
假定项目名为project1, 需要把这个project1项目整体放到另一个叫project2的项目中,
要同时保留project1提交历史,
先把project1拷贝到任意目录, 比如 /tmp目录下. 然后执行如下命令
git filter-branch这个命令超级长. 用来批量变更文件路径. 让project1仓库中的所有文件的路径前多上一层PREFIX
GIT_INDEX_FILE
是GIT的内置环境变量, 不需要赋值. 详见: https://git-scm.com/book/en/v2/Git-Internals-Environment-Variables最后是进到project2项目根目录, 执行merge操作.
背后的思想
The submodule approach is good if you want to maintain the project separately. However, if you really want to merge both projects into the same repository, then you have a bit more work to do.
The first thing would be to use
git filter-branch
to rewrite the names of everything in the second repository to be in the subdirectory where you would like them to end up. So instead offoo.c
,bar.html
, you would haveprojb/foo.c
andprojb/bar.html
.Then, you should be able to do something like the following:
The
git pull
will do agit fetch
followed by agit merge
. There should be no conflicts, if the repository you're pulling to does not yet have aprojb/
directory.By Greg Hewgill
Linux版
Linux和mac的sed命令略有不同. 对于tab键. Linux是用的字面量
\t
, mac要实际的按下tab键(看起来是个长空格), 详见 References 链接4Referecences