git merges individual files from other branches, and partially merges files

Introduction

In the process of using git, sometimes we may have such a requirement. Some files on other branches are needed by our current branch, but if we use conventional merge, all the contents of other branches will be merged. It's not what we want. Here's a brief introduction to merging only specified files.

scene one

Currently there are two branches, master and develop. Three functions are developed on develop, which are function1.js, function2.js, and function3.js respectively. These functions are not available on master, so there are no these three files. Because For some reasons, function1.js needs to be launched first, so we need to merge function1.js to master, but function2.js and function3.js cannot be merged together.

accomplish

    git checkout source_branch <path>...

We first switch to the master branch, that is, the branch where resources are to be merged, and then execute it  git checkout develop function1.js. At this time, we will find that funciton1.js already exists on the master, and the specified file will be merged.

Specific steps:

git checkout master  // 先切换到master分支

git checkout develop function1.js // 合并develop上的function1.js

But one thing to note is that if there is already a function1.js file on the current master, and some other functions have been developed, when the function1.js on the develop is merged with the above method, the original file with the same name on the master will be completely deleted. Overwriting, not merging, is definitely not an option, which is the following scenario.

scene two

Different modules have been developed for function1 on master and develop, and function2 and function3 have been independently developed on develop. Now, function1 and function2 need to be uploaded first, that is, function1.js on the two branches will be merged, and the function2.js is merged into master.

accomplish

Cut out a temporary branch from the master, merge develop into the temporary branch, then switch to the master, and use the above  git checkout source_branch <path> method to merge the relevant files on the temporary branch into the master.

Specific steps:

git checkout master  // 先切换到master分支

git checkout -b master_temp // 从master切一个新的分支

git merge develop // 在 master_temp 上 merge develop 分支, 如果有冲突,解决下冲突,然后 commit 掉

git checkout master // 切回master

git checkout master_temp function1.js function2.js // 合并临时分支上的 function1.js, function2.js

Done~ In normal development, such scenarios may be rare, and they are all prepared and developed according to the specifications. Since there may be such scenarios, it is better to remember this little trick first.

Guess you like

Origin blog.csdn.net/weixin_44786530/article/details/130719331