我用:
git checkout -b testbranch
我做了20次提交。
现在我想要压缩这20个提交。我是这样做的:
git rebase -i HEAD~20
如果我不知道有多少次提交呢?有没有什么方法可以做到:
git rebase -i all on this branch
我用:
git checkout -b testbranch
我做了20次提交。
现在我想要压缩这20个提交。我是这样做的:
git rebase -i HEAD~20
如果我不知道有多少次提交呢?有没有什么方法可以做到:
git rebase -i all on this branch
当前回答
签出您希望将所有提交压缩为一次提交的分支。我们说它叫feature_branch。
git checkout feature_branch
步骤1:
用你的本地主分支对你的origin/feature_branch进行软重置(根据你的需要,你也可以用origin/main重置)。这将重置feature_branch中所有额外的提交,但不会在本地更改任何文件更改。
git reset --soft main
步骤2:
将git repo目录中的所有更改添加到将要创建的新提交中。并通过信息提交相同的信息。
# Add files for the commit.
git add ...
git commit -m "commit message goes here"
其他回答
在之前的回答中,我没有看到任何关于如何处理“凌乱的分支”和“自我冲突”的信息。例如,我经常在我的特性分支(称为特性)上有主提交,这导致了它们之间的冲突。我发现这是最难处理的问题之一。
如何挤压凌乱的树枝?使用临时分支!
我发现Felix Rieseberg的解决方案是最好的。以下是我对他的建议略短的抄写:
Create a local tmp branch of off master git checkout master && git pull && git checkout -b tmp Merge all feature changes into tmp (without any commits, only staged file changes). git merge --squash $feature Manually solve all remaining "real conflicts" (This is the only step you cannot have a script do for you) Commit. tmp is now master + 1 commit (containing all changes). git commit ... Checkout feature and git reset --hard tmp (feature's original contents are gone, and it is now basically tmp, but renamed) git checkout $feature && git reset --hard tmp Ignore and override origin/feature (then clean up) git push -f && git branch -D tmp
Felix指出,这将产生最干净的合并,没有任何来自master和feature之间混乱/复杂关系的奇怪的自我冲突:
您可能会遇到一些不可避免的合并冲突。请相信,这是尽可能少的冲突,因为您跳过了最初创建的许多中间提交。
另一种压缩所有提交的方法是将索引重置为master:
git checkout yourBranch
git reset $(git merge-base master $(git branch --show-current))
git add -A
git commit -m "one commit on yourBranch"
这并不完美,因为这意味着你知道“你的分支”来自哪个分支。 注意:在Git中找到原始分支并不容易/不可能(可视化的方式通常是最简单的,如图所示)。
注意:git分支——show-current已在git 2.22(2019年第二季度)中引入。
或者,正如Hiroki Osame在评论中指出的那样:
git switch yourBranch
git reset --soft $(git merge-base main HEAD)
git commit -m "one commit on yourBranch"
不需要git分支——show-current,因为HEAD已经是该分支的引用。 不需要git add -A,因为git重置-soft只移动HEAD,并保持索引不变(换句话说,文件已经“添加”)。
编辑:你将需要使用git push——force
Karlotcha Hoa在评论中写道:
对于重置,可以这样做 git reset $(git merge-base master $(git rev-parse——abbrev-ref HEAD)) 自动使用您当前所在的分支。 如果使用它,还可以使用别名,因为该命令不依赖于分支名称。
Sschoof在评论中补充道:
因为我的默认分支被称为main,我的搜索多次把我带到这里: 为我下次上课抄一份 git reset $(git merge-base main $(git rev-parse——abbrev-ref HEAD))
你正在做的事情很容易出错。只做:
git rebase -i master
它会自动将你的分支的提交重置到当前最新的主节点上。
Git重置,正如之前在许多回答中提到的,是迄今为止实现你想要的最好和最简单的方法。我在以下工作流程中使用它:
(有关发展分支)
git fetch
git merge origin/master #so development branch has all current changes from master
git reset origin/master #will show all changes from development branch to master as unstaged
git gui # do a final review, stage all changes you really want
git commit # all changes in a single commit
git branch -f master #update local master branch
git push origin master #push it
签出您希望将所有提交压缩为一次提交的分支。我们说它叫feature_branch。
git checkout feature_branch
步骤1:
用你的本地主分支对你的origin/feature_branch进行软重置(根据你的需要,你也可以用origin/main重置)。这将重置feature_branch中所有额外的提交,但不会在本地更改任何文件更改。
git reset --soft main
步骤2:
将git repo目录中的所有更改添加到将要创建的新提交中。并通过信息提交相同的信息。
# Add files for the commit.
git add ...
git commit -m "commit message goes here"