Copy a Commit from One Branch to Another

Chris Achard
InstructorChris Achard
Share this video with your friends

Social Share Links

Send Tweet
Published 4 years ago
Updated 3 years ago

If you want to pull over just a single commit from one branch onto another, you can do that with git cherry-pick [HASH-TO-MOVE]

We'll demonstrate that by making a commit on a feature branch, and then copying that commit (but not the entire branch) onto master.

Instructor: [00:00] If we do git branch, we see we have two different branches here. If we do git log one line, we can see that our latest branch has adsHelloWorld as the commit, and that is diverse from master, which is the reversion of the take three commit.

[00:17] This is our app.js on the JSChanges branch. Let's check out master. Now our app.js is empty. Let's say we're on master and we really want that function on master, but we don't want to pull over the entire JSChanges branch yet. What we really want is just to pick this commit and move it over to master.

[00:38] If we do a git log one line now, we see that the latest is revert take three, and we want this adsHelloWorld. We're going to get the hash of the commit that we want, and we're going to do git cherry-pick, and then we're going to do the hash that we want.

[00:56] If we do a git log one line, we can see that our head is pointing at adsHelloWorld, but the hash is different. It's made a new commit hash as it came over and we have our function here.

[01:11] If we are happy with what we have now, we can do a git push. Now, we've copied the commit from js.changes over the master and pushed it up. The commit now exists in both trees.

egghead
egghead
~ an hour ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today