Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to exclude specific commits when doing a git merge?

Tags:

git

Let's say that I want to merge from a release branch to the master branch and there are some commits in the release branch that I don't want to include in the master branch. Is there a way to do the merge so that one or more of those commits will not be merged?

My strategy so far is to do the following (in master):

git merge --no-commit release-branch # Resolve conflicts and apply reverse patch of the commits that I don't want included git commit # Edit commit message so that it lists the commits that have been reverse-patched 

Is there a better way to do this?

like image 216
readonly Avatar asked Dec 01 '08 22:12

readonly


People also ask

How do I remove a commit from a merge?

How to Undo a Merge Commit in Git. You can use the Git reset command to undo a merge. Firstly, you need to check for the commit hash (or id) so you can use it to go back to the previous commit. To check for the hash, run git log or git reflog .

How do I exclude a commit?

Set “–assume-unchanged” to a path to exclude to check on git commit and it will exclude your file from git commit. You will need to use the git update-index and –assume-unchanged to exclude files from git commit.


1 Answers

I've found a solution that works for me in the Pro Git book.

Let's say you want to exclude the file config.php.

On branch A:

  1. Create a file named .gitattributes in the same dir, with this line: config.php merge=ours. This tells git what strategy to use when merging the file. In this case it always keep your version, ie. the version on the branch you are merging into.

  2. Add the .gitattributes file and commit

On branch B: repeat steps 1-2

Try merging now. Your file should be left untouched.

like image 142
fcurella Avatar answered Sep 20 '22 19:09

fcurella