什么可能导致“提前提交的数量”?变基后要改变吗?

发布于 2024-10-17 10:15:39 字数 140 浏览 5 评论 0 原文

在对我几周没有碰过的一个功能分支进行变基之前,它比 master 提前了 25 次提交。变基后,现在有 18 次提交。一路上我必须解决一些冲突。可能正好是 7。

什么可能导致这个数字变化?沿途发现的樱桃选择并变成了 NOOP 提交?上述冲突解决方案?

Before rebasing a feature branch that I hadn't touched in a few weeks, it was 25 commits ahead of master. After rebasing, it is now 18 commits. There were several conflicts I had to resolve along the way. Possibly exactly 7.

What could cause this number change? Cherry picks that were discovered along the way and turned into NOOP commits? The above mentioned conflict resolutions?

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

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

发布评论

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

评论(2

深白境迁sunset 2024-10-24 10:15:39

有几种可能发生这种情况的方式 - 这不是一个详尽的列表,但应该让您了解这是如何发生的:

1. 精心挑选的提交

首先,您提到来自您的分支的提交已被择优挑选到您要变基的上游分支:只是为了确认这一点,git rebase 将跳过任何此类提交,这可能会导致部分差异。 VonC这个问题

2. 冲突完全解决到上游版本

我最初的怀疑是,当您进行变基并看到冲突时,您总是通过选择上游版本而不是您的更改来解决它们。 (如果这没有敲响警钟,那么我的怀疑可能是完全错误的:))如果该提交没有引入其他非冲突的更改,那么您的冲突解决方案所做的就是使树相同就像在尝试应用您的更改中的补丁之前一样,因此无需提交任何内容。

不幸的是,当发生这种情况时 git 给你的提示可能相当令人困惑。以下是您将在 git 1.7.1 中看到的示例。首先,提交一个将与尚未从 origin/master 合并的更改发生冲突的更改:

$ git commit
[master 1efa20f] Add a change designed to conflict
 1 files changed, 1 insertions(+), 1 deletions(-)

...现在开始变基:

$ git rebase origin/master 
First, rewinding head to replay your work on top of it...
Applying: Add a change designed to conflict
Using index info to reconstruct a base tree...
Falling back to patching base and 3-way merge...
Auto-merging src-plugins/3D_Viewer/ij3d/Image3DUniverse.java
CONFLICT (content): Merge conflict in src-plugins/3D_Viewer/ij3d/Image3DUniverse.java
Failed to merge in the changes.
Patch failed at 0001 Add a change designed to conflict

When you have resolved this problem run "git rebase --continue".
If you would prefer to skip this patch, instead run "git rebase --skip".
To restore the original branch and stop rebasing run "git rebase --abort".

看看冲突是什么:

$  git diff
diff --cc src-plugins/3D_Viewer/ij3d/Image3DUniverse.java
index 36ec046,f4841ec..0000000
--- a/src-plugins/3D_Viewer/ij3d/Image3DUniverse.java
+++ b/src-plugins/3D_Viewer/ij3d/Image3DUniverse.java
@@@ -264,7 -264,7 +264,11 @@@ public class Image3DUniverse extends De
        public void cleanup() {
                timeline.pause();
                removeAllContents();
++<<<<<<< HEAD
 +              contents.clear();
++=======
+               contents.clear(); // A change designed to conflict
++>>>>>>> Add a change designed to conflict
                universes.remove(this);
                adder.shutdownNow();
                executer.flush();

我已经决定我喜欢来自上游的版本,因此编辑文件来解决这个问题:

$  vim src-plugins/3D_Viewer/ij3d/Image3DUniverse.java

暂存该冲突的解决方案:

$ git add src-plugins/3D_Viewer/ij3d/Image3DUniverse.java

现在尝试像往常一样继续进行变基:

$ git rebase --continue
Applying: Add a change designed to conflict
No changes - did you forget to use 'git add'?

When you have resolved this problem run "git rebase --continue".
If you would prefer to skip this patch, instead run "git rebase --skip".
To restore the original branch and stop rebasing run "git rebase --abort".

我认为在最新版本的 git 中,它表明您可能刚刚解决了对已经存在的内容的提交,并且你应该考虑做 git rebase --skip 。然而,通常人们会认为这是摆脱这种情况的唯一方法,因此:

$  git rebase --skip
HEAD is now at f3a2de3 3D Viewer: Avoid NPE when closing the viewer window.
Nothing to do.

该提交现在不会出现在 git log 中。

3. 合并提交

您在下面的评论中提到,您在检查两个分支时看到了几次合并提交 - 这也可能是丢失提交的原因,因为 git rebase 默认情况下会忽略任何合并提交它准备其提交列表以重新应用到上游。

There are a few possible ways this could happen - this isn't an exhaustive list, but should give you the idea of how this can come about:

1. Cherry-picked commits

Firstly, you mention the possibility that commits from your branch had been cherry-picked into the upstream branch that you're rebasing onto: just to confirm this, git rebase will skip any such commits, which might account for some part of the discrepancy. There's more on that in VonC's answer to this question.

2. Conflicts resolved to exactly the upstream version

My initial suspicion was that what happened was that when you were rebasing and saw conflicts, you always resolved them by choosing the upstream version rather than your changes. (If this doesn't ring a bell, then my suspicion is probably completely wrong :)) If there were no other non-conflicting changes introduced by that commit, then what your conflict resolution will have done is to just make the tree the same as it was before it tried to apply the patch from your change, so there's nothing to commit.

Unfortunately, the prompts that git gives you when this happens can be rather confusing. Here's an example of what you'll see with git 1.7.1. First, commit a change that's going to conflict with one that's not yet been merged from origin/master:

$ git commit
[master 1efa20f] Add a change designed to conflict
 1 files changed, 1 insertions(+), 1 deletions(-)

... now start the rebase:

$ git rebase origin/master 
First, rewinding head to replay your work on top of it...
Applying: Add a change designed to conflict
Using index info to reconstruct a base tree...
Falling back to patching base and 3-way merge...
Auto-merging src-plugins/3D_Viewer/ij3d/Image3DUniverse.java
CONFLICT (content): Merge conflict in src-plugins/3D_Viewer/ij3d/Image3DUniverse.java
Failed to merge in the changes.
Patch failed at 0001 Add a change designed to conflict

When you have resolved this problem run "git rebase --continue".
If you would prefer to skip this patch, instead run "git rebase --skip".
To restore the original branch and stop rebasing run "git rebase --abort".

Look at what the conflict was:

$  git diff
diff --cc src-plugins/3D_Viewer/ij3d/Image3DUniverse.java
index 36ec046,f4841ec..0000000
--- a/src-plugins/3D_Viewer/ij3d/Image3DUniverse.java
+++ b/src-plugins/3D_Viewer/ij3d/Image3DUniverse.java
@@@ -264,7 -264,7 +264,11 @@@ public class Image3DUniverse extends De
        public void cleanup() {
                timeline.pause();
                removeAllContents();
++<<<<<<< HEAD
 +              contents.clear();
++=======
+               contents.clear(); // A change designed to conflict
++>>>>>>> Add a change designed to conflict
                universes.remove(this);
                adder.shutdownNow();
                executer.flush();

I've decided I like the version from upstream, so edit the file to resolve that:

$  vim src-plugins/3D_Viewer/ij3d/Image3DUniverse.java

Stage the resolution of that conflict:

$ git add src-plugins/3D_Viewer/ij3d/Image3DUniverse.java

Now try to continue with the rebase as usual:

$ git rebase --continue
Applying: Add a change designed to conflict
No changes - did you forget to use 'git add'?

When you have resolved this problem run "git rebase --continue".
If you would prefer to skip this patch, instead run "git rebase --skip".
To restore the original branch and stop rebasing run "git rebase --abort".

I think that in more recent versions of git, it suggests that you might have just resolved the commit to what was there already, and that you should consider is doing git rebase --skip. However, typically people work out that that's the only way out of this situation anyway, so:

$  git rebase --skip
HEAD is now at f3a2de3 3D Viewer: Avoid NPE when closing the viewer window.
Nothing to do.

That commit will now not appear in git log.

3. Merge commits

You mention in the comments below that you saw several merge commits when examining the two branches afterwards - this could also be a cause of missing commits, since git rebase by default ignores any merge commits when it prepares its list of commits to reapply onto upstream.

凉墨 2024-10-24 10:15:39

要扩展 Jefromi 的评论,您可以将 git log --pretty=oneline -n​​18 的输出与 git log --pretty=oneline -n​​25 进行比较;@{1} 并查看哪些提交丢失了。正如他提到的,如果您已经做了一些工作来确定哪个条目是您的预变基分支头,那么您可能需要使用一下 git reflog 。

To expand on Jefromi's comment a bit, you could compare the ouput of git log --pretty=oneline -n18 <branch> with git log --pretty=oneline -n25 <branch>@{1} and see which commits went missing. As he mentioned, you might need to use git reflog a bit if you've done some work since to determine which entry is your pre-rebase branch head.

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