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

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


当前回答

最简单的方法是使用'plumbing'命令update-ref删除当前分支。

你不能使用git branch -D,因为它有一个安全阀来阻止你删除当前的分支。

这会让你回到“初始提交”状态,在那里你可以开始一个新的初始提交。

git update-ref -d refs/heads/master
git commit -m "New initial commit"

其他回答

要做到这一点,你可以将本地git存储库重置为第一个commit标签,这样你在提交后的所有更改都将被取消,然后你可以使用——modify选项提交。

git reset your-first-commit-hashtag
git add .
git commit --amend

然后编辑第一个提交nam如果需要和保存文件。

创建备份

git branch backup

重置为指定提交

git reset --soft <#root>

然后将所有文件添加到登台

git add .

提交而不更新消息

git commit --amend --no-edit

推送新分支与压缩提交到回购

git push -f

这对我来说效果最好。

git rebase -X ours -i master

这将使git更喜欢你的特性分支;避免艰苦的合并编辑。您的分支需要与主机同步。

ours
           This resolves any number of heads, but the resulting tree of the merge is always that of the current
           branch head, effectively ignoring all changes from all other branches. It is meant to be used to
           supersede old development history of side branches. Note that this is different from the -Xours
           option to the recursive merge strategy.

一行6个字:

git checkout --orphan new_root_branch  &&  git commit

假设你的分支中有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