如何将整个存储库压缩到第一次提交?

我可以将base转换为第一次提交,但这将留给我2次提交。 有没有办法在第一个提交之前引用这个提交?


当前回答

我用HEAD^{tree}尝试了不同的命令,但从zsh得到了这个错误:

zsh: no matches found: HEAD^{tree}

这是ohmyzsh处理扩展的方式,查看这个问题了解更多细节: https://github.com/ohmyzsh/ohmyzsh/issues/449

最简单的解决方法是运行这个命令:

unsetopt nomatch

其他回答

这个答案改进了上面的几个(请投票),假设除了创建一个提交(no-parents - no-history),你还想保留该提交的所有提交数据:

作者(姓名及邮箱) 撰写日期 提交人(姓名及邮箱) 提交日期 提交日志消息

当然,新的/single提交的commit- sha会改变,因为它代表了一个新的(非)历史,变成了一个无父/根提交。

这可以通过读取git日志并为git提交树设置一些变量来实现。假设你想在一个新的分支one-commit中从master创建一个单独的提交,保留上面的commit-data:

git checkout -b one-commit master ## create new branch to reset
git reset --hard \
$(eval "$(git log master -n1 --format='\
COMMIT_MESSAGE="%B" \
GIT_AUTHOR_NAME="%an" \
GIT_AUTHOR_EMAIL="%ae" \
GIT_AUTHOR_DATE="%ad" \
GIT_COMMITTER_NAME="%cn" \
GIT_COMMITTER_EMAIL="%ce" \
GIT_COMMITTER_DATE="%cd"')" 'git commit-tree master^{tree} <<COMMITMESSAGE
$COMMIT_MESSAGE
COMMITMESSAGE
')

以下是我最后是如何做到这一点的,以防其他人也能做到:

记住,做这样的事情总是有风险的,在开始之前创建一个保存分支从来都不是一个坏主意。

从日志开始

git log --oneline

滚动到第一次提交,复制SHA

git reset --soft <#sha#>

将<#sha#>替换为从日志中复制的sha

git status

确保所有内容都是绿色的,否则运行git add -A

git commit --amend

将当前所有更改修改为当前首次提交

现在强制推这个分支,它会覆盖那里的内容。

如果您想要做的只是将所有提交压缩到根提交,那么while

git rebase --interactive --root

可以工作,但对于大量提交(例如,数百个提交)来说是不切实际的,因为在生成交互式的重基编辑器提交列表以及运行重基本身时,重基操作可能会运行得非常慢。

当你压缩大量的提交时,这里有两个更快更有效的解决方案:

替代解决方案#1:孤儿分支

你可以简单地在你当前分支的顶端(即最近的提交)创建一个新的孤儿分支。这个孤儿分支形成了一个全新的独立提交历史树的初始根提交,这实际上相当于压缩了你所有的提交:

git checkout --orphan new-master master
git commit -m "Enter commit message for your new initial commit"

# Overwrite the old master branch reference with the new one
git branch -M new-master master

文档:

git-checkout(1)手册。

备选方案2:软复位

另一个有效的解决方案是简单地使用混合或软重置到根提交<root>:

git branch beforeReset

git reset --soft <root>
git commit --amend

# Verify that the new amended root is no different
# from the previous branch state
git diff beforeReset

文档:

git-reset(1)手动页面。

一个衬套

你可以直接从HEAD创建一个squash-all commit,完全不需要rebase,只需要运行:

git reset $(git commit-tree HEAD^{tree} -m "A new start")

在~/.gitconfig中创建别名

[alias]
  squash-all = "!f(){ git reset $(git commit-tree HEAD^{tree} \"$@\");};f"

然后只要运行:git squash-all -m“一个全新的开始”

注意:要么从标准输入提供提交消息,要么通过-m/-F选项,就像git commit命令一样。参见git-commit-tree手册。

或者,您也可以使用以下命令创建别名:

git config --global alias.squash-all '!f(){ git reset $(git commit-tree HEAD^{tree} "$@");};f'

解释

create a single commit via git commit-tree What git commit-tree HEAD^{tree} -m "A new start" does is: Creates a new commit object based on the provided tree object and emits the new commit object id on stdout. The log message is read from the standard input, unless -m or -F options are given. The expression HEAD^{tree} represents the tree object corresponding to HEAD, namely the tip of your current branch. see Tree-Objects and Commit-Objects. reset the current branch to the new commit Then git reset simply reset the current branch to the newly created commit object. This way, nothing in the workspace is touched, nor there's need for rebase/squash, which makes it really fast. And the time needed is irrelevant to the repository size or history depth.


变体:项目模板中的新回购

这对于在新项目中使用另一个存储库作为模板/原型/种子/框架创建“初始提交”非常有用。例如:

cd my-new-project
git init
git fetch --depth=1 -n https://github.com/toolbear/panda.git
git reset --hard $(git commit-tree FETCH_HEAD^{tree} -m "initial commit")

这避免了将模板回购作为远程(源或其他)添加,并将模板回购的历史记录折叠到初始提交中。

从git 1.6.2开始,你可以使用git rebase——root -i

对于除第一次提交外的每个提交,将pick更改为squash。