如何将整个存储库压缩到第一次提交?
我可以将base转换为第一次提交,但这将留给我2次提交。 有没有办法在第一个提交之前引用这个提交?
如何将整个存储库压缩到第一次提交?
我可以将base转换为第一次提交,但这将留给我2次提交。 有没有办法在第一个提交之前引用这个提交?
当前回答
从git 1.6.2开始,你可以使用git rebase——root -i
对于除第一次提交外的每个提交,将pick更改为squash。
其他回答
我通常是这样做的:
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/info/grafts,把你想要成为根的提交散列放在那里
Git日志现在将从该提交开始
要让它“真实”运行git filter-branch
要做到这一点,你可以将本地git存储库重置为第一个commit标签,这样你在提交后的所有更改都将被取消,然后你可以使用——modify选项提交。
git reset your-first-commit-hashtag
git add .
git commit --amend
然后编辑第一个提交nam如果需要和保存文件。
假设你的分支中有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 merge origin/master && git reset --soft origin/master
前面的合并cmd确保,在提交时,没有来自master的最近更改会出现在您的头上!之后,只需提交更改并执行git push -f