Why does git delete the files in the branch when merging?

Let’s first reproduce the situation where merging will delete code:

Examples are as follows:

1. Initialize a warehouse, create a new master.txt file, and then submit it.

$ git init tem
Initialized empty Git repository in D:/temp/tem/.git/

$ cd tem

$ touch master.txt

$ vim master.txt

$ cat master.txt
this is master branch!

$ git commit -a -m "commit master branch"
[master (root-commit) 3e4d6ff] commit master branch
 1 file changed, 1 insertion(+)
 create mode 100644 master.txt

2. Create a new branch develop, create a file develop.txt, and submit it.

$ git checkout -b develop
Switched to a new branch 'develop'

$ touch develop.txt

$ vim develop.txt

$ cat develop.txt
this is develop branch!

$ git add .

$ git commit -a -m "commit develop branch"
[develop f439ba3] commit develop branch
 1 file changed, 1 insertion(+)
 create mode 100644 develop.txt

3. Create branch feature1, create a new file feature.txt, and submit it.

$ git checkout -b feature1
Switched to a new branch 'feature1'

$ touch feature1.txt

$ vim feature1.txt

$ git add .

$ git commit -a -m "feature1 commite"
[feature1 98e05e7] feature1 commite
 1 file changed, 1 insertion(+)
 create mode 100644 feature1.txt

4. Merge the develop branch.

$ git checkout master
Switched to branch 'master'

$ ll
total 1
-rw-r--r-- 1 mydwh 197121 23 Jul 22 16:18 master.txt

$ git merge develop
Updating 3e4d6ff..f439ba3
Fast-forward
 develop.txt | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 develop.txt

5. Merge feature1 branch

$ git merge feature1
Updating f439ba3..98e05e7
Fast-forward
 feature1.txt | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 feature1.txt

$ ll
total 3
-rw-r--r-- 1 mydwh 197121 25 Jul 22 17:01 develop.txt
-rw-r--r-- 1 mydwh 197121 25 Jul 22 17:03 feature1.txt
-rw-r--r-- 1 mydwh 197121 23 Jul 22 16:18 master.txt

6. Delete the feature1.txt file and submit it.

$ rm feature1.txt

$ ll
total 2
-rw-r--r-- 1 mydwh 197121 25 Jul 22 17:01 develop.txt
-rw-r--r-- 1 mydwh 197121 23 Jul 22 16:18 master.txt

$ git commit -a -m "delete feature1.txt,commit master branch"
[master 88cd117] delete feature1.txt,commit master branch
 1 file changed, 1 deletion(-)
 delete mode 100644 feature1.txt

7. Log out to the feature1 branch, delete master.txt, develop.txt, and then submit.

$ git checkout feature1
Switched to branch 'feature1'

$ ll
total 3
-rw-r--r-- 1 mydwh 197121 25 Jul 22 17:01 develop.txt
-rw-r--r-- 1 mydwh 197121 25 Jul 22 17:08 feature1.txt
-rw-r--r-- 1 mydwh 197121 23 Jul 22 16:18 master.txt

$ rm master.txt develop.txt

$ ll
total 1
-rw-r--r-- 1 mydwh 197121 25 Jul 22 17:08 feature1.txt

$ git commit -a -m "delete master.txt ,develop.txt,commit feature1 branch"
[feature1 70b4ed7] delete master.txt ,develop.txt,commit feature1 branch
 2 files changed, 2 deletions(-)
 delete mode 100644 develop.txt
 delete mode 100644 master.txt

8. Log out of the master branch and merge the feature1 branch.

$ git checkout master
Switched to branch 'master'

$ ll
total 2
-rw-r--r-- 1 mydwh 197121 25 Jul 22 17:11 develop.txt
-rw-r--r-- 1 mydwh 197121 24 Jul 22 17:11 master.txt

$ git merge feature1
Merge made by the 'ort' strategy.
 develop.txt | 1 -
 master.txt  | 1 -
 2 files changed, 2 deletions(-)
 delete mode 100644 develop.txt
 delete mode 100644 master.txt

$ ll
total 0

Question: During the last merge, I found that git not only failed to merge, but deleted the files, why?

First of all, let me clarify, is git's merge a mathematical collection merge?

No. The merger in mathematics is that set A has {a, b} and set B has {c}, then the merged set is {a, b, c}.

As for the merging of git, I think it is the merging of branch developers' wishes, not the merging of source code files. Understand this, and it will be easier to understand why git deletes files when merging.

In my experiment, if the master branch is compared to the company founder, he first created the file master.txt, put it in the company's warehouse, and used it himself. Then the master wanted to go into retreat and practice, so he found a project manager develop. He created the file develop.txt based on the company's original file and put it in the company warehouse for the CCP's own use. After a while, develop also wanted to practice in retreat, so he found another project manager, feature1, who created the file feature1.txt for his own use.

Since the master is in retreat, he does not know that there are files develop.txt and feature1.txt in the company warehouse. When master and develop merge, they both exchange their willingness to use the files in the warehouse. The result of the merger is that the master perceives that there is a develop.txt file in the warehouse and can use it. Note that the merge does not mean that the master copies the develop.txt file, but that there is a develop.txt file in the warehouse that I can use.

In the same way, when master merges with feature1, master perceives that the warehouse has a file feature1.txt that it can use.

It's like the founder of the company came out and exchanged opinions with the two project managers. The founders of the company found that the company's assets had increased. It became three files.

Next, both master and feature1 govern the company. The master deleted the files master.txt, develop.txt. feature1 deleted the file feature1.txt.

Note that you need to understand git deletion here. As long as you submit the file to the warehouse, it will always be in the warehouse. As long as you want it, you can always find it. Therefore, the deletion of a file can be understood as the deprecation of the file by the branch, rather than actual deletion from the warehouse.

The situation at this time is that the master only uses the feature1.txt file in the warehouse, and discards master.txt and develop.txt. Feature1 only uses the master.txt and develop.txt files, and the feature1.txt file is discarded. When the master wants to merge the feature1 branch, many people think it is a merge of files. Then there are no files master.txt and develop.txt in the master branch. Then merge the master.txt and develop.txt in the feature1 branch. In this way, master There are three files in the branch. In fact, this is not the case. The merger is essentially the master's intention to adopt feature1. Because when the master deleted the master.txt and develop.txt files, feature1 did not express any objection, then git considered that there was no conflict. The master's wish was to abandon the master.txt and develop.txt files. Git respected the master when merging. will. In addition, feature1 has deprecated the feature1.txt file, and master has no objection. Then git thinks there is no conflict, adopts feature1's wishes, and deletes the feature1.txt file in the master branch during the merge. The final result of the merger is: the latest willingness to use files from the two branches is combined, that is, after the merger, there are no files in the master branch.

Many people have git delete the source code when merging branches. This is the principle. Once you understand, there are many ways to avoid the possibility of source code being deleted.

Guess you like

Origin blog.csdn.net/wwwjr00/article/details/131869388
Recommended