合并不同的git仓库
“How to megre two git repository?”
假设有两个不同的git代码仓库repoA和repoB, 而我们希望将其在本地合并到一个git仓库”Code”,该如何完成呢?
我们期望将repoA的代码checkout到”Code/sourceA”中, repoB中的代码在”Code/sourceB”中。
在本地的git repository中开一个新的branch:
git checkout -b merged
首先,先检出repoA的代码:
git remote add -f repoA url_to_repoA git merge -s ours --no-commit repoA/master git read-tree --prefix=Code/sourceA -u repoA/master git ci -m "merge repoA into our local repo"
然后,再检出repoB的代码
git remote add -f repoB url_to_repoB git merge -s ours --no-commit repoB/master git read-tree --prefix=Code/sourceB -u repoB/master git ci -m "merge repoA into our local repo"
这样,本地的git仓库中的merged分支即合并了repoA和repoB的master
特别地, 在本地repoB作为repoA的子目录,即repoA的代码在Code中, 而repoB的代码在Code/sourceB中:
cd Code git clone url_to_repoA git remote add -f repoB url_to_repoB git merge -s ours --no-commit repoB/master git read-tree --prefix=Code/sourceB -u repoB/master git ci -m "merge repoB into repoA"
Patch中的空文件
通过版本管理工具生成的patch中若包含空文件, 直接用patch在目标项目中去应用这个补丁,这个空文件不会被patch创建,这是因为patch会自动忽略掉补丁中的空文件。
例如,version0 和version1分别是同一个git项目的两个版本拷贝
$ ls version0/ 001.txt $ ls version1/ 001.txt 002.txt
version1比version0多了一个002.txt的空文件, 用git diff生成test.patch
$cat test.patch diff --git a/002.txt b/002.txt new file mode 100644 index 0000000..e69de29
然后再version01中用patch命令应用test.path,出现提示“Only garbage was found…”
$ patch -p1
解决方法: 利用版本管理工具去应用补丁, 比如git中,利用“git apply”去打补丁。
$ git apply ../test.patch
一般情况下,很少直接创建空文件并提交, 但是这种情况一般发生在python package中, python package需要在目录中有个__init__.py文件
__init__.py是个空文件, 在patch的时候,目标目录中并不会创建这个文件, 如果直接用patch命令,于是悲剧发生了…
Traceback (most recent call last): File "gyp/gyp", line 18, in sys.exit(gyp.main(sys.argv[1:])) File "gyp/pylib/gyp/__init__.py", line 436, in main options.circular_check) File "gyp/pylib/gyp/__init__.py", line 55, in Load generator = __import__(generator_name, globals(), locals(), generator_name) ImportError: No module named generator.make
另外一种解决方法 提交的__init__.py中插入一些空格字符或者注释,以避免上述情况的发生。