tags:

views:

5446

answers:

2

I have forked a branch from a repository it github and commit something for my using. now I found the original repository has a good feature which is at head, I want to merge it only without previous commits . what I should do? I have known how to merge all commit:

git branch -b a-good-feature
git pull repository master
git checkout master
git merge a-good-feature
git commit -a
git push

Thank you very much!

+21  A: 

'git cherry-pick' should be your answer here.

Apply the change introduced by an existing commit.

Do not forget to read bdonlan's answer about the consequence of cherry-picking in this post:
"Pull all commits from a branch, push specified commits to another", where:

A-----B------C
 \
  \
   D

becomes:

A-----B------C
 \
  \
   D-----C'

The problem with this commits is that git considers commits to include all history before them

Where C' has a different SHA-1 ID.
Likewise, cherry picking a commit from one branch to another basically involves generating a patch, then applying it, thus losing history that way as well.

This changing of commit IDs breaks git's merging functionality among other things (though if used sparingly there are heuristics that will paper over this).
More importantly though, it ignores functional dependencies - if C actually used a function defined in B, you'll never know.

VonC
Thank you, I solved this problem.
thanks for the information. I learn a little philosophy of git, although it is not very suitable for small project.Perhaps a better way to handle this would be to have more fine grained branches. That is, instead of just having a 'master', have 'featureA', 'bugfixB', etc. Perform code review on an entire branch at a time - where each branch is very focused on doing only one thing - and then merge that one branch when you're done. This is the workflow that git is designed for, and what it's good at :)
@openid000: "more fine grained branches": which is indeed exactly what bdonlan suggested in his answer.
VonC
Note: "git rebase" also changes SHA-1. See also "git rebase vs. git merge ( http://stackoverflow.com/questions/804115/git-rebase-vs-git-merge ) and "git workflow" ( http://stackoverflow.com/questions/457927/git-workflow-and-rebase-vs-merge-questions ) for cases where "git rebase" is legitimate.
VonC
Between "fine grained branches", "cherry-pick" and "rebase", you will then have all the possibilities for managing code in branches with git.
VonC
+6  A: 

You can use git cherry-pick to apply a single commit by itself to your current branch.

bdonlan
Thank you all the same!
+1 for your former post on cherry picking ( http://stackoverflow.com/questions/880957/pull-all-commits-from-a-branch-push-specified-commits-to-another/881014#881014 ). I took the liberty to copy an extract of it in my own answer above.
VonC