Git rebase (Merge Squash) 我的功能分支到另一个分支

标签 git git-merge git-squash

我正在寻找一个 git 命令来帮助我处理我的功能分支,当它们准备好进入 Master 时。这个 git 命令会将我在分支上的所有更改压缩到 master 之上的单个提交中。我今天这样做:

git rebase origin/master
git rebase -i HEAD~4

其中 4 是压缩的提交数。但是,这需要我知道我有多少次提交。我今天通过运行来做到这一点:

git log HEAD...origin/master

然后计算提交数。

我觉得好像应该有更好的方法来做到这一点。或者其他人也是这样做的?

最佳答案

您所要做的就是:

git checkout feature_branch
git rebase master
git checkout master
git merge --squash feature_branch

作为docs对于 git merge --squash 说:

Produce the working tree and index state as if a real merge happened (except for the merge information), but do not actually make a commit or move the HEAD, nor record $GIT_DIR/MERGE_HEAD to cause the next git commit command to create a merge commit. This allows you to create a single commit on top of the current branch whose effect is the same as merging another branch (or more in case of an octopus).

之后,您可以git commit您已经暂存的更改。

关于Git rebase (Merge Squash) 我的功能分支到另一个分支,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9622131/

相关文章:

git - 我的工作树是干净的,但 'git pull' 表示有本地文件将被覆盖

Git 忽略剩余的 merge 冲突

git - 如何拥有一个具有完整历史记录的分支和一个具有压缩历史记录的分支?

git - 是否可以将我最近的提交(更改)保存到目录中?如何?

Git 命令将我从当前目录移到父目录

git - 如何备份 git stash 内容?

git - 如何在执行 git pull 时显示 git 差异?

git - 在 Git 中将本地 master 分支与远程 master 分支 merge

Git squash 在分支中间提交

git - 将压缩的提交从 main merge 到功能分支时避免 git 中的冲突