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

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


当前回答

假设你的分支中有3个提交,并且它已经被推送到远程分支。

例子:

git log -4

将显示如下结果:

<your_third_commit_sha>
<your_second_commit_sha>
<your_first_commit_sha>
<master_branch_commit_sha - your branch created from master>

你想把最后3次提交压缩成一次提交,然后推送到远程分支。以下是步骤。

git reset --soft <master_branch_commit_sha>

现在所有提交的更改都被集成但未提交。验证:

git status

用一条消息提交所有更改:

git commit -m 'specify details'

强制将单次提交推到远程分支:

git push -f

其他回答

用嫁接压扁

添加一个文件。git/info/grafts,把你想要成为根的提交散列放在那里

Git日志现在将从该提交开始

要让它“真实”运行git filter-branch

对我来说是这样的: 我总共提交了4次,并使用了交互式rebase:

git rebase -i HEAD~3

第一次提交仍然存在,我最近进行了3次提交。

如果你卡在接下来出现的编辑器中,你会看到smth:

pick fda59df commit 1
pick x536897 commit 2
pick c01a668 commit 3

你必须先做出承诺,然后把其他人推到上面。你需要的是:

pick fda59df commit 1
squash x536897 commit 2
squash c01a668 commit 3

为此,使用INSERT键更改“插入”和“编辑”模式。

使用:wq保存并退出编辑器。如果您的光标位于这些提交行之间或其他位置,请按ESC并重试。

结果我有两次提交:第一次仍然存在,第二次带有“这是3次提交的组合”的消息。

详情请点击此处: https://makandracards.com/makandra/527-squash-several-git-commits-into-a-single-commit

当我从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是一个中间分支

假设你的分支中有3个提交,并且它已经被推送到远程分支。

例子:

git log -4

将显示如下结果:

<your_third_commit_sha>
<your_second_commit_sha>
<your_first_commit_sha>
<master_branch_commit_sha - your branch created from master>

你想把最后3次提交压缩成一次提交,然后推送到远程分支。以下是步骤。

git reset --soft <master_branch_commit_sha>

现在所有提交的更改都被集成但未提交。验证:

git status

用一条消息提交所有更改:

git commit -m 'specify details'

强制将单次提交推到远程分支:

git push -f
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.")