-1

I have situation described in the image below. I want to create new branch that will exclude commits 1 & 2, and will consist commits 4 & 3.

enter image description here

Alexander Gorelik
  • 3,985
  • 6
  • 37
  • 53
  • 3
    `git checkout -b new_branch_name actual_master_commit;git cherry-pick 3 4`. Solution 2, `git checkout -b new_branch_name 4;git revert 2 1 --no-edit`. – ElpieKay Sep 26 '17 at 09:10
  • @ElpieKay the cherrypick 3,4 includes all the changes of 1,2 how exactly this going to work? – Alexander Gorelik Sep 26 '17 at 09:19
  • 1
    No, they shouldn't. Do you encounter a conflict when cherry-picking 3 and 4? – ElpieKay Sep 26 '17 at 09:21
  • The second approach works well thanks.you can add it as answer, When i cherry pick some commit(3 or 4) it is already contains all his parents (1-2). I my case i want completely ignore changes done in 1 and 2 – Alexander Gorelik Sep 26 '17 at 09:40
  • `git-cherry-pick` works with diff only. So `git cherry-pick 3` applies the diff between 2 and 3, not including the diff between 2 and its parent 1. – ElpieKay Sep 26 '17 at 09:48

1 Answers1

2

I am assuming that the tree goes 4 -> 3 -> 2 -> 1, that is, 4 is the oldest commit, and 1 the newest. If so,

git checkout -b new_branch <commit-hash-of-3>

However, if the tree goes in the opposite direction, you will create a copy of your branch at commit 4 (tree is 1 -> 2 -> 3 -> 4), and revert the changes made in the old commits 1 and 2.

git checkout -b new_branch <hash-of-4>
git revert <hash-1> <hash-2>

Another solution would be to checkout to the newest commit before 1 and rebase on top of this.

git checkout -b new_branch <hash-of-1>^
git rebase -i <hash-of-4>

and in the editor, just drop the commits for 1 and 2. (This is similar to cherry picking)


For more reading purposes, do look at git scm page.

hjpotter92
  • 78,589
  • 36
  • 144
  • 183