如何将整个存储库压缩到第一次提交?
我可以将base转换为第一次提交,但这将留给我2次提交。 有没有办法在第一个提交之前引用这个提交?
如何将整个存储库压缩到第一次提交?
我可以将base转换为第一次提交,但这将留给我2次提交。 有没有办法在第一个提交之前引用这个提交?
当前回答
这个答案改进了上面的几个(请投票),假设除了创建一个提交(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
')
其他回答
我读过一些关于移植的东西,但从来没有深入研究过。
不管怎样,你可以手动压缩最后2次提交,就像这样:
git reset HEAD~1
git add -A
git commit --amend
我通常是这样做的:
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 merge origin/master && git reset --soft origin/master
前面的合并cmd确保,在提交时,没有来自master的最近更改会出现在您的头上!之后,只需提交更改并执行git push -f
也许最简单的方法是用工作副本的当前状态创建一个新的存储库。如果你想保留所有的提交消息,你可以先执行git log > original.log,然后在新的存储库中编辑你的初始提交消息:
rm -rf .git
git init
git add .
git commit
or
git log > original.log
# edit original.log as desired
rm -rf .git
git init
git add .
git commit -F original.log
对我来说是这样的: 我总共提交了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