我用:

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 checkout -b temp
git checkout yourbranch
git fetch
git reset --hard origin/master
git merge --squash temp
git commit -m "new message" 

最简单的方法。

这将创建一个新的分支,然后将你的分支重置为基础分支,然后在将临时分支合并回我们的分支之前,我们压缩更改并创建一个新的提交

假设你从master分支,你不需要一直输入你的branch到重置步骤:

git checkout yourBranch
git reset --soft HEAD~$(git rev-list --count HEAD ^master)
git add -A
git commit -m "one commit on yourBranch"

解释:

git revlist——count HEAD ^master自master创建feature分支以来的提交次数,f.ex。20. git reset——soft HEAD~20将对最近20次提交进行软重置。这将把更改保留在文件中,但删除提交。

用法:

在我的.bash_profile中,我为gisquash添加了一个别名,用一个命令就可以做到这一点:

# squash all commits into one
alias gisquash='git reset --soft HEAD~$(git rev-list --count HEAD ^master)'

在重置和提交后,你需要做一个git push -force。

提示:

如果你正在使用Gitlab >= 11.0,你就不需要再这样做了,因为它在合并分支时有一个压缩选项。

我知道这个问题已经有了答案,但我围绕已接受的答案编写了一个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重置,硬的,软的,以及这里提到的所有其他东西都可能工作(它不适合我),如果你正确地执行步骤和某种精灵。 如果你是普通的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——然后等待第二天重复。 你不喜欢编程吗?:)

假设你在特征分支上:

在特性分支中找到第一个提交。如果你正在使用gitlab或github,你可以直接在分支中查看它,并从那里复制散列,或者你可以使用以下命令:

Git日志<source_branch>..< feature_branch >——漂亮=格式:% h

执行以下命令:

git reset --soft <base_commit_hash>

git commit --amend --no-edit

现在在这个阶段,在您的本地,您有一个提交,其中包括在所有以前的提交中所做的更改。

回顾它,你需要用力推它。在强制推送之后,所有的更改都将合并到一个提交中,而您的分支将只有1个提交。

在特征分支中强制推送

git push --force