我用:
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
当前回答
为了完善一下Caveman的回答,使用git reset——soft <commit>。从文档中,这个命令:
根本不触及索引文件或工作树(但将头部重置为<commit>,就像所有模式一样)。这将使所有更改过的文件都变成“要提交的更改”,就像git状态所显示的那样。
换句话说,它将撤销到<commit>之前的所有提交。但是它不会改变工作目录。您最终会得到所有的更改,这些更改都是未分期和未提交的。就好像那些介入的提交从未发生过一样。
例子:
# on master
git checkout -b testbranch
# make many commits
git reset --soft master
git add .
git commit -m 'The only commit.'
此时,您仍然在testbranch上,它只有一次提交。像往常一样合并到master中。
在我的手中,Caveman回答的第一部分(git rebase -i)并没有压缩提交。
其他回答
所有这些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——然后等待第二天重复。 你不喜欢编程吗?:)
How
你需要得到分支的合并基
git merge-base master your-branch
# 566f8438e0cd0e331ceb49a9cb0920143dfb065c
然后你就可以改基了
git rebase -i 566f8438e0cd0e331ceb49a9cb0920143dfb065c
# then squash/pick/do commit messages
或者只是进行软重置并提交所有内容
git reset --soft 566f8438e0cd0e331ceb49a9cb0920143dfb065c
git add .
git commit -m "The only commit"
自动化
如果你经常这样做,你可以把这些放在你的.bashrc使用自动化。
g-rebase-branch() {
git branch --show-current | xargs git merge-base master | xargs git rebase -i
}
g-one-commit() {
local last_commit_message=`git show -s --format=%s`
git branch --show-current | xargs git merge-base master | xargs git reset --soft
git add -A
git commit -m "$last_commit_message"
git commit --amend
}
然后直接在终端上做这些。
g-one-commit
但如果你合并的是与master不同的分支,那么你可以用“$1”替换master来做到这一点
g-one-commit staging
我知道这个问题已经有了答案,但我围绕已接受的答案编写了一个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 HEAD~$(git rev-list -count HEAD ^master)
这将首先计算从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