![](/img/trans.png)
[英]git automatically squash commits to previous commits that changed the same file/s
[英]Git Squash and remove previous commits
也許我誤解了GIT的工作原理。
我已經運行git rebase -i HEAD~10
,我可以將10次提交壓縮為一個。 問題是所有壓縮的提交仍然存在,並且我認為在將它們全部合並為一個之后, 它們將被丟棄。
這是預期的結果嗎? 如果是這樣,我是否可以重寫歷史記錄以刪除無用的提交(因為這些更改已經存在於以前所有提交已被壓縮的提交中)?
在開始交互式rebase會話時,應該提示您當前分支中最近10次提交的列表:
git rebase -i HEAD~10
pick as2i8dw first commit
pick ee361eb second commit
...
pick b2762sx most recent commit
您需要將此文件更改為以下內容:
pick as2i8dw first commit
squash ee361eb second commit
...
squash b2762sx most recent commit
然后,您需要執行git commit
來保存更改。 現在,當執行git日志時,您應該只會看到as2i8dw
提交,而其他十個都看不到。
話雖這么說,這是你做的嗎?
問題是所有壓縮的提交仍然存在
如果仍然可以通過任何其他引用(其他分支或標記)訪問那些提交,則即使在重新定義當前分支之后,該提交仍然可見。
嘗試使用git reset --soft
。
如果HEAD仍然引用您的10次提交:
git reset --soft HEAD~10
git commit -m "squashed 10 commits"
我遇到了類似的問題,並找出了導致該問題的實際原因:流程:
git rebase -i HEAD~10
# Retain the first commit from below( as pick) and change the rest of the `pick` to `squash`
# After your rebase is successful
git log
# You can see all your commits squashes to one commit
然后,現在當您從遠程分支進行git pull
時,它將拉出本地中不存在的其余提交(基本上是您之前壓縮過的所有提交,因為它現在在一個提交中),因此您看到了以前的提交。
如果您確信沒有在其中添加新的更改,更好的方法是git push -f
到您的遠程分支。
PS:如果您在遠程分支中有任何新更改,最好執行以下操作:
git rebase origin remote_branch
然后壓縮您的提交。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.