Post

Two useful uses of git when working with monorepo - skipping or selecting only a specific part of a branch

During my adventure with monorepo, I’ve often struggled with the need to skip a particular module, directory, or set of files when merging from one branch to another.

On the other hand, I also had the need to merge only a specific module, directory or set of files.

This happened so often that I’ve created a cheat sheet that I’ll share below. I hope you find it as useful as I did.

Merge everything but a specific item

First, check out the target branch.

1
git checkout dest_branch

Do the merge, but do not commit. Do not resolve conflicts (if any) yet.

1
git merge --no-commit --no-ff source_branch

Reset the directory (or files) you want to skip in the merge.

1
git reset -- module_to_skip

Now it is time to resolve conflicts and commit changes.

1
git commit

Then you can remove files from the Changes not staged for commit section.

1
git restore module_to_skip

And files from the Untracked files section.

1
git clean -fd

Merge only a specific item

Of course, in this case, we could use the exact same procedure as above, but with many more modules, directories, or files reset.

Fortunately, for our use case, the following way was just as useful.

First, check out the destination branch.

1
git checkout dest_branch

Checkout the necessary changes from the source branch.

1
git checkout source_branch -- module_to_get

Resolve conflicts and commit.

1
git commit

There is no other way

I have found many more uses of git to perform similar operations. I’m not saying the above is the only one. It was convenient to use. If you have another more efficient way, please share it in the comments section below.

This post is licensed under CC BY 4.0 by the author.