如何将整个存储库压缩到第一次提交?
我可以将base转换为第一次提交,但这将留给我2次提交。 有没有办法在第一个提交之前引用这个提交?
如何将整个存储库压缩到第一次提交?
我可以将base转换为第一次提交,但这将留给我2次提交。 有没有办法在第一个提交之前引用这个提交?
当前回答
首先,使用git rebase -interactive将所有提交压缩成一个提交。现在就剩下两个壁球提交了。要做到这一点,请阅读任何一本
如何合并Git存储库的前两次提交? Git:如何压缩前两次提交?
其他回答
一个衬套
你可以直接从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")
这避免了将模板回购作为远程(源或其他)添加,并将模板回购的历史记录折叠到初始提交中。
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.")
以下是我最后是如何做到这一点的,以防其他人也能做到:
记住,做这样的事情总是有风险的,在开始之前创建一个保存分支从来都不是一个坏主意。
从日志开始
git log --oneline
滚动到第一次提交,复制SHA
git reset --soft <#sha#>
将<#sha#>替换为从日志中复制的sha
git status
确保所有内容都是绿色的,否则运行git add -A
git commit --amend
将当前所有更改修改为当前首次提交
现在强制推这个分支,它会覆盖那里的内容。
我读过一些关于移植的东西,但从来没有深入研究过。
不管怎样,你可以手动压缩最后2次提交,就像这样:
git reset HEAD~1
git add -A
git commit --amend
从git 1.6.2开始,你可以使用git rebase——root -i
对于除第一次提交外的每个提交,将pick更改为squash。