我正在使用git subtree
来组织我的git代码库。假设我有一个名为repo
的主要代码库和一个名为lib
的库。
我成功地通过压缩其历史记录来“导入”lib
代码库。现在,我想通过压缩历史记录来向lib
贡献代码。但这似乎行不通:我尝试将--squash
选项指定为git subtree push
,但查看历史记录时,我仍然发送了所有提交记录。
如何复现问题
这里是一个脚本,展示了复现该问题所需的最小命令:
#!/bin/bash
rm -rf lib lib-work repo
# repo is the main repository
git init repo
# lib is the 'subtreed' repository (bare to accept pushes)
git init --bare lib
git clone lib lib-work
cd lib-work
# adding a bunch of commits to lib
echo "v1" > README
git add README
git commit -m 'lib commit 1'
echo "v2" > README
git add README
git commit -m 'lib commit 2'
echo "v3" > README
git add README
git commit -m 'lib commit 3'
git push origin master
cd ..
cd repo
# adding initial commit to have a valid HEAD
echo "v1" > README
git add README
git commit -m 'repo commit 1'
git remote add lib ../lib
git subtree add --prefix lib lib master --squash
echo "v4" > lib/README
git add lib/README
git commit -m 'repo commit 2'
echo "v5" > lib/README
git add lib/README
git commit -m 'repo commit 3'
echo "v6" > lib/README
git add lib/README
git commit -m 'repo commit 4'
#git log --all --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s%Creset' --abbrev-commit
# "not working" command :
git subtree push --prefix lib lib master --squash
# pretty print the history
git log --all --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s%Creset' --abbrev-commit
cd ../lib
echo
git log --all --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s%Creset' --abbrev-commit
git log
显示问题
两个 git log blabla
命令的输出结果如下:
* b075d5e - (HEAD, master) repo commit 4
* ebdc7c7 - repo commit 3
* 9f1edab - repo commit 2
* 3d48bca - Merge commit '34e16a547819da7e228f3add35efe86197d2ddcb' as 'lib'
|\
| * 34e16a5 - Squashed 'lib/' content from commit 2643625
* 3f1490c - repo commit 1
* 1f86fe3 - (lib/master) repo commit 4
* 9f1639a - repo commit 3
* 8bd01bd - repo commit 2
* 2643625 - lib commit 3
* 3d64b8c - lib commit 2
* aba9fcb - lib commit 1
并且:
* 1f86fe3 - (HEAD, master) repo commit 4
* 9f1639a - repo commit 3
* 8bd01bd - repo commit 2
* 2643625 - lib commit 3
* 3d64b8c - lib commit 2
* aba9fcb - lib commit 1
正如您所见,虽然我指定了squash选项,但lib仍看到了“repo commit 2,3,4”。相反的方式起作用,因此出现了 “Squashed 'lib/' content from commit f28bf8e'”。
我尝试了在Windows上使用git版本1.8.1.msysgit.1,在Linux上使用git版本1.8.3.4。
那么为什么--squash选项不会压缩呢?
附带问题
为什么lib/master出现在repo存储库的日志中? 仅当“失败”的git push之后才会出现。如果取消注释第一个git log blabla,则会得到以下输出,显示已存档的历史记录,但没有lib/master的迹象:
* b075d5e - (HEAD, master) repo commit 4
* ebdc7c7 - repo commit 3
* 9f1edab - repo commit 2
* 3d48bca - Merge commit '34e16a547819da7e228f3add35efe86197d2ddcb' as 'lib'
|\
| * 34e16a5 - Squashed 'lib/' content from commit 2643625
* 3f1490c - repo commit 1
--all
选项。使用该选项就像获取refs/
中所有引用的日志一样,因为推送操作在那里创建了一个新的引用,因此它会与HEAD
的日志一起打印出来。 - Maic López Sáenzgit subtree push
命令,您执行的是一次分割操作(split
),它会创建一个分支(即额外的引用),然后再将该引用推送到服务器上。当您只使用split
命令时,它将只创建一个提交记录而不创建新的分支,split
命令将只打印出所创建的提交记录哈希值。 - Maic López Sáenz--branches
替换--all
就可以解决问题,日志手册链接。 - ibizaman