Skip to content

How to: Exclude files or folder from merge

Michael Mans edited this page Jul 24, 2017 · 1 revision

This pages gives a quick example on how to exclude files or folders when merging branches

Use Case

You want to create a new branch (e.g. a release branch) without a specific file or folder (because this might be not ready for a release) which already exists in the base branch.

How to Git console that?!

First you need to identify the commit bevor the file or folder was integrated into the git versioning. After you identified this specific commit, you can start by checking out this specific commit

git checkout specific_comit_aka_SHA-1_checksum

now your HEAD should be detached and point to the specific commit. The next step is to create a branch, based on this commit with:

git checkout -b name_of_your_branch

With this branch you are at the same point (history, files changes and everything) like the specific commit (the one bevor the integration of the specific files and folders)

Now you can start doing the magic. What you will do now is the following:

  • You'll merge the branch and state of the branch you really want to base on, meaning the branch of your imagination only with the files and folders you, in fact, don't want.
  • Then you'll reset your HEAD of the files and folders you do not want to the commit bevor these files and folders are integrated
  • then you'll clean it and commit it

Let's start with merging the branch:

git merge --no-commit --no-ff branch_with_the_files_you_dont_want

--no-commit and --no-ff are flags for git that with your merge you don't want to perform an automated commit and fast-forward the changes. As a result you get with git status the status of your branch with a lot of modified or added files. now we want to exclude the files we don't like:

git reset HEAD -- path/to/yourfile this will reset your file you do not want to the actual HEAD used in this branch, which is the commit bevor the file/folder was added. After that you can clean your git repository with:

git clean -fd which will clean your repo and delete the files you don't want.

The last step is to commit the changes with:

git commit -m "your message"

and you are done. Now you got a new branch based on another branch, but without the specific files or folder you didn't want to include

Clone this wiki locally