-1

When merging a branch A with another branch B, how to exclude some commits from B to merge?

For example, B has 100 commits, how can I merge the specific 98 commit and exclude the 2?

  • https://stackoverflow.com/questions/1994463/how-to-cherry-pick-a-range-of-commits-and-merge-into-another-branch – Javad Jafari Aug 26 '17 at 09:41
  • Possible duplicate of [How to cherry pick a range of commits and merge into another branch](https://stackoverflow.com/questions/1994463/how-to-cherry-pick-a-range-of-commits-and-merge-into-another-branch) – Javad Jafari Aug 26 '17 at 09:42
  • Possible duplicate of [How to take out ones of commit from a branch](https://stackoverflow.com/questions/45893126/how-to-take-out-ones-of-commit-from-a-branch) – max630 Aug 26 '17 at 09:51
  • Could you just do the merge, and then 'git revert' the two commits you don't want? You could incorporate that into your merge commit, or do it explicitly as separate commits after the merge. – Mort Aug 26 '17 at 13:15
  • 1
    Possible duplicate of [Is it possible to exclude specific commits when doing a git merge?](https://stackoverflow.com/questions/332528/is-it-possible-to-exclude-specific-commits-when-doing-a-git-merge) – phd Aug 26 '17 at 13:29

4 Answers4

0

So for example if you're on branchB and you git add . and git commit you could git reset <file name> for the ones you want to exclude then when you merge branchB into branchA it will only include the 98 that are committed.

  • Thanks for sharing, but I need to completely exclude the commit hash code and message in the history of my new merge branch. The way seems adding one rest commit on. Top of 100 commit. So the totally will be 101 commit and last one is for reset. Is my understanding correct? – user3472080 Aug 26 '17 at 10:09
0

git cherry-pick
That might be the command you want. It ables you to select the commits you want.

https://git-scm.com/docs/git-cherry-pick

git cherry-pick [--edit] [-n] [-m parent-number] [-s] [-x] [--ff]
          [-S[]] …​
git cherry-pick --continue
git cherry-pick --quit
git cherry-pick --abort

Description extract from the site

Given one or more existing commits, apply the change each one introduces, recording a new commit for each. This requires your working tree to be clean (no modifications from the HEAD commit).

Frederic
  • 2,015
  • 4
  • 20
  • 37
kenfire
  • 1,305
  • 12
  • 23
  • Does that mean I have to cherry pick 98 commits one by one and do manually merge in between? Is there cherry-pick commit like cherry-pick all except "commit hash code#2 "commit hash code #2" – user3472080 Aug 26 '17 at 10:14
  • @user3472080 You can find an example here: https://stackoverflow.com/questions/9853681/git-create-branch-from-range-of-previous-commits/9853814#9853814 – kenfire Aug 27 '17 at 06:53
0

After the merge you could rebase interactively and exclude the unwanted commits:

git rebase -i HEAD~100

You may want to create an intermediate branch from the source do rebase -i and then merge that into your target branch.

Something like the following.

git checkout source
git checkout -b middle
git rebase -i HEAD~100 or commit-ish
git checkout target
git merge middle
tymtam
  • 31,798
  • 8
  • 86
  • 126
0

If the changes are sequential/consecutive from the latest commit back then its easy.

First you would switch to the branch you merging into, lets call this branch 'des' and the branch with the changes 'src'.

git checkout dsc

Then, merge the src branch into des with the --no-commit option, this will merge the changes but will not create a commit.:

git merge --no-commit src

Then, use git log command (or your git gui/tools) to find the hash of the commit you want to exclude from. Finally Reset the dst branch to the commit before the unwanted commit using the git reset command:

git reset --hard a123456^

Now commit with a message:

git commit -m "Merge src into dsc (excluding unwanted commit)"
Steven Mark Ford
  • 3,372
  • 21
  • 32