[英]squash several commits on branch
我想將最后7次提交the_branch
為特定分支的1個提交(所有提交都在同一分支the_branch
),在此之前,我想驗證這些是否是必需的步驟
git rebase -i HEAD~7
git commit -m “new commit”
git push origin the_branch
這些是必要步驟還是我應該添加一些內容?
是的,您所指的答案是正確的,您建議執行的步驟不正確:
git rebase -i HEAD~7
現在您需要將所有行的第一個單詞更改為第一個單詞(從pick改為壁球)
在這之后,你並不需要創建一個新的提交。
如果您在擠壓提交之前已推送分支,則由於重寫了歷史記錄,因此您將需要強制推送分支
git push --force-with-lease origin the_branch
如果您以前沒有推送過,則正常推送就足夠了:
git push origin the_branch
我會進行一次軟重置,然后從此過上幸福的生活:
git checkout --detach
git reset --soft HEAD~7 # move branch pointer 7 revisions back, _DO NOT_ tough my working tree.... all changes between HEAD~7 and the tip of the branch are saved in index, ready to be committed
git commit -m "Blah blah"
# if you like the result
git branch -f my-branch
git checkout my-branch
git push -f origin my-branch # force-push as needed
我認為將git reset
與soft
選項一起使用會更容易。
git reset --soft HEAD~7
如果你不能確定HEAD~7
,做git log --oneline
,復制德8日哈希提交和:
git reset --soft <hash_your_commit>
現在,您在最近7次提交中修改的所有文件都在暫存區域中,可以提交了:
git commit -m "7 commits squashed into 1"
最后,如果將7個提交中的一個或多個提交到了remote
,則必須使用-f
或--force
選項來提交到remote
。 這樣,那些提交也將從remote
消失。
// if none of the 7 commits are on the remote
git push origin branch
//else
git push -f origin branch
git fetch
和git log
只是為了確保您已更新,您可以查看日志以確保您的rebase正常工作。 git rebase -i origin/master
之后,使用選項fixup壓縮所有不需要的提交 git add .
和git rebase --continue
否則您可以忽略此步驟。 只需使用git log
檢查日志以確保您的提交在正確的位置,則不需要此步驟(可選)。
最后,您需要使用git push -f
強制執行
git fetch git log git rebase -i origin/master git status git add . git rebase --continue git log git push -f
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.