如何将整个存储库压缩到第一次提交?
我可以将base转换为第一次提交,但这将留给我2次提交。 有没有办法在第一个提交之前引用这个提交?
如何将整个存储库压缩到第一次提交?
我可以将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
其他回答
假设你有这些从06年到10年的提交。
您希望将06年后的提交合并为一个提交Id。
commit 10
commit 09
commit 08
commit 07
commit 06
你可以软复位git。
git reset --soft "06"
然后,运行下面的命令将这些更改推送到远程分支。
git push origin HEAD --force
现在,您之前所做的所有提交都应该作为您的本地更改可用,您可以将所有这些提交合并到单个提交中。
现在,新的提交结构应该如下所示:
commit <new_commit_containing_all_7_8_9_10>
commit 06
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.")
对我来说是这样的: 我总共提交了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 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
一个衬套
你可以直接从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")
这避免了将模板回购作为远程(源或其他)添加,并将模板回购的历史记录折叠到初始提交中。