简体   繁体   English

如何在没有更改的情况下将分支合并到git中的master分支中?

[英]How to merge the branch with no changes into master branch in git?

Having a basic doubt in git branching and merge . 在git分支和合并中有一个基本的疑问。 Can a branch be merged without any changes into master . 可以合并分支而无需对主服务器进行任何更改。 If so , What happens to the delta changes that exists between master and branch . 如果是这样,主和分支之间存在的增量变化会发生什么。 Will that be overridden with master branch changes . 是否会被主分支更改覆盖。 Refer the below scenario : 请参阅以下方案:

I have two branches master and BugBranch. 我有两个分支主和BugBranch。

STEP 1: 步骤1:

C--B--A   (master)
      |
      X   (branch)

Took a branch from master say BugBranch . 从大师那里拿了一个分支说BugBranch。 Now I have a file TestMerge.java. 现在我有一个文件TestMerge.java。

public static void main(String[] args){
...
...
callSomeMethod();
  switch(decision)
  {
    case A:
        do_something
        break;
    case B:
        do_something
        break;
  }
}

So at STEP 1 , both master and BugBranch looks same. 所以在第1步,master和BugBranch看起来都一样。

STEP 2: The file(TestMerge) is changed in master as below . 第2步:在master中更改文件(TestMerge),如下所示。 ( just removed the switch ) (刚卸下开关)

public static void main(String[] args){
...
...
callSomeMethod();
}

C--B--A--1  (master)
      |
      X     (branch)

STEP 3: The problem is when I try to merge the branch with the master . 第3步:问题是当我尝试将分支与主服务器合并时。 Expecting a merge conflict in file , since there exists a delta for sure and want to preserve the changes in BugBranch . 期待文件中的合并冲突,因为肯定存在delta并且想要保留BugBranch中的更改。 But the strange thing is that , its getting overridden with the master changes . 但奇怪的是,它被主人改变了。

Tried out the below commands 试过以下命令

git checkout master git checkout master

git merge BugBranch git merge BugBranch

This is really confusing ! 这真令人困惑!

A Git branch is just a pointer to a commit. Git分支只是一个提交指针。 The X branch in your question points to commit A and for many Git operations, X can be used instead of A and vice-versa and you get the same result. 你问题中的X分支指向提交A ,对于许多Git操作,可以使用X而不是A ,反之亦然,你得到相同的结果。

Only by its creation, a Git branch does not produce any change in the repository. 只有通过它的创建,Git分支才会在存储库中产生任何变化。 Until you do some changes in the files and commit them, the branch does not diverge from its source branch ( master in your case). 在对文件进行一些更改并提交它们之前,分支不会偏离其源分支(在您的情况下为master )。

You change some files and committed on master (commit 1 ). 您更改了一些文件并在master (commit 1 )上提交。 If you didn't change and commit anything on branch X , it still points to commit A which is in the history of branch 1 (actually, it's its parent). 如果你没有在分支X上更改和提交任何内容,它仍然指向提交A ,它位于分支1的历史中(实际上,它是它的父级)。

When you want to commit branch X into branch master , Git detects that the branch X is in the past of branch master ; 当你想将分支X提交到分支master ,Git检测到分支X在分支master的过去; starting from master , if Git repeatedly jumps from one commit to one of its parents it can reach the commit pointed by branch X (which is the A commit). master开始,如果Git反复从一个提交跳转到它的一个提交,它可以到达分支X指向的提交(这是A提交)。

This means the branch X doesn't contain anything that is not already in branch master . 这意味着分支X不包含任何尚未在分支master In other words, there is nothing to merge and Git informs you about this situation with the message "Already up-to-date.". 换句话说,没有任何东西可以合并,Git会通过“已经是最新的”消息通知您这种情况。

On the other hand, if you want to merge master into X , because master is in the future of X , Git transforms the merge operation into a "fast-forward" operation. 另一方面,如果要将master合并到X ,因为masterX的未来,Git会将合并操作转换为“快进”操作。 This means, it doesn't create an unnecessary new merge commit and just pushes the X branch (the branch you merge into) forward until it reaches the master commit (the branch you merge from). 这意味着,它不会创建不必要的新合并提交,只是向前推送X分支(您合并的分支),直到它到达master提交(您合并的分支)。

You can force Git to create a merge commit by using the --no-ff flag in the git merge command line but, except for really exceptional situations, you don't have any reason to do it. 您可以通过在git merge命令行中使用--no-ff标志强制Git创建合并提交,但除了非常特殊的情况之外,您没有任何理由这样做。 It produces a commit that doesn't introduce any change in the files. 它会生成一个不会在文件中引入任何更改的提交。

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

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