在 Git 上推送后如何合并提交?

发布于 2024-12-01 22:19:06 字数 203 浏览 3 评论 0原文

我是git新手,我提交了一些更改,git告诉我需要先拉取,所以我拉取了,但是我的编辑器没有显示修改后的版本,所以我再次提交了冲突,我解决了冲突并再次提交。现在我有 3 个提交! (虽然我认为第一个不应该在那里)

问题是我每次提交后都会推送。那么有没有办法合并这些推送的提交呢?

您能提供一步一步的解释吗?我以前使用过 svn 和 cvs,但我对 git 很陌生

I am new to git, I committed some changes, git told me that I need to pull first, so I pulled but my editor didn't display the modified versions, so I committed again with conflicts, I resolved the conflicts and committed again. Now I have 3 commits! (although I think the first one should not be there)

The problem is that I pushed every time after each commit. So is there a way to merge those pushed commits?

Can you please provide a step by step explanation? I have used svn and cvs before but I am very new to git

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

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

发布评论

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

评论(2

流心雨 2024-12-08 22:19:07

您描述的情况似乎不太可能,因为在冲突时尝试提交会导致以下错误:

fatal: 'commit' is not possible because you have unmerged files.
Please, fix them up in the work tree, and then use 'git add/rm <file>' as
appropriate to mark resolution and make a commit, or use 'git commit -a'.

如果您无法提交冲突,那么运行 git push 实际上不会推送更改。

您可以运行以下命令来查看实际发生的提交

git log --patch --color

The situation you described seems unlikely as trying to commit while in conflict results in the following error:

fatal: 'commit' is not possible because you have unmerged files.
Please, fix them up in the work tree, and then use 'git add/rm <file>' as
appropriate to mark resolution and make a commit, or use 'git commit -a'.

And if you couldn't commit the conflict then running git push wouldn't actually push the change.

You can run the following command to see what commits actually took place

git log --patch --color
2024-12-08 22:19:07

如果我正确理解你的问题,这应该可以满足你的要求:

你可以使用 git rebase -i HEAD~3 将所有三个提交压缩在一起。这基本上将摆脱您的错误提交,并且最后一个提交中的内容将是最终内容。

当您运行该命令时,将弹出一个编辑器。将第二行和第三行中的 pick 替换为 squash。您将需要使用 -f 标志再次推送。

如果它们是您的最后三个提交,这将起作用。如果他们比这还落后的话。您将需要进一步变基,并且仅将 squash 放在要压缩的两个提交上。

If I understand your question correctly, this should do what you want:

You can squash all three of those commits together with git rebase -i HEAD~3. This will basically get rid of your bad commits and the content in your last one will be the final content.

When you run the command, an editor will pop up. Replace pick in the second and third line with squash. You will than need to push again with the -f flag.

This will work if they are your last three commits. If they are farther behind than that. You will need to rebase farther back and only put squash on the two commits you want to squash.

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