Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I merge a single commit?

Tags:

git

merge

commit

Sometimes I'm working with several branches at once. Say I have branches called master, release-1.1, and experimental. I create a new file or make a change in experimental and I want that one single change to apply to the other branches.

Can I do this in git? If I simply merge the commit into another branch, git automatically "fast-forwards" and includes any commits in between. But there must be some way to handle this use case.

like image 388
Bill Avatar asked Nov 03 '09 02:11

Bill


People also ask

Can I git merge a single file?

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.

How do I merge a specific commit in Sourcetree?

Double-click the main branch to switch to that branch. Click the Merge button. From the popup that appears, make sure the commit on your wish-list branch is highlighted. You are indicating that you want to add the commit from this branch to the main branch.


1 Answers

What you want to do is called cherry picking. You can cherry pick a single commit using the following command:

$ git cherry-pick <commit hash or name>

This will incorporate the change from that commit only into your current branch. Note, however, that this creates a new commit; this new commit has the exact same changes as the cherry-picked commit (and even the same commit date and author), but it is technically a new commit, so you'll see it as a new commit in, e.g., gitk. You'll also have to perform the cherry pick for each branch in which you want to see the change.

like image 190
mipadi Avatar answered Oct 19 '22 16:10

mipadi