Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to merge a specific commit in Git

Tags:

git

merge

I have forked a branch from a repository in GitHub and committed something specific to me. Now I found the original repository had a good feature which was at HEAD.

I want to merge it only without previous commits. What should I do? I know how to merge all commits:

git branch -b a-good-feature git pull repository master git checkout master git merge a-good-feature git commit -a git push 
like image 478
netawater Avatar asked May 19 '09 05:05

netawater


People also ask

How do I select a commit for merge request?

From a merge request On the top bar, select Main menu > Projects and find your project. On the left sidebar, select Merge requests, and find your merge request. In the merge request's secondary menu, select Commits to display the commit details page. Select the title of the commit you want to cherry-pick.

Can you merge just one file in git?

In Conclusion. We can use git checkout for far more than simply changing branches. If we supply it with a branch name and a file, we can replace a corrupted or broken file. Instead, if we want to pass some of the changed content we can use the --patch flag to manually merge an individual file.


1 Answers

'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 commit 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.

like image 172
VonC Avatar answered Oct 04 '22 18:10

VonC