Updated: 24.02.2023

This guide is generally from StackOverflow on how to selectively merge files form one branch to another.

Basically it’s:

git checkout main
git merge --no-ff --no-commit dev

Where dev is the branch you want to merge from into the current branch ie. main.

The --no-commit option will stage the files that have been merged by Git without actually committing them. This will give opportunity to modify the merged files however you want to change and then commit them yourself.

For alternatives to merge the files:

True merge

You want to accept the merged files the way Git merged them automatically and then commit them.

Useful options to use

Only some file to merge

You want to retain the version in the current branch eg. main and ignore the version in the branch you are merging from eg. dev

git checkout HEAD R/selected-file.R

This retain the version of selected-file.R in the current branch and overwrite the selected-file.R automerged by Git.

Only the version in branch dev

Retreive the version of selected-file.R in dev branch and overwrite selected-file.R auto-merged by Git with:

git checkout dev R/selected-file.R

Select only specific merges in selected-file.R

You can edit the modified selected-file.R directly, update it to whatever version of selected-file.R you want, and then commit.

If Git cannot merge the file automatically, it will report the file as unmerged and produce a copy where you will need to resolve the conflict manually.

Example

To explain further with an example, let’s say you want to merge branchX into the current branch:

git merge --no-ff --no-commit branchX

You then run the git status command to view the status of modified files.

For example:


git status

# On branch master
# Changes to be committed:
#
#       modified:   file1
#       modified:   file2
#       modified:   file3
# Unmerged paths:
#   (use "git add/rm <file>..." as appropriate to mark resolution)
#
#       both modified:      file4
#

Where file1, file2, and file3 are the files git have successfully auto-merged.

What this means is that changes in the master and branchX for all those three files have been combined together without any conflicts.

You can inspect how the merge was done by running the git diff --cached;


git diff --cached file1
git diff --cached file2
git diff --cached file3

If you find some merge undesirable then you can

If you don’t want to merge file1 and want to retain the version in the current branch

Run

git checkout HEAD file1

If you don’t want to merge file2 and only want the version in branchX

Run

git checkout branchX file2

If you want file3 to be merged automatically, don’t do anything.

Git has already merged it at this point.

file4 above is a failed merge by Git. This means there are changes in both branches that occur on the same line. This is where you will need to resolve the conflicts manually. You can discard the merged done by editing the file directly or running the checkout command for the version in the branch you want file4 to become.

Finally, don’t forget to git commit.

comments powered by Disqus