如何创建一个兄弟分支,其中包含从另一个分支到一个节点的所有提交?

发布于 2024-12-02 06:46:51 字数 246 浏览 4 评论 0原文

我有一个分支 mybranch,它有 6 个提交,源自提交 C。我想创建一个新分支,也基于提交 C,它有效地包含来自 mybranch 的所有提交,并压缩为一个提交。做到这一点最简单的方法是什么?

由于“在 git-push 之前压缩”的口头禅,我需要这样做。对于新分支,我不希望在推送到远程服务器的历史记录中提及 mybranch。我想创建一个用于推送的全新分支的原因是因为我想保留 mybranch 中的所有提交以供参考。

I have a branch, mybranch, that has six commits and stems from commit C. I would like to create a new branch, also based on commit C, that effectively includes all the commits from mybranch, squashed into one commit. What's the easiest way to do this?

I need to do this due to the mantra of "squash before you git-push". For the new branch, I don't want any mention of mybranch in the history pushed up to the remote server. The reason I want to create a whole new branch used to push is because I'd like to keep all the commits in mybranch for reference.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝海似她心 2024-12-09 06:46:51

为什么只推送压缩的提交?这听起来很疯狂(而且是错误的)。

但要回答你的问题:

git checkout -b newbranch # checkout new branch
git reset --soft C # move to commit C
git commit -m 'squashed commit'

另一种可能性是使用 rebase -i 将所有提交压缩/修复为单个提交:

git checkout -b newbranch
git rebase -i C # now, replace every 'pick' with 'fixup'/'squash' except the first one

同样有趣的是 git merge --squash

像真正发生合并一样生成工作树和索引状态(合并信息除外),但不实际进行提交或移动 HEAD,也不记录 $GIT_DIR/MERGE_HEAD 以使下一个 git commit 命令执行创建合并提交。这允许您在当前分支之上创建单个提交,其效果与合并另一个分支相同(或者在章鱼的情况下更多)。

git checkout -b newbranch C # create newbranch at commit C
git merge --squash mybranch
git commit -m 'squashed commit'

Why only push squashed commits? That sounds crazy (and wrong).

But to answer your question:

git checkout -b newbranch # checkout new branch
git reset --soft C # move to commit C
git commit -m 'squashed commit'

Another possibility would be to use rebase -i to squash/fixup all commits into a single one:

git checkout -b newbranch
git rebase -i C # now, replace every 'pick' with 'fixup'/'squash' except the first one

Also of interest is 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 checkout -b newbranch C # create newbranch at commit C
git merge --squash mybranch
git commit -m 'squashed commit'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文