Course lesson
Copy a Commit from One Branch to Another
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]
- Duration
- 1 min
- Access
- Free
- Transcript
- Retained from source evidence
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.
If we do git branch, we notice we have two branches. In addition, git log --oneline shows us our latest branch has our commit diverging from master, the reversion of the take 3 commit. We can switch over to master with git checkout master leaving our app.js empty.
A problem we could face is that we want the function we created on our separate branch js-changes transferred/copied to master without pulling the entire branch. We simply want to pick the individual commit to js-changes and move it to master.
git log --oneline shows us our current latest commit in master isn't up to date with the separate branch. What we can do to match the commit in our branches is use cherry picking. We run git cherry-pick {HASH} with the hash being the desired commit. Now, our git log --oneline actually shows there was a new commit hash created as it came over. What happened is we copied the commit from js-changes over to master and pushed it up, so the commit exists in both trees.