In this scenario we have a number of commits in the _newbranch which has created two html files. In this scenario we only care about changes to one of the files but if we merged the branch then we'd merge all five commits and the unwanted changes.
To merge individual commits we use the
git cherry-pick <hash-id|ref> command. This behaves in a similar way to merge, if no conflicts exist then the commit will be automatically merged.
Follow the steps below to merge the three commits we're interested in. We want to replay the commits in the order they happened in the original repository.
- Cherrypick the commit with the message "Initial commit, no items"
- Cherrypick the commit with the message "Initial list"
- Cherrypick the commit with the message "Adding final items to the list"
We've used HEAD previously to indicate the tip of the current branch. You can refer across branches using the syntax _
new_branch~3 refers to the second to-last commit in the branch, in this case it has the commit message "Initial commit, no items"