压缩分支上的多个提交

时间:2019-08-08 06:15:21

标签: git github

我想将最后7次提交压缩为特定分支的1次提交(所有提交都在同一分支the_branch上), 在此之前,我想验证这些是否是必需的步骤

git rebase -i HEAD~7
git commit -m “new commit”
git push origin the_branch

这些是必要步骤还是我应该添加一些内容?

Squash my last X commits together using Git

4 个答案:

答案 0 :(得分:2)

是的,您所指的答案是正确的,建议的步骤不正确:

git rebase -i HEAD~7

现在您需要将除第一个单词以外的所有行的第一个单词从“ pick”更改为“壁球”

此后,您不需要需要创建新的提交。

如果您在压缩提交之前已推送分支,则由于重写了历史记录,您将需要强制推送分支

git push --force-with-lease origin the_branch

如果您以前没有推送过,那么正常的推送就足够了:

git push origin the_branch

答案 1 :(得分:2)

我会进行一次软重置并从此过上幸福的生活:

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

答案 2 :(得分:2)

我认为将git resetsoft选项一起使用会更容易。

git reset --soft HEAD~7

如果不确定HEAD~7,请执行git log --oneline,复制第8次提交的de hash,然后:

git reset --soft <hash_your_commit>

现在,您在最近7次提交中修改的所有文件都在暂存区域中,可以提交了:

git commit -m "7 commits squashed into 1"

最后,要对remote进行提交,如果7个提交中的一项或多项已经在{{1上,则必须使用-f--force选项}}。这样,这些提交也将从remote中消失。

remote

答案 3 :(得分:0)

  • 从主站点重新调整git
  • git fetchgit log只是为了确保您被更新,并且您可以查看日志以确保您的基准正常运行。
  • 在vi编辑器中执行git rebase -i origin/master后,使用选项fixup压缩所有不需要的enter image description here
  • 如果有任何冲突,请使用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
    
相关问题