首页 > 解决方案 > 我们可以在分支上创建一个空提交而不切换到它吗

问题描述

简而言之,存储库又旧又大,比如说 7GB。例如,当前分支是branch01,远程被调用origin。任务是在branch02. 基本上,空提交是在执行 git-push 时触发 CI(持续集成)过程。

然而,从一个分支切换到另一个分支可能需要几秒钟,甚至几分钟,因为可能会有大量的 IO 操作。有没有办法在不切换到它的情况下创建空提交branch02,就像下面一样。

# to create the empty commit
# git command: hash-object, write-tree, commit-tree

# after the empty commit is created
git push origin branch02:branch02

标签: git

解决方案


只是为了回答最初的问题,您可以使用一些低级命令来做到这一点:

cmt=$(git commit-tree -m "trigger Jenkins" -p branch02 branch02^{tree}) &&
git update-ref refs/heads/branch02 $cmt

也就是说,您创建一个新的提交对象,其树对象与原始分支相同,原始分支作为父分支,然后将分支指向新提交。


推荐阅读