简体   繁体   English

合并后,合并文件的提交ID(无冲突)如何更改?

[英]After merge, how does the commit ID of a merged file (no conflicts) change?

Suppose we have branch A, and branch B. 假设我们有分支A和分支B。

Branch A works on file a.cpp, and have several commits say commit ID 10000 10001, 10002, 10003 (faked). 分支A在文件a.cpp上工作,并具有多个提交,例如提交ID 10000 10001、10002、10003(伪造)。

Branch B has a.cpp. 分支B具有a.cpp。 a.cpp still shows the old commit ID 10000. [Note] Branch B has other commits already. a.cpp仍显示旧的提交ID10000。[注意]分支B已经具有其他提交。 But a.cpp history in Gighub only show one old commit ID 10000. 但是Gighub中的a.cpp历史记录仅显示一个旧的提交ID 10000。

Now we merge A to B. In B, we have a.cpp. 现在我们将A合并到B。在B中,我们有一个.cpp。 What are its history commit ID series "FOR a.cpp File"? 它的历史提交ID系列“ FOR a.cpp文件”是什么? I didn't mean the commit history for B. I mean the history for the file a.cpp (you will see that in Gighub) 我的意思不是B的提交历史。我的意思是a.cpp文件的历史(您会在Gighub中看到)

Are they same commit IDs? 它们是相同的提交ID吗?

I assumed they are the same. 我以为他们是一样的。 But from one practical project on Github, I find they are not the same! 但是从Github上的一个实际项目中,我发现它们并不相同!

The problem is: Someone made modification on some other files in Branch B (not on a.cpp file). 问题是:有人对分支B中的某些其他文件(而不是a.cpp文件)进行了修改。 Later on I merge B to A, since I work on A, I find that there are conflicts for a.cpp file, although they are the same! 后来我将B合并到A,因为我在A上工作,所以我发现a.cpp文件存在冲突,尽管它们是相同的! (but different commit IDs). (但提交ID不同)。

I don't want to resolve the conflicts, which is time-consuming. 我不想解决冲突,这很耗时。

[Update 1] Here is some quote from Do the commit ids remain same after a merge? [更新1]这是来自的一些引用合并后,提交ID是否保持不变?

If your work is rebased (or cherry-picked) into an upstream, and you fetch and then attempt to merge the upstream, git will sometimes, but not always, be able to detect the duplication and clean up automatically. 如果您将工作重新设置(或挑选)到上游,并且先获取然后尝试合并上游,则git有时(但并非总是)能够检测到重复并自动清除。 When it cannot auto-detect the duplication, you will almost always get various merge conflicts: 当它无法自动检测到重复项时,您几乎总是会遇到各种合并冲突:

How come? 怎么会? If Git cannot automatically detect no change of some files, it creates a lot of fake merge conflict errors, which is time-consuming to manually solve. 如果Git无法自动检测到某些文件没有变化,则会创建许多伪造的合并冲突错误,这是手动解决的耗时。

[Update 2] Thanks for answers below. [更新2]感谢您在下面的回答。 However, in github, I find that different commit IDs for the same commit files, authors and change. 但是,在github中,我发现相同提交文件,作者和更改的提交ID不同。 See the quote from Do the commit ids remain same after a merge? 请参见引用。 合并后提交ID是否保持不变?

I viewed the commit log of the team's branch and realized that although the commit message, author(me) & date were same, the commit id was different in the remote branch. 我查看了团队分支的提交日志,并意识到尽管提交消息,author(me)和日期是相同的,但远程分支中的提交ID是不同的。

[Update 3] For Tomato Branch: [更新3]对于Tomato Branch:

1000, 1001, 1002, 1003 1000、1001、1002、1003

For Potato Branch: 对于马铃薯分公司:

1000, 2001, 2002, 2003 1000、2001、2002、2003

Now merge Tomato Branch to Potato Branch: 现在将Tomato Branch合并到Potato Branch:

The common ancestor is 1000, and the merge commit is 2004 共同祖先是1000,合并提交是2004

Between 1000 and 2004, there are 1001, 1002, 1003, 2001, 2002, 2003. 在1000年至2004年之间,有1001、1002、1003、2001、2002、2003。

it seems Git put them between based on chronicle order, AND change the commit name. 似乎Git根据编年顺序将它们放在两者之间,并更改提交名称。 So it would like 所以它想

1000 1000

( 3001 (from 1001), (3001(从1001开始,

3002 (form 1002), 3002(表格1002),

4001 (from 2001), 4001(从2001年开始),

4002 (from 2002), 4002(从2002年开始),

4003 (from 2003), 4003(从2003年开始),

3003 (from 1003) ) 3003(从1003开始)

2004 2004年

Merge are based on common ancestor 1000, and the two latest commits from the two branches (1003, 2003). 合并基于共同祖先1000和两个分支的两个最新提交(1003,2003)。

Git also give out 4003, which might mean it also merge: Git还给出了4003,这可能意味着它也合并了:

2003 AND 1002 2003和1002

So actually Git might do many merging operations in order to give out an effective chronicle intermediate commits. 因此,实际上,Git可能会进行许多合并操作,以便给出有效的编年史中间提交。

Each commit always contains all files of your repository. 每次提交始终包含存储库中的所有文件。 If you change any file (and commit that change) you will get a new commit with a new commit id. 如果您更改任何文件(并提交更改),您将获得具有新提交ID的新提交。

  • Without any commit on B: 没有对B的任何提交:

Before the commit your history looks like this: 在提交之前,您的历史记录如下所示:

10000, 10001, 10002, 10003
  B                    A

You have 4 commits. 您有4次提交。 Branch B points to the first one and branch A to the last one. 分支B指向第一个,分支A指向最后一个。

After the merge it looks like this: 合并后,它看起来像这样:

10000, 10001, 10002, 10003
                      A,B

Git noticed that you did not do any commits on B and therefore simply does a fast-forward. Git注意到您没有对B进行任何提交,因此只进行了快进。 Therefore now both branches point to the same commit. 因此,现在两个分支都指向同一提交。

  • If there was another commit on B: 如果对B有另一次提交:

Before the commit your history looks like this: 在提交之前,您的历史记录如下所示:

10000,    10001,   10002,   10003
   10004                        
     B                        A 

Now commit 10004 was made on branch B. while branch A still points to 10003. 现在在分支B上进行了提交10004,而分支A仍指向10003。

After the merge it looks like this: 合并后,它看起来像这样:

10000,    10001,   10002,   10003
   10004                           10005
                             A      B

Now git created a real merge commit (10005) combining 10003 and 10004. If 1004 did not change any file change by A there should be no merge conflict. 现在git创建了一个合并合并10003和10004的真实合并提交(10005)。如果1004没有更改A进行的任何文件更改,则应该没有合并冲突。

To see how things work in git, sometimes I like to use Visualizing Git Concepts with D3 which does a reasonable job of showing how the repository looks with each commit or branch or rebase. 为了查看git的工作方式,有时我喜欢将Visualization Git Concepts与D3结合使用 ,该工具可以合理地显示每次提交,分支或变基后仓库的外观。

Starting out with: 开始于:

git branch B
git commit
git commit
git commit

the repo will look like: 回购将如下所示:

在此处输入图片说明

Checking out B ( git checkout B ) brings us to: git checkout B出B( git checkout B )将我们带到:

在此处输入图片说明

And now to bring the two into sync with a git merge master : 现在,使两者与git merge master同步:

在此处输入图片说明

The history on the side looks like: 侧面的历史记录如下:

在此处输入图片说明

Note the fast-forward merge. 注意快进合并。 Git detected no changes were done on B since the branch so it was able to just move the label. Git检测到自分支以来B上没有进行任何更改,因此它只能移动标签。

However, if we don't move to the B branch and instead do a git merge B while on master, we get: 但是,如果我们不移至B分支,而是在master上进行git merge B ,则会得到:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM