如何将整个存储库压缩到第一次提交?
我可以将base转换为第一次提交,但这将留给我2次提交。 有没有办法在第一个提交之前引用这个提交?
如何将整个存储库压缩到第一次提交?
我可以将base转换为第一次提交,但这将留给我2次提交。 有没有办法在第一个提交之前引用这个提交?
当前回答
当我从git存储库恢复模板时,我通常会压缩整个树,以获得更干净的历史记录,并确保合法合规。我的工作流程是这样的:
git clone https://git.invalid/my-awesome-template.git my-awesome-foo
cd !$
git branch -M master my-awesome-template/master
git checkout --orphan master
git rm -rf /
git commit --allow-empty --allow-empty-message -m 'Initial commit'
git merge --squash --allow-unrelated-histories my-awesome-template/master
git commit
git branch -D my-awesome-template/master
# you can now `drop' that "Initial commit":
git rebase -i --root
这将把整个历史记录压缩到一个大的提交消息中。
在这个例子中:
Master是工作分支 My-awesome-template /master是一个中间分支
其他回答
我通常是这样做的:
Make sure everything is committed, and write down the latest commit id in case something goes wrong, or create a separate branch as the backup Run git reset --soft `git rev-list --max-parents=0 --abbrev-commit HEAD` to reset your head to the first commit, but leave your index unchanged. All changes since the first commit will now appear ready to be committed. Run git commit --amend -m "initial commit" to amend your commit to the first commit and change the commit message, or if you want to keep the existing commit message, you can run git commit --amend --no-edit Run git push -f to force push your changes
从git 1.6.2开始,你可以使用git rebase——root -i
对于除第一次提交外的每个提交,将pick更改为squash。
我用HEAD^{tree}尝试了不同的命令,但从zsh得到了这个错误:
zsh: no matches found: HEAD^{tree}
这是ohmyzsh处理扩展的方式,查看这个问题了解更多细节: https://github.com/ohmyzsh/ohmyzsh/issues/449
最简单的解决方法是运行这个命令:
unsetopt nomatch
一行6个字:
git checkout --orphan new_root_branch && git commit
echo "message" | git commit-tree HEAD^{tree}
这将创建一个带有HEAD树的孤立提交,并在stdout上输出其名称(SHA-1)。然后重置你的分支。
git reset SHA-1
要在一个步骤中完成上述工作:
git reset $(git commit-tree HEAD^{tree} -m "Initial commit.")