我用:
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
当前回答
我知道这个问题已经有了答案,但我围绕已接受的答案编写了一个bash函数,以允许您在一个命令中完成它。它首先创建一个备份分支,以防压缩由于某种原因失败。然后压缩并提交。
# Squashes every commit starting after the given head of the given branch.
# When the squash is done, it will prompt you to commit the squash.
# The head of the given parent branch must be a commit that actually exists
# in the current branch.
#
# This will create a backup of the current branch before it performs the squash.
# The name of the backup is the second argument to this function.
#
# Example: $ git-squash master my-current-branch-backup
git-squash() {
PARENT_BRANCH=$1
BACKUP_BRANCH=$2
CURRENT_BRANCH=$(git branch --show-current)
git branch $BACKUP_BRANCH
BACKUP_SUCCESS=$?
if [ $BACKUP_SUCCESS -eq 0 ]; then
git reset $(git merge-base $PARENT_BRANCH $CURRENT_BRANCH)
git add -A
git commit
echo "Squashed $CURRENT_BRANCH. Backup of original created at $BACKUP_BRANCH$"
else
echo "Could not create backup branch. Aborting squash"
fi
}
其他回答
你正在做的事情很容易出错。只做:
git rebase -i master
它会自动将你的分支的提交重置到当前最新的主节点上。
另一种压缩所有提交的方法是将索引重置为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))
在阅读了几个关于压缩的Stackoverflow问题和答案后,我认为这是一个很好的压缩分支上所有提交的代码行:
git reset --soft $(git merge-base master YOUR_BRANCH) && git commit -am "YOUR COMMIT MESSAGE" && git rebase -i master
这是假设master是基础分支。
如果你可以接受涉及另一个分支的答案,尝试git checkout——orphan <new_branch>它允许我简单地将前一个分支的所有文件作为一个提交。
这有点像git的合并压缩,但不完全相同。
所有这些git重置,硬的,软的,以及这里提到的所有其他东西都可能工作(它不适合我),如果你正确地执行步骤和某种精灵。 如果你是普通的smo,试试这个: 如何使用git合并-squash?
Saved my life, and will be my go to squash, been using this 4 times since I found out about it. Simple, clean and basically 1 comamnd. In short: If you are on a branch lets call it "my_new_feature" off develop and your pull request has 35 commits (or however many) and you want it to be 1. A. Make sure your branch is up to date, Go on develop, get latest and merge and resolve any conflicts with "my_new_feature" (this step really you should take as soon as you can all the time anyway) B. Get latest of develop and branch out to a new branch call it "my_new_feature_squashed"
C.魔法在这里。 你想把你的工作从“my_new_feature”变成“my_new_feature_squashed” 所以只要这样做(在我们创建off develop的新分支上): Git merge—squash my_new_feature
你所有的改变现在都在你的新分支上,你可以自由地测试它,然后只做一个单一的提交,推送,那个分支的新PR——然后等待第二天重复。 你不喜欢编程吗?:)