Git tips, how to merge individual files or folders from other branches

In actual work, a large-scale project or version iteration may not go online at one time, but may go online several times. At this time, it will involve creating multiple branches for separate development.

create branch

The function is divided into two branches, namely A and B.
There is a list page function on A,
and there is a detail page function on B, and a system message function

The product manager said that the list function should be launched first, so we developed the A branch, and the list function was developed quickly.
On the second day, the B branch was developed according to common sense. Halfway through the development, the product manager said that the current system message function needs to be launched urgently, and it needs to be launched together with the list function. For the system message function, after the development is completed, the list function and the system message function need to be tested on a branch. At this time, branch merging comes in handy.

merge branch

It is a common practice to use the git merge command to merge branches, but when git merge merges, the contents of the two branches will be completely merged. If you want to merge part of them, it will definitely not work. So what to do?

How to merge specified files from other branches to the current branch, git checkout is a suitable tool.

git checkout source_branch <path>...

forced merger

We use git checkout to add the system message function on the B branch to the A branch

$ git branch
  * A  
    B
    
$ git checkout B message.html message.css message.js other.js

$ git status
# On branch A
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#    new file:   message.css
#    new file:   message.html
#    new file:   message.js
#    modified:   other.js
#

merge complete

Note: When using git checkout a file to the current branch, the corresponding file of the current branch will be forcibly overwritten

There is no problem with adding new files here, but the original other.js on branch A will be forcibly overwritten. If other.js on branch A is modified, the content of other.js will be forcibly overwritten during checkout, which is definitely not possible. of. How to avoid non-mandatory coverage, see below.

smart merge

1. Using git checkout will create an A_temp branch based on the A branch to avoid affecting the A branch

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

2. Merge the B branch into the A_temp branch

$ git merge B
Updating 1f73596..04627b5
Fast-forward
 message.css                     | 0
 message.html                    | 0
 message.js                      | 0
 other.js                        | 1 +
 4 files changed, 1 insertion(+)
 create mode 100644 message.css
 create mode 100644 message.html
 create mode 100644 message.js

3. Switch to the A branch, and use git checkout to overwrite the system message function-related files or folders on the A_temp branch to the A branch

$ git checkout A
Switched to branch 'A'

$ git checkout A_temp message.html message.css message.js other.js

$ git status
# On branch A
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#    new file:   message.css
#    new file:   message.html
#    new file:   message.js
#    modified:   other.js
#

ok, it's over, this is the experience of using git merge at work, for reference only, please point out any mistakes, thank you!

Guess you like

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