如何将多个提交合并到另一个分支作为单个压缩提交?

发布于 2024-10-22 03:43:31 字数 249 浏览 1 评论 0原文

我有一个远程 Git 服务器,这是我想要执行的场景:

  • 对于每个错误/功能,我创建一个不同的 Git 分支

  • 我继续使用非官方 Git 消息在该 Git 分支中提交我的代码

  • 在顶级存储库中,我们必须针对一个错误进行一次提交带有官方 Git 消息

那么我如何将我的分支合并到远程分支,以便他们只为我的所有签入获得一次提交(我什至想要为此提供提交消息)?

I have a remote Git server, here is the scenario which I want to perform:

  • For each bug/feature I create a different Git branch

  • I keep on committing my code in that Git branch with un-official Git messages

  • In top repository we have to do one commit for one bug with official Git message

So how can I merge my branch to remote branch so that they get just one commit for all my check-ins (I even want to provide commit message for this)?

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

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

发布评论

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

评论(15

忆梦 2024-10-29 03:43:32
git checkout YOUR_RELEASE_BRANCH
git pull
git checkout -b A_NEW_BRANCH
git merge --squash YOUR_BRANCH_WITH_MULTIPLE_COMMITS
git commit -am "squashing all commits into one"
git push --set-upstream origin A_NEW_BRANCH
git checkout YOUR_RELEASE_BRANCH
git pull
git checkout -b A_NEW_BRANCH
git merge --squash YOUR_BRANCH_WITH_MULTIPLE_COMMITS
git commit -am "squashing all commits into one"
git push --set-upstream origin A_NEW_BRANCH
忆梦 2024-10-29 03:43:32

如果出现错误:无法提交,因为您有未合并的文件。

git checkout master
git merge --squash bugfix
git add .
git commit -m "Message"

所有冲突文件

git add . 

修复了您也可以使用的

git add [filename]

if you get error: Committing is not possible because you have unmerged files.

git checkout master
git merge --squash bugfix
git add .
git commit -m "Message"

fixed all the Conflict files

git add . 

you could also use

git add [filename]
粉红×色少女 2024-10-29 03:43:32

您的功能分支已完成,只需一次提交即可提交到 master、develop 或其他目标分支

  • 转到合并分支: git checkout master && git pull
  • 从干净的本地 master 创建一个工作分支: git checkout -b work
  • 合并压缩工作上的功能分支: git merge --squash your_feature_branch。
  • 使用默认或新消息提交: git commit (使用特定或默认消息)
  • 返回到您的功能分支: git checkout your_feature_branch
  • 将您的功能分支指向工作目录: git reset --hard work
  • 验证但您已准备好推送: git push -f
  • 然后根据需要清理工作分支

将 master 替换为目标分支:开发等

  • 无需指定从 master 到功能分支的提交数量。 Git 负责*

Your feature branch is done and ready to commit to master, develop or other target branch with only one commit

  • Go to merge branch : git checkout master && git pull
  • Create a work branch from your clean local master : git checkout -b work
  • Merge squash your feature branch on work : git merge --squash your_feature_branch.
  • Commit with default or a new message : git commit (with a specific or default message)
  • Go back to your feature branch : git checkout your_feature_branch
  • Point your feature branch to work dir : git reset --hard work
  • Verify but you are ready to push : git push -f
  • Then clean up work branch if needed

Replace master with your target branch : develop and so on

  • No need to specify how many commit from your master to your feature branch. Git takes care*
菩提树下叶撕阳。 2024-10-29 03:43:32

假设您进行多次提交的分支名称称为 bugfix/123,并且您想要压缩这些提交。
首先,从开发创建一个新分支(或者无论您的存储库名称是什么)。假设新分支的名称为 bugfix/123_up。在 git bash 中签出此分支 -

  • git fetch
  • git checkout bugfix/123_up
  • git merge bugfix/123 --squash
  • git commit -m "your message"
  • git push origin bugfix/123_up

现在,该分支将只有一个提交,其中包含您的所有更改。

Assume the name of the branch where you made multiple commits is called bugfix/123, and you want to squash these commits.
First, create a new branch from develop (or whatever the name of your repo is). Assume the name of the new branch is called bugfix/123_up. Checkout this branch in git bash -

  • git fetch
  • git checkout bugfix/123_up
  • git merge bugfix/123 --squash
  • git commit -m "your message"
  • git push origin bugfix/123_up

Now this branch will have only one commit with all your changes in it.

故乡的云 2024-10-29 03:43:32

您可以使用我创建的工具来简化此过程:git-squash。例如,要压缩从 master 分支分支的功能分支上的所有提交,请编写:

git squash master
git push --force

You can use tool I've created to make this process easier: git-squash. For example to squash all commits on feature branch that has been branched from master branch, write:

git squash master
git push --force
遇到 2024-10-29 03:43:32

用于

git status 

检查发生了什么。

然后

git checkout master 
git merge --squash bugfix
git add (add which files you want or use wildcard command like ".")

然后

git commit -m "message"

现在最后但并非最不重要的

git push -u origin master

这里origin可以是您喜欢的其他远程。

Use

git status 

to check what's going on.

Then

git checkout master 
git merge --squash bugfix
git add (add which files you want or use wildcard command like ".")

Then

git commit -m "message"

And now last but not the least

git push -u origin master

Here origin can be other remote you prefer.

微凉徒眸意 2024-10-29 03:43:31

假设您的错误修复分支名为 bugfix 并且您想将其合并到 master 中:

git checkout master
git merge --squash bugfix
git commit

这将获取来自 bugfix 分支的所有提交,squash将它们合并到 1 个提交中,并将其与您的 master 分支合并。


说明

git checkout master

切换到您的 master 分支。

git merge --squash bugfix

bugfix 分支获取所有提交,并将其分组为当前分支的 1 次提交。
(没有出现合并提交;您可以在提交之前手动解决冲突)

git commit

从合并的更改创建单个提交。

省略 -m 参数可让您在最终提交之前修改草稿提交消息,其中包含压缩提交中的每条消息。

Say your bug fix branch is called bugfix and you want to merge it into master:

git checkout master
git merge --squash bugfix
git commit

This will take all the commits from the bugfix branch, squash them into 1 commit, and merge it with your master branch.


Explanation:

git checkout master

Switches to your master branch.

git merge --squash bugfix

Takes all commits from the bugfix branch and groups it for a 1 commit with your current branch.
(no merge commit appears; you could resolve conflicts manually before following commit)

git commit

Creates a single commit from the merged changes.

Omitting the -m parameter lets you modify a draft commit message containing every message from your squashed commits before finalizing your commit.

つ低調成傷 2024-10-29 03:43:31

最终为我解决这个问题的是 评论 表明:

git checkout main
git merge --squash feature

相当于执行以下操作:

git checkout feature
git diff main > feature.patch
git checkout main
patch -p1 < feature.patch
git add .

当我想要合并具有 105(!!) 次提交的功能分支并将它们全部压缩为一个时,我不想 git rebase -i origin/master ,因为我需要单独解析合并每个中间提交(或者至少是 git 无法弄清楚的提交)都有冲突。使用 git merge --squash 可以获得我想要的结果,即用于合并整个功能分支的单个提交。而且,我最多只需要进行一次手动冲突解决。

What finally cleared this up for me was a comment showing that:

git checkout main
git merge --squash feature

is the equivalent of doing:

git checkout feature
git diff main > feature.patch
git checkout main
patch -p1 < feature.patch
git add .

When I want to merge a feature branch with 105(!!) commits and have them all squashed into one, I don't want to git rebase -i origin/master because I need to separately resolve merge conflicts for each of the intermediate commits (or at least the ones which git can't figure out itself). Using git merge --squash gets me the result I want, of a single commit for merging an entire feature branch. And, I only need to do at most one manual conflict resolution.

冷心人i 2024-10-29 03:43:31

您想与挤压选项合并。如果您想一次只处理一个分支,那就是这样。

git merge --squash feature1

如果您想在单个提交的同时合并所有分支,那么首先以交互方式变基并压缩每个功能,然后进行章鱼合并:

git checkout feature1
git rebase -i master

压缩到一个提交,然后对其他功能重复。

git checkout master
git merge feature1 feature2 feature3 ...

最后一次合并是“章鱼合并”,因为它一次合并了很多分支。

You want to merge with the squash option. That's if you want to do it one branch at a time.

git merge --squash feature1

If you want to merge all the branches at the same time as single commits, then first rebase interactively and squash each feature then octopus merge:

git checkout feature1
git rebase -i master

Squash into one commit then repeat for the other features.

git checkout master
git merge feature1 feature2 feature3 ...

That last merge is an "octopus merge" because it's merging a lot of branches at once.

清欢 2024-10-29 03:43:31

假设您在 feature/task1 中进行了多次提交。

  1. 转到您的项目分支 (project/my_project)

     git checkout 项目/my_project
    
  2. 创建一个新分支 (feature/task1_bugfix)

     git checkout -b feature/task1_bugfix
    
  3. --squash 选项合并

     git merge --squash feature/task1
    
  4. 创建单个提交

     git commit -am "添加单个注释"
    
  5. 推送您的分支

     git push --set-upstream origin feature/task1_bugfix
    

Suppose you worked in feature/task1 with multiple commits.

  1. Go to your project branch (project/my_project)

     git checkout project/my_project
    
  2. Create a new branch (feature/task1_bugfix)

     git checkout -b feature/task1_bugfix
    
  3. Merge with the --squash option

     git merge --squash feature/task1
    
  4. Create a single commit

     git commit -am "add single comments"
    
  5. Push your branch

     git push --set-upstream origin feature/task1_bugfix
    
七七 2024-10-29 03:43:31

使用自定义提交将 newFeature 分支合并到 master 中:

git merge --squash newFeature && git commit -m 'Your custom commit message';

如果相反,您可以执行

git merge --squash newFeature && git commit

您将收到一条提交消息,其中包含您可以自定义的所有 newFeature 分支提交。

我在这里彻底解释一下:https://youtu.be/FQNAIacelT4

Merge newFeature branch into master with a custom commit:

git merge --squash newFeature && git commit -m 'Your custom commit message';

If instead, you do

git merge --squash newFeature && git commit

you will get a commit message that will include all the newFeature branch commits, which you can customize.

I explain it thoroughly here: https://youtu.be/FQNAIacelT4

泪痕残 2024-10-29 03:43:31

如果您已经在 main 上进行了 git merge bugfix,则可以使用以下命令将合并提交压缩为一个:

git reset --soft HEAD^1
git commit

If you have already git merge bugfix on main, you can squash your merge commit into one with:

git reset --soft HEAD^1
git commit
救赎№ 2024-10-29 03:43:31

我知道这个问题并不是专门针对 Github 的,但由于 Github 的使用非常广泛,而且这就是我一直在寻找的答案,所以我将在这里分享。

Github 能够执行压缩合并,具体取决于存储库启用的合并选项。

如果启用了挤压合并,则“挤压并合并”选项应出现在“合并”按钮下方的下拉列表中。

屏幕截图“压缩并合并”Github 功能

I know this question isn't about Github specifically, but since Github is so widely used and this is the answer I was looking for, I'll share it here.

Github has the ability to perform squash merges, depending on the merge options enabled for the repository.

If squash merges are enabled, the "Squash and merge" option should appear in the dropdown under the "Merge" button.

Screenshot of "Squash and merge" Github feature

梦归所梦 2024-10-29 03:43:31

要在推送之前压缩本地分支:

  1. 如果尚未签出,请签出要处理的相关分支。

  2. 查找您希望保留的最旧提交的 sha。

  3. 从该提交创建/签出一个新分支 (tmp1)。

    git checkout -b tmp1

  4. 将原始分支合并到新分支中。

    git merge --squash <原始分支>

  5. 提交合并创建的更改,并带有摘要提交消息。

    git commit -m

  6. 签出您想要压缩的原始分支。

    git checkout

  7. 重置为您希望保留的原始提交 sha。

    git reset --soft

  8. 根据新的 tmp1 分支重新设置此分支的基础。

    git rebase tmp1

  9. 就是这样 - 现在,一旦您确定一切正常,就删除临时 tmp1 分支。

To squash your local branch before pushing it:

  1. checkout the branch in question to work on if it is not already checked out.

  2. Find the sha of the oldest commit you wish to keep.

  3. Create/checkout a new branch (tmp1) from that commit.

    git checkout -b tmp1 <sha1-of-commit>

  4. Merge the original branch into the new one squashing.

    git merge --squash <original branch>

  5. Commit the changes which have been created by the merge, with a summary commit message.

    git commit -m <msg>

  6. Checkout the original branch you want to squash.

    git checkout <branch>

  7. Reset to the original commit sha you wish to keep.

    git reset --soft <sha1>

  8. Rebase this branch based on the new tmp1 branch.

    git rebase tmp1

  9. That's it - now delete the temporary tmp1 branch once you're sure everything is ok.

奢望 2024-10-29 03:43:31

对于 Git

创建新功能

通过终端/Shell

git checkout origin/feature/<featurename>
git merge --squash origin/feature/<featurename>

:这不会提交它,允许您先查看它。

然后提交并完成这个新分支的功能,并删除/忽略旧分支(您进行开发的分支)。

For Git

Create a new feature

via Terminal/Shell:

git checkout origin/feature/<featurename>
git merge --squash origin/feature/<featurename>

This doesnt commit it, allows you to review it first.

Then commit, and finish feature from this new branch, and delete/ignore the old one (the one you did dev on).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文